简体   繁体   English

d3将数据值映射到多个饼图

[英]d3 map data values to multiple pie charts

I'm trying to create a pie chart for each value in the details array (see json file: https://dl.dropboxusercontent.com/u/23726217/timeline.json ). 我正在尝试为details数组中的每个值创建一个饼图(请参阅json文件: https : //dl.dropboxusercontent.com/u/23726217/timeline.json )。 I'm having a difficult time mapping the data correctly. 我很难正确地映射数据。

I used this as an example although my version is quite a bit different - this generates multiple pies on a page via an array: http://bl.ocks.org/mbostock/1305337 我以这个为例,尽管我的版本有很大不同-这会通过数组在页面上生成多个py: http : //bl.ocks.org/mbostock/1305337

This is what I currently have set up although it is broken right now because the values are not mapping correctly: http://jsfiddle.net/featherita/k8G5q/1/ 这是我目前设置的,尽管现在由于值映射不正确而被破坏了: http : //jsfiddle.net/featherita/k8G5q/1/

This seems to be the part that is broken: 这似乎是损坏的部分:

var pie = d3.layout.pie()
   .value(function (d) { return +d.pcArray; })
   .sort(null);

This is where the pie is called and then there is an area after this that appends a path to each g created in this one for the color application: 这是调用饼图的地方,然后在此之后有一个区域,向该颜色应用程序中为此创建的每个g附加一个路径:

var g = svg.selectAll("g")
    //.data(pie(data.details))
    .data(function (d) { return pie(d.values); })
    .enter().append("svg:g");

Let me know if I can provide any further clarification! 让我知道是否可以提供进一步的说明!

Take a look at this fiddle: http://jsfiddle.net/peterburrell/cvLHG/ 看看这个小提琴: http : //jsfiddle.net/peterburrell/cvLHG/

I tried to provide a simplified example using your data to create a pie chart for each object in the details array. 我试图提供一个简化的示例,使用您的数据为details数组中的每个对象创建一个饼图。 These are the key points, I think ... 这些是关键点,我认为...

var svg = d3.select("body")
    .selectAll("svg")
    .data(data.details)
    .enter().append("svg")
    .attr("width", width)
    .attr("height", height)
    .append("g")
    .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

This creates a new svg element for each object in data.details . 这会为data.details每个对象创建一个新的svg元素。 It looked like your example was appending svg elements to other svg elements. 看起来您的示例将svg元素附加到其他svg元素。

var g = svg.selectAll("g")
    .data(function (d) { return pie(d.pcArray); })
    .enter().append("g");

g.append("path")
    .attr("d", arc)
    .style("fill", function (d) { return color(d.value); })
    .append("title")
    .text(function (d) { return d.data + "%"; });

Then, for each data-bound object we draw the pie/donut chart paths using the data from pcArray . 然后,对于每个数据绑定对象,我们使用来自pcArray的数据绘制饼图/甜甜圈图路径。

Does that help at all? 这些帮助有用?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM