为可堆叠和嵌套图表形成d3的json数据

时间:2017-04-28 12:43:32

标签: javascript d3.js

我的json数据看起来像这样:

[{"DateText":"Apr 2016","Agent":"wade","Talk":2},
{"DateText":"Apr 2016","Agent":"winop4","Talk":0},
{"DateText":"Apr 2016","Agent":"amy","Talk":2},
{"DateText":"Apr 2016","Agent":"GARY","Talk":0},
{"DateText":"Apr 2016","Agent":"kmahoney","Talk":0}]

我需要它看起来像d3:

["DateText":"Apr 2016", "Agent":"wade","Talk":2}, "Agent":"winop4","Talk":0}, 
"Agent":"amy","Talk":2}, "Agent":"GARY","Talk":0}, 
"Agent":"kmahoney","Talk":0}
"DateText":"May 2016", "Agent":"wade","Talk":5}, 
"Agent":"winop4","Talk":200}, "Agent":"amy","Talk":400}, 
"Agent":"GARY","Talk":900}, "Agent":"kmahoney","Talk":3}]

我想在JavaScript中甚至在d3中这样做。任何人都可以提供这样做的好方法的例子吗?

2 个答案:

答案 0 :(得分:0)

我认为您想要创建主迭代器的日期,然后才能访问这些代理。

[{
    "DateText": "Apr 2016",
    [{
      "Agent": "wade",
      "Talk": 2
    }, {
      "Agent": "winop4",
      "Talk": 0
    }, {
      "Agent": "amy",
      "Talk": 2
    }, {
      "Agent": "GARY",
      "Talk": 0
    }, {
      "Agent": "kmahoney",
      "Talk": 0
    }]
  }, {
    "DateText": "May 2016",
    [{
        "Agent": "wade",
        "Talk": 5
      }, {
        "Agent": "winop4",
        "Talk": 200
      }, {
        "Agent": "amy",
        "Talk": 400
      }, {
        "Agent": "GARY",
        "Talk": 900
      }, {
        "Agent": "kmahoney",
        "Talk": 3
      }
    }
  ]
}]

答案 1 :(得分:0)

我是这样做的,它完成了我需要的大部分内容而没有'凌乱的'sql。

<html> 
   <div> 
      <h1>MainTitle <span id="h1span">SubTitle </span> </h1> 
   </div> 
</html>

代理的密钥位于字典的最后一个密钥中。