我想绘制最小均方误差函数,并将该线更新为通过单击该图添加的每个新点。到目前为止,一切正常,但我似乎无法删除旧线来仅绘制新线。
这是更新线的代码 - 而不是在其他地方更新的点。
Graph.prototype.update = function() {
var this_ = this;
var data = d3.csv.parse(this.collectData());
var color = d3.scale.category10();
this.svg.selectAll("lines").remove();
lines = this.svg.selectAll("lines")
.data(data)
.attr("class", "update");
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "x"; }));
var lineData = color.domain().map(function(name) {
return {
name: name,
values: data.map(function(d) {
return {x: d.x, y: +d[name]};
})
};
});
lines.enter().append("path")
.data(lineData)
.attr("class", "line")
.attr("d", function(d) { return this_.line(d.values); })
.style("stroke", function(d) { return color(d.name); });
};
这就是目前为止的样子:
最佳答案
在添加新行之前,删除所有现有行:
// remove existing lines
this.svg.selectAll('lines').remove();
var lines = this.svg.selectAll('lines')
.data(data);
// the rest of the code here.
然后,添加包含新数据的行。
关于javascript - 从图表中删除线条并绘制新的线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16714238/