D3 pie legend

本文介绍了如何在Node.js环境中安装Rollup模块打包器,并提供了具体的安装命令。通过npm install rollup,开发者可以轻松地将Rollup集成到项目中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

rollup 在本地装好node后用npm install rollup就可以安装好了

http://plnkr.co/edit/KxIGVU1YjWgxDNLSSxCh?p=preview


https://stackoverflow.com/questions/20675617/how-to-add-legend-to-a-pie-chart-using-d3js-and-how-to-centralise-the-pie-chart/22261120#22261120


https://vida.io/documents/gSvr8dAH23eirKQDp


https://stackoverflow.com/questions/26506210/legend-toggling-for-d3-js-pie-chart


https://stackoverflow.com/questions/33890012/how-to-add-legend-to-a-pie-chart-d3-js


https://stackoverflow.com/questions/32298837/how-to-add-a-nice-legend-to-a-d3-pie-chart

好的,以下是一个利用D3设计交互式图表的实战示例,供您参考: ```javascript // 定义数据 var dataset = [ { name: "A", value: 10 }, { name: "B", value: 20 }, { name: "C", value: 30 }, { name: "D", value: 40 }, { name: "E", value: 50 } ]; // 创建SVG画布 var svg = d3.select("body") .append("svg") .attr("width", 500) .attr("height", 300); // 创建饼图 var pie = d3.pie() .sort(null) .value(function(d) { return d.value; }); var path = d3.arc() .outerRadius(100) .innerRadius(0); var label = d3.arc() .outerRadius(120) .innerRadius(120); var arc = svg.selectAll(".arc") .data(pie(dataset)) .enter() .append("g") .attr("class", "arc") .attr("transform", "translate(200,150)"); arc.append("path") .attr("d", path) .attr("fill", function(d) { return d3.schemeCategory10[d.index]; }) // 添加交互 .on("mouseover", function(d) { d3.select(this) .transition() .duration(500) .attr("d", d3.arc() .outerRadius(120) .innerRadius(0) ); }) .on("mouseout", function(d) { d3.select(this) .transition() .duration(500) .attr("d", path); }); arc.append("text") .attr("transform", function(d) { return "translate(" + label.centroid(d) + ")"; }) .attr("text-anchor", "middle") .text(function(d) { return d.data.name; }); // 添加图例 var legend = svg.selectAll(".legend") .data(dataset) .enter() .append("g") .attr("class", "legend") .attr("transform", function(d, i) { return "translate(320," + (i * 20 + 100) + ")"; }); legend.append("rect") .attr("x", 0) .attr("y", 0) .attr("width", 18) .attr("height", 18) .attr("fill", function(d, i) { return d3.schemeCategory10[i]; }); legend.append("text") .attr("x", 24) .attr("y", 9) .attr("dy", ".35em") .text(function(d) { return d.name; }); ``` 这个示例创建了一个饼图,使用了D3pie、arc和centroid等方法。每个扇形上方都添加了一个文本标签,显示对应的名称。同时,每个扇形都添加了交互事件,当鼠标悬停时变成大圆环,鼠标移开时恢复为正常大小。另外,还添加了一个图例,显示对应颜色和名称。 希望这个示例能够帮助您了解如何使用D3设计交互式图表。您可以根据实际需求修改代码,并尝试添加更多的交互事件和图表类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值