【发布时间】:2018-04-19 19:35:00
【问题描述】:
我正在尝试使用 d3.js v4 获取更新模式以在饼图上工作。从下拉菜单中选择时,数据会发生变化。
主要是我希望能够使用正确的更新模式,以便我可以 .exit() .enter() 并更新数据。我还希望能够使用他在 Bostock 的一个示例中使用的 arcTween() 函数。
我正在尝试实现this example by HarryStevens on b.locks。
目前我收到以下错误,
饼不是函数
从第 140 行开始,
.data(pie(category_count),
并在下面的代码中用星号标记。
这是代码的一部分,
// Generate an array object on categories as a category
var category_count = d3.nest()
.key(function(d) {
return d.category;
})
.rollup(function(leaves) {
return leaves.length;
})
.entries(data);
// console.log(category_count);
var pie = d3.pie()
.padAngle(.02)
.sort(null)
.value(function(d) {
return d.value;
})
(category_count);
function arcTween(a) {
console.log(this._current);
var i = d3.interpolate(this._current, a);
this._current = i(0);
return function(t) {
return arc(i(t));
};
}
var arc = d3.arc()
.outerRadius(radius - 10)
.innerRadius(radius / 2);
var labelArc = d3.arc()
.outerRadius(radius - 10)
.innerRadius(radius - 100);
var svgPie = d3.select("#pie")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + (margin.left + radius) + "," + (margin.top + radius) + ")"); //center of pie
var arcs = svgPie.selectAll("arc")
******** .data(pie(category_count), ********
function(d) {
return d.data.key
});
arcs
.transition()
.duration(4000)
.attrTween("d", arcTween);
// EXIT old elements not present in new data
arcs.enter()
.append("path")
.attr("class", "arc exit")
.style("fill", function(d, i) {
return color[i];
})
.attr("d", arc)
.each(function(d) {
this._current = d;
});
完整代码在github上here。
任何帮助将不胜感激。
【问题讨论】:
-
为什么
category_count在函数声明时被应用到函数上?我不确定它会返回一个实际的函数。 -
是的,@RyanMorton 说的!您正在将 pie 设置为 d3.pie()(category_count) 的返回