Home > Software engineering >  Data refresh issue with D3 burnup chart
Data refresh issue with D3 burnup chart

Time:02-14

I'm building a "burnup" d3 line chart attempting to forecast trends based on scope and historic data.

A dropdown box selects the data to be displayed, ideally transitioning between lines, but I'm having troubles clearing the previous data displayed, and instead the new lines are written over the existing lines.

Link to the jsfiddle: https://jsfiddle.net/dgf1vts8/

Currently doing it this way (line 329):

svg.append("path")
      .datum(selectedData)
      .attr("class", "line")
      .attr("d", line);

other approach I've tried and did not work (line 318)

var lines = svg.selectAll(".line").data(selectedData).attr("class","line");
    lines.transition().duration(500).attr("d",line);
    lines.enter()
      .append("path")
      .attr("class","line")
      .attr("d",line);
    lines.exit()
      .remove();

Any guidance with this would be much appreciated

CodePudding user response:

Since you only append new object in your update function, every time that function is invoked a new line is added to the chart.

The easiest workaround would be to just remove all path objects before you add the new ones.

svg.selectAll("path").remove();

However, the enter-update-exit logic for this would be

  var lines = svg.selectAll(".line").data(selectedGroup);
  lines.enter() // enter any new data
    .append("path")
    .attr("class", "line")
    .merge(lines)
    .datum(selectedData)
    .attr("d", line);
  lines.exit() // exit
    .remove();

The logic for lines is different as for e. g. points or bars, where you need to create an object for every data point of the time series. For lines you only have to create one path object. This is why the data binding to the selection

svg.selectAll(".line")

should be the label of the line. If you work with multiple lines, it is often best to nest the array.

If you start a new project you should probably use the current version d3v7 and not d3v3, since there have been major changes which break compatibility. I reduced your fiddle and changed all the parts to make it work with d3v7: https://jsfiddle.net/esd4kofr/1/

  • Related