d3 JSON multiple line chart

后端 未结 2 486
不知归路
不知归路 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条回答
  •  猫巷女王i
    2021-01-01 07:13

    My guess is that the biggest problem here is that you're starting with an object, not an array. d3.data takes an array, not an object, so you might need your data set up like:

    [
      [{"val":10, "mon":10}, {"val":20, "mon":11}, {"val":40, "mon":12}, ...],
      [{"val":20, "mon":8}, {"val":20, "mon":9}, {"val":40, "mon":10}, ...],
      ...
    ]
    

    If you need to convert your current data to this format, look at d3.entries, which will give you an array with your key as well as your array of points:

    [
      {
        key: "2008",
        value: [{"val":10, "mon":10}, {"val":20, "mon":11}, {"val":40, "mon":12}, ...]
      },
      { 
        key: "2008",
        value: [{"val":20, "mon":8}, {"val":20, "mon":9}, {"val":40, "mon":10}, ...]
      },
      ...
    ]
    

    See this fiddle for a simple-case version of your graph, using the first data format above: http://jsfiddle.net/nrabinowitz/kmmyc/

提交回复
热议问题