Javascript D3js v3到v4迁移合并
当我尝试将d3js图从版本3迁移到版本4时,我遇到了一些问题。我已经解决了很多问题,但我不明白“合并”是如何工作的。在此代码中,V4中未设置数据标题属性,并且contextmenu不起作用。 有什么问题吗?我不明白合并是如何工作的。有人能给我解释一下如何修复这段代码,为什么我要用这种方式修复它,因为我有更多的图表要修复Javascript D3js v3到v4迁移合并,javascript,d3.js,Javascript,D3.js,当我尝试将d3js图从版本3迁移到版本4时,我遇到了一些问题。我已经解决了很多问题,但我不明白“合并”是如何工作的。在此代码中,V4中未设置数据标题属性,并且contextmenu不起作用。 有什么问题吗?我不明白合并是如何工作的。有人能给我解释一下如何修复这段代码,为什么我要用这种方式修复它,因为我有更多的图表要修复 var slice = self.svg.select(".slices").selectAll("path.slice") .data(pie(new_
var slice = self.svg.select(".slices").selectAll("path.slice")
.data(pie(new_node_data), key);
slice.enter()
.insert("path")
.attr("class", "slice").attr("fill", function(d2) { return color(d2.data.name);} )
.merge(slice) //<-- merge back in update selection
.transition().duration(1000).attrTween("d", tweenIn);
slice.attr('data-title',function(d){
return d.data.period + ' ' + d.data.name;
})
slice.on("contextmenu", function(d,i){
console.log(d.data);
self.context_menu(d.data, i, false);
});
self.attach_graph_items_click_event_handler(slice, false, true);
var slice=self.svg.select(“.slices”).selectAll(“path.slice”)
.数据(饼图(新节点数据),键);
slice.enter()
.插入(“路径”)
.attr(“class”,“slice”).attr(“fill”,函数(d2){返回颜色(d2.data.name);})
.merge(slice)/您缺少一些简单的内容,合并后您不会将组合的更新+输入返回到变量(您缺少slice=
):
slice = slice.enter() //<-- SAVE IT TO A VARIABLE
.insert("path")
.attr("class", "slice").attr("fill", function(d2) { return color(d2.data.name);} )
.merge(slice)
....
// this is your update selection
// slice is a selection of all the things being updated
// in your collection path.slice
var slice = self.svg.select(".slices").selectAll("path.slice")
.data(pie(new_node_data), key);
// .enter returns the enter selection
// all the things that are being added
slice = slice.enter()
.insert("path")
.attr("class", "slice").attr("fill", function(d2) { return color(d2.data.name);} )
// this merges the update slice with the enter slice
.merge(slice)
// this is now operating on enter + update
.transition().duration(1000).attrTween("d", tweenIn);