我正在使用 d3 版本 5 来创建散点图。
以下代码运行顺利。
svg.selectAll(".point")
.data(data)
.enter().append("path")
.attr("class", "point")
.attr("d", d3.symbol().type(d3.symbolCross))
.attr("Fill",'steelblue')
.attr("transform", function(d) {return
"translate("+ x(d.x) + "," + y(d.y) + ")"; })
});
但是当我在任何函数中使用 d 时,它会破坏显示。即使我使用几乎最简单的功能。
svg.selectAll(".point")
.data(data)
.enter().append("path")
.attr("class", "point")
.attr("d", function(d) { return
"d3.symbol().type(d3.symbolCross)";}
.attr("Fill",'steelblue')
.attr("transform", function(d) {
return "translate("
+ x(d.x) + "," + y(d.y) + ")"; })
});
实际上,错误不是因为使用了d。问题完全在于没有正确关闭该功能。确保正确打开和关闭大括号。请尝试此。
svg.selectAll(".point")
.data(data)
.enter().append("path")
.attr("class", "point")
.attr("d", d3.symbol().type(d3.symbolCross))
.attr("Fill",'steelblue')
.attr("transform", (d) => "translate(" + d.x + "," + d.y + ")");