gpt4 book ai didi

d3.js - 使用 D3js v4 的多环 donut chart

转载 作者:行者123 更新时间:2023-12-04 16:11:50 25 4
gpt4 key购买 nike

我正在尝试使用多环工作来获得这个 donut chart 。这是使用 d3js v4。这是另一篇在 v3( multi-ring pie chart using d3js ) 中工作的帖子的延续。创建的路径总是显示 M0,0Z .谁能让我知道这是哪里出了问题。我也收到 Expected lenght NaN在控制台中。

var dataset = [{
"name": "Population Quater",
"code": "POP_QUATER",
"parent": "POP_BY_QUAT",
"children": [{
"name": "POP_CYQ1",
"code": "POP_CYQ1",
"parent": "POP_QUATER",
"value": "6772",
"label": "CYQ1",
"children": []
}, {
"name": "POP_CYQ2",
"code": "POP_CYQ2",
"parent": "POP_QUATER",
"value": "6716",
"label": "CYQ2",
"children": []
}, {
"name": "POP_CYQ3",
"code": "POP_CYQ3",
"parent": "POP_QUATER",
"value": "6714",
"label": "CYQ3",
"children": []
}, {
"name": "POP_CYQ4",
"code": "POP_CYQ4",
"parent": "POP_QUATER",
"value": "6703",
"label": "CYQ4",
"children": []
}, {
"name": "POP_LYQ1",
"code": "POP_LYQ1",
"parent": "POP_QUATER",
"value": "6721",
"label": "LYQ1",
"children": []
}, {
"name": "POP_LYQ2",
"code": "POP_LYQ2",
"parent": "POP_QUATER",
"value": "6671",
"label": "LYQ2",
"children": []
}, {
"name": "POP_LYQ3",
"code": "POP_LYQ3",
"parent": "POP_QUATER",
"value": "6708",
"label": "LYQ3",
"children": []
}, {
"name": "POP_LYQ4",
"code": "POP_LYQ4",
"parent": "POP_QUATER",
"value": "6734",
"label": "LYQ4",
"children": []
}]
}, {
"name": "Transient Pop",
"code": "TRANSIENT_POP",
"parent": "POP_BY_QUAT",
"label": "Transient Pop",
"children": [{
"name": "TRANSIENT_LYQ1",
"code": "TRANSIENT_LYQ1",
"parent": "TRANSIENT_POP",
"value": "54",
"label": "LYQ1",
"children": []
}, {
"name": "TRANSIENT_LYQ2",
"code": "TRANSIENT_LYQ2",
"parent": "TRANSIENT_POP",
"value": "86",
"label": "LYQ2",
"children": []
}, {
"name": "TRANSIENT_LYQ3",
"code": "TRANSIENT_LYQ3",
"parent": "TRANSIENT_POP",
"value": "219",
"label": "LYQ3",
"children": []
}, {
"name": "TRANSIENT_LYQ4",
"code": "TRANSIENT_LYQ4",
"parent": "TRANSIENT_POP",
"value": "191",
"label": "LYQ4",
"children": []
}, {
"name": "TRANSIENT_CYQ1",
"code": "TRANSIENT_CYQ1",
"parent": "TRANSIENT_POP",
"value": "52",
"label": "CYQ1",
"children": []
}, {
"name": "TRANSIENT_CYQ2",
"code": "TRANSIENT_CYQ2",
"parent": "TRANSIENT_POP",
"value": "91",
"label": "CYQ2",
"children": []
}, {
"name": "TRANSIENT_CYQ3",
"code": "TRANSIENT_CYQ3",
"parent": "TRANSIENT_POP",
"value": "222",
"label": "CYQ3",
"children": []
}, {
"name": "TRANSIENT_CYQ4",
"code": "TRANSIENT_CYQ4",
"parent": "TRANSIENT_POP",
"value": "186",
"label": "CYQ4",
"children": []
}]
}, {
"name": "Seasonal Pop",
"code": "SEASONAL_POP",
"parent": "POP_BY_QUAT",
"label": "Seasonal Pop",
"children": [{
"name": "SEASONAL_LYQ1",
"code": "SEASONAL_LYQ1",
"parent": "SEASONAL_POP",
"value": "2",
"label": "LYQ1",
"children": []
}, {
"name": "SEASONAL_LYQ2",
"code": "SEASONAL_LYQ2",
"parent": "SEASONAL_POP",
"value": "24",
"label": "LYQ2",
"children": []
}, {
"name": "SEASONAL_LYQ3",
"code": "SEASONAL_LYQ3",
"parent": "SEASONAL_POP",
"value": "152",
"label": "LYQ3",
"children": []
}, {
"name": "SEASONAL_LYQ4",
"code": "SEASONAL_LYQ4",
"parent": "SEASONAL_POP",
"value": "55",
"label": "LYQ4",
"children": []
}, {
"name": "SEASONAL_CYQ1",
"code": "SEASONAL_CYQ1",
"parent": "SEASONAL_POP",
"value": "2",
"label": "CYQ1",
"children": []
}, {
"name": "SEASONAL_CYQ2",
"code": "SEASONAL_CYQ2",
"parent": "SEASONAL_POP",
"value": "22",
"label": "CYQ2",
"children": []
}, {
"name": "SEASONAL_CYQ3",
"code": "SEASONAL_CYQ3",
"parent": "SEASONAL_POP",
"value": "161",
"label": "CYQ3",
"children": []
}, {
"name": "SEASONAL_CYQ4",
"code": "SEASONAL_CYQ4",
"parent": "SEASONAL_POP",
"value": "55",
"label": "CYQ4",
"children": []
}]
}];

