-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
79 lines (72 loc) · 2.27 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>3D Pie Chart with Three.js</title>
<style>
body { margin: 0; overflow: hidden; }
#canvas { width: 100%; height: 100%; display: block; }
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
// 获取数据
let data = [
{ label: "A", value: 10, color: 0xff0000 },
{ label: "B", value: 20, color: 0xffff00 },
{ label: "C", value: 30, color: 0x00ff00 },
{ label: "D", value: 40, color: 0x0000ff }
];
// 计算角度和颜色
let total = data.reduce((sum, item) => sum + item.value, 0);
let angles = [];
let colors = [];
for (let item of data) {
angles.push(Math.PI * 2 * item.value / total);
colors.push(item.color);
}
// 创建场景、相机、渲染器
let scene = new THREE.Scene();
let camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 0, 5);
let renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// 创建饼图
let geometry = new THREE.ConeGeometry(1, 2, data.length, 1, true);
let material = new THREE.MeshBasicMaterial({ color: 0xffffff, wireframe: true });
let mesh = new THREE.Mesh(geometry, material);
mesh.rotation.y = Math.PI / 2;
scene.add(mesh);
// 将饼图划分为扇形
let offset = 0;
for(let i=0; i<data.length; i++) {
let angle = angles[i];
let geometry = new THREE.Geometry();
geometry.vertices.push(new THREE.Vector3(0,0,0));
for(let j=0; j<=30; j++) {
let a = offset + (angle/30)*j;
let x = Math.sin(a);
let y = Math.cos(a);
geometry.vertices.push(new THREE.Vector3(x,y,0));
}
let color = colors[i];
let material = new THREE.MeshBasicMaterial({ color: color, transparent: true, opacity: 0.8 });
let shape = new THREE.Line(geometry, material);
scene.add(shape);
offset += angle;
}
// 添加光源
let light = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(light);
// 渲染场景
function render() {
requestAnimationFrame(render);
mesh.rotation.y += 0.01; // 旋转饼图
renderer.render(scene, camera);
}
render();
</script>
</body>
</html>