基于Three.js 绘制折线图, 请给出完整代码
时间: 2024-03-27 10:35:40 浏览: 131
折线图代码
以下是一个基于Three.js绘制折线图的完整代码示例:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Three.js Line Chart</title>
<style>
body {
margin: 0;
overflow: hidden;
}
canvas {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
const data = [
{ x: 0, y: 0 },
{ x: 1, y: 1 },
{ x: 2, y: 2 },
{ x: 3, y: 1 },
{ x: 4, y: 3 },
{ x: 5, y: 2 },
{ x: 6, y: 4 },
{ x: 7, y: 3 },
{ x: 8, y: 5 },
{ x: 9, y: 4 }
];
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const material = new THREE.LineBasicMaterial({ color: 0x0000ff });
const geometry = new THREE.Geometry();
for (let i = 0; i < data.length; i++) {
geometry.vertices.push(new THREE.Vector3(data[i].x, data[i].y, 0));
}
const line = new THREE.Line(geometry, material);
scene.add(line);
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```
这个例子中,我们创建了一个包含10个点的数据集,然后使用Three.js创建了一个二维折线图。我们使用了`THREE.Line`对象,并将其添加到了场景中。在动画循环中,我们不断地渲染场景。
阅读全文