var width = 460,
height = 300,
cwidth = 25;


var color = d3.scaleOrdinal(d3.schemeCategory20);

var pie = d3.pie()
.sort(null).value(function(d) {
return d.value; //since score is the parameter for the pie
});

var arc = d3.arc();

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

var gs = svg.selectAll("g")
.data(dataset)
.enter()
.append("g");
var path = gs.selectAll("path")
.data(function(d) {
return pie(d.children);
})
.enter().append("path")
.attr("fill", function(d, i) {
return color(i);
})
.attr("d", function(d, i, j) {
return arc.innerRadius(10 + cwidth * j).outerRadius(cwidth * (j + 1))(d);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.11.0/d3.min.js"></script>

最佳答案

我重写了一点你的代码。检查,现在它可以工作了。注意我用属性 parentIndex 扩展数据对象当我存储当前数据项的索引时。在 d3 版本 3 中,您可以获得父数据项的索引作为第三个参数 function(d,i,j) (本例中为 j),但在 d3 版本 4 中,第三个参数不是索引。

var path = gs.selectAll("path")
.data(function(d, i) {
return pie(d.children).map(function(payload) { // <-- !!!
return {
payload: payload,
parentIndex: i
}
})
})
.enter().append("path")
.attr("fill", function(d, i) {
return color(i);
})
.attr("d", function(d, i) {
return arc
.innerRadius(10 + cwidth * d.parentIndex) //<-- !!!
.outerRadius(cwidth * (d.parentIndex + 1))(d.payload); //<-- !!!
});

var dataset = [{
"name": "Population Quater",
"code": "POP_QUATER",
"parent": "POP_BY_QUAT",
"children": [{
"name": "POP_CYQ1",
"code": "POP_CYQ1",
"parent": "POP_QUATER",
"value": "6772",
"label": "CYQ1",
"children": []
}, {
"name": "POP_CYQ2",
"code": "POP_CYQ2",
"parent": "POP_QUATER",
"value": "6716",
"label": "CYQ2",
"children": []
}, {
"name": "POP_CYQ3",
"code": "POP_CYQ3",
"parent": "POP_QUATER",
"value": "6714",
"label": "CYQ3",
"children": []
}, {
"name": "POP_CYQ4",
"code": "POP_CYQ4",
"parent": "POP_QUATER",
"value": "6703",
"label": "CYQ4",
"children": []
}, {
"name": "POP_LYQ1",
"code": "POP_LYQ1",
"parent": "POP_QUATER",
"value": "6721",
"label": "LYQ1",
"children": []
}, {
"name": "POP_LYQ2",
"code": "POP_LYQ2",
"parent": "POP_QUATER",
"value": "6671",
"label": "LYQ2",
"children": []
}, {
"name": "POP_LYQ3",
"code": "POP_LYQ3",
"parent": "POP_QUATER",
"value": "6708",
"label": "LYQ3",
"children": []
}, {
"name": "POP_LYQ4",
"code": "POP_LYQ4",
"parent": "POP_QUATER",
"value": "6734",
"label": "LYQ4",
"children": []
}]
}, {
"name": "Transient Pop",
"code": "TRANSIENT_POP",
"parent": "POP_BY_QUAT",
"label": "Transient Pop",
"children": [{
"name": "TRANSIENT_LYQ1",
"code": "TRANSIENT_LYQ1",
"parent": "TRANSIENT_POP",
"value": "54",
"label": "LYQ1",
"children": []
}, {
"name": "TRANSIENT_LYQ2",
"code": "TRANSIENT_LYQ2",
"parent": "TRANSIENT_POP",
"value": "86",
"label": "LYQ2",
"children": []
}, {
"name": "TRANSIENT_LYQ3",
"code": "TRANSIENT_LYQ3",
"parent": "TRANSIENT_POP",
"value": "219",
"label": "LYQ3",
"children": []
}, {
"name": "TRANSIENT_LYQ4",
"code": "TRANSIENT_LYQ4",
"parent": "TRANSIENT_POP",
"value": "191",
"label": "LYQ4",
"children": []
}, {
"name": "TRANSIENT_CYQ1",
"code": "TRANSIENT_CYQ1",
"parent": "TRANSIENT_POP",
"value": "52",
"label": "CYQ1",
"children": []
}, {
"name": "TRANSIENT_CYQ2",
"code": "TRANSIENT_CYQ2",
"parent": "TRANSIENT_POP",
"value": "91",
"label": "CYQ2",
"children": []
}, {
"name": "TRANSIENT_CYQ3",
"code": "TRANSIENT_CYQ3",
"parent": "TRANSIENT_POP",
"value": "222",
"label": "CYQ3",
"children": []
}, {
"name": "TRANSIENT_CYQ4",
"code": "TRANSIENT_CYQ4",
"parent": "TRANSIENT_POP",
"value": "186",
"label": "CYQ4",
"children": []
}]
}, {
"name": "Seasonal Pop",
"code": "SEASONAL_POP",
"parent": "POP_BY_QUAT",
"label": "Seasonal Pop",
"children": [{
"name": "SEASONAL_LYQ1",
"code": "SEASONAL_LYQ1",
"parent": "SEASONAL_POP",
"value": "2",
"label": "LYQ1",
"children": []
}, {
"name": "SEASONAL_LYQ2",
"code": "SEASONAL_LYQ2",
"parent": "SEASONAL_POP",
"value": "24",
"label": "LYQ2",
"children": []
}, {
"name": "SEASONAL_LYQ3",
"code": "SEASONAL_LYQ3",
"parent": "SEASONAL_POP",
"value": "152",
"label": "LYQ3",
"children": []
}, {
"name": "SEASONAL_LYQ4",
"code": "SEASONAL_LYQ4",
"parent": "SEASONAL_POP",
"value": "55",
"label": "LYQ4",
"children": []
}, {
"name": "SEASONAL_CYQ1",
"code": "SEASONAL_CYQ1",
"parent": "SEASONAL_POP",
"value": "2",
"label": "CYQ1",
"children": []
}, {
"name": "SEASONAL_CYQ2",
"code": "SEASONAL_CYQ2",
"parent": "SEASONAL_POP",
"value": "22",
"label": "CYQ2",
"children": []
}, {
"name": "SEASONAL_CYQ3",
"code": "SEASONAL_CYQ3",
"parent": "SEASONAL_POP",
"value": "161",
"label": "CYQ3",
"children": []
}, {
"name": "SEASONAL_CYQ4",
"code": "SEASONAL_CYQ4",
"parent": "SEASONAL_POP",
"value": "55",
"label": "CYQ4",
"children": []
}]
}];

var width = 460,
height = 300,
cwidth = 25;


var color = d3.scaleOrdinal(d3.schemeCategory20);

var pie = d3.pie()
.sort(null).value(function(d) {
return d.value; //since score is the parameter for the pie
});

var arc = d3.arc();

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

var gs = svg.selectAll("g")
.data(dataset)
.enter()
.append("g");
var path = gs.selectAll("path")
.data(function(d, i) {
return pie(d.children).map(function(payload) {
return {
payload: payload,
parentIndex: i
}
})
})
.enter().append("path")
.attr("fill", function(d, i) {
return color(i);
})
.attr("d", function(d, i, j) {
return arc.innerRadius(10 + cwidth * d.parentIndex).outerRadius(cwidth * (d.parentIndex + 1))(d.payload);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.11.0/d3.min.js"></script>

关于d3.js - 使用 D3js v4 的多环 donut chart ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46837117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com