我正在尝试向d3.js强制有向图添加图例。我的图表没有图例也可以正常工作。我正在按照https://gist.github.com/ZJONSSON/3918369上的说明进行操作,这是修改代码的部分:
const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", 7)
.attr("fill", color )
.attr("data-legend",function(d) { return d.lineage}) // XXX: added for legend
.call(drag(simulation));
node.append("title")
.text(d => d.id);
// added this block
legend = svg.append("g")
.attr("class","legend")
.attr("transform","translate(50,30)")
.style("font-size","12px")
.call(d3.legend)
// end add block
一旦我在// added this block
之间添加了零件,就会收到错误chart = TypeError: Cannot read property 'apply' of undefined
。我通过每个节点的属性沿袭为节点着色。
我在这里想念什么?