D3.js 是一个强大的 JavaScript 库,用于数据可视化。它允许你将数据绑定到 DOM 元素,并使用数据来驱动 DOM 的更新。
快速开始
安装 D3.js 你可以通过 npm 或 yarn 来安装 D3.js:
npm install d3
或者
yarn add d3
创建基本图表 以下是一个简单的柱状图示例:
const data = [30, 50, 70, 90, 110]; const svg = d3.select("svg") .attr("width", 500) .attr("height", 300); const bar = svg.selectAll("rect") .data(data) .enter().append("rect") .attr("width", d => d * 10) .attr("height", 50) .attr("x", (d, i) => i * 60) .attr("y", d => 300 - d * 10);
学习更多 想要深入了解 D3.js,可以阅读 D3.js 官方文档