Drawing Multiple Lines in D3.js

后端 未结 1 1241
悲哀的现实
悲哀的现实 2020-11-28 22:07

Up until now, I\'ve been using loops to add line elements to a D3 visualization, but this doesn\'t seem in the spirit of the API.

Let\'s say I have got some data, <

相关标签:
1条回答
  • 2020-11-28 22:44

    Yes. First I would restructure your data for easier iteration, like this:

    var series = [
      [{time: 1, value: 2}, {time: 2, value: 4}, {time: 3, value: 8}],
      [{time: 1, value: 5}, {time: 2, value: 9}, {time: 3, value: 12}],
      [{time: 1, value: 3}, {time: 2, value: 2}, {time: 3, value: 2}],
      [{time: 1, value: 2}, {time: 2, value: 4}, {time: 3, value: 15}]
    ];
    

    Now you need just a single generic line:

    var line = d3.svg.line()
        .interpolate("basis")
        .x(function(d) { return x(d.time); })
        .y(function(d) { return y(d.value); });
    

    And, you can then add all of the path elements in one go:

    group.selectAll(".line")
        .data(series)
      .enter().append("path")
        .attr("class", "line")
        .attr("d", line);
    

    If you want to make the data structure format smaller, you could also extract the times into a separate array, and then use a 2D array for the values. That would look like this:

    var times = [1, 2, 3];
    
    var values = [
      [2, 4, 8],
      [5, 9, 12],
      [3, 2, 2],
      [2, 4, 15]
    ];
    

    Since the matrix doesn't include the time value, you need to look it up from the x-accessor of the line generator. On the other hand, the y-accessor is simplified since you can pass the matrix value directly to the y-scale:

    var line = d3.svg.line()
        .interpolate("basis")
        .x(function(d, i) { return x(times[i]); })
        .y(y);
    

    Creating the elements stays the same:

    group.selectAll(".line")
        .data(values)
      .enter().append("path")
        .attr("class", "line")
        .attr("d", line);
    
    0 讨论(0)
提交回复
热议问题