d3 JSON multiple line chart

后端 未结 2 484
不知归路
不知归路 2021-01-01 06:37

I\'m trying to make a plot with multiple lines on it from a JSON blob that looks like:

{\"2007\": [{\"val\":10, \"mon\":10}, {\"val\":20, \"mon\":11}, {\"va         


        
2条回答
  •  遥遥无期
    2021-01-01 07:14

    Here's what worked out in case anyone else runs across this problem. The trick is to pass a function that returns the values in the associative array in the "d" attribute of the path element.

      entries = d3.entries(data);
    
      var colors = d3.scale.category20()
        .domain(d3.keys(data));
    
      var line = d3.svg.line()
        .interpolate("basis")
        .x(function(d) { return x(d.month) })
        .y(function(d) { return y(d.value) });
    
      svg1.selectAll(".line")
        .data(entries)
      .enter().append("path")
        .attr("class", "line")
        // function(d), not just line function 
        .attr("d", function(d){ return  line(d.value); })
        .attr("stroke", function(d) { return colors(d.key) });
    

    Some help from this answer too: Using nested data with javascript D3 problem

提交回复
热议问题