d3带有enter-update-exit逻辑的折线图

Kol*_*urs 8 d3.js

今天我使用enter-update-exit逻辑创建了一个简单的条形图 - 一切正常.现在我将使用折线图进行相同的操作 - 图表的整个数据集可以随时更改,因此我将顺利更新图表.我找不到一个使用折线图和enter-update-exit逻辑的好例子(或者我看错了).目前我必须记住,如果第一次调用图表或更新数据(数据已更改) - 这是我的脏版本:

   var channelGroup = d3.select(".ch1");

// init. Line Chart 
    if (firstLoad) {
    channelGroup
        .append('path')
        .attr("class", "line")
        .style("stroke", channel.Color)
        .transition()
        .ease("linear")
        .duration(animChart / 2)
        .attr('d', line(channel.DataRows));
}
// update Line Chart    
else {
    channelGroup
        .selectAll("path")
        .data([channel.DataRows])
        .transition()
        .ease("linear")
        .duration(animChart / 2)
        .attr("d", function (d) { return line(d); });
}  
Run Code Online (Sandbox Code Playgroud)

我怎么能以一种好的方式实现这个目标?... thx!

Ame*_*aBR 10

你混合了两种不同的方法,一种是将数据绑定到线上,另一种只是将数据传递给线函数.任何一个都可以工作(只要你只有一行),但如果你想摆脱你的if/else结构,你仍然需要将处理输入/附加元素的语句与语句更新分开它.

选项1(不绑定数据,只需调用函数):

var linegraph = channelGroup.select('path');

if ( linegraph.empty() ) {
     //handle the entering data
     linegraph = channelGroup.append('path')
                      .attr("class", "line")
                      .style("stroke", channel.Color);
}

linegraph
        .transition()
        .ease("linear")
        .duration(animChart / 2)
        .attr('d', line(channel.DataRows));
Run Code Online (Sandbox Code Playgroud)

选项2(使用数据连接):

var linegraph = channelGroup.selectAll("path")
        .data([channel.DataRows]);

linegraph.enter().append('path')
                 .attr("class", "line");

linegraph
        .transition()
        .ease("linear")
        .duration(animChart / 2)
        .attr("d", line); //This is slightly more efficient than
                          //function (d) { return line(d); }
                          //and does the exact same thing.
                          //d3 sees that `line` is a function, and 
                          //therefore calls it with the data as the 
                          //first parameter.
Run Code Online (Sandbox Code Playgroud)

  • 谢谢 - 一直在拖网,这是第一个有意义的解释.非常重要的是要注意`channel.DataRows`在选项2中的方括号中! (5认同)
  • @RobinL 是的,这就是技巧:创建一个单元素数组来传递整个数据对象,以便它将绑定到单个“<path>”。 (2认同)