Javascript 从图表中删除线条并绘制新线条

Javascript 从图表中删除线条并绘制新线条,javascript,d3.js,Javascript,D3.js,我想画出最小均方误差函数,并通过点击该图将该线更新到添加的每个新点。到目前为止,一切都很顺利,但我似乎无法去掉旧的界限,只画新的界限 这是更新直线的代码,而不是在别处更新的点 Graph.prototype.update = function() { var this_ = this; var data = d3.csv.parse(this.collectData()); var color = d3.scale.category10(); this.svg

我想画出最小均方误差函数,并通过点击该图将该线更新到添加的每个新点。到目前为止,一切都很顺利,但我似乎无法去掉旧的界限,只画新的界限

这是更新直线的代码,而不是在别处更新的点

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.

然后,添加包含新数据的行。

在添加新行之前,请删除所有现有行:

// remove existing lines
this.svg.selectAll('lines').remove();

var lines = this.svg.selectAll('lines')
    .data(data);

// the rest of the code here.

然后,添加带有新数据的行。

我已将代码示例更改为您的建议。但是它仍然显示相同的行为。现在它工作了!我试着用一个例子来说明
obj.exit().remove()
应该删除旧的日期。似乎我理解错了什么。我已经将代码示例更改为您的建议。但是它仍然显示相同的行为。现在它工作了!我试着用一个例子来说明
obj.exit().remove()
应该删除旧的日期。好像我误解了什么。