简体   繁体   English

在d3中使用嵌套数据

[英]Consume nested data in d3

I am exploring d3. 我正在探索d3。 I am creating a bubble chart from very detailed and transactional csv data. 我正在根据非常详细的事务性csv数据创建气泡图。

I have used d3.nest() and d3.rollup() remove unnecessary columns of the data and end up with final nested data structure. 我已经使用d3.nest()和d3.rollup()删除了不必要的数据列,并最终得到了嵌套的数据结构。 But I am not sure how to consume nested data in d3. 但是我不确定如何在d3中使用嵌套数据。

Here is the sample of my flat data ( Some columns are removed to display it better here on the post) 这是我的平面数据的示例(一些列已删除,以便在帖子中更好地显示)


State,Date,Day,Month,Year,Dayweek,Crash Type,Number of Fatalities,Speed Limit 状态,日期,日期,月份,年份,星期几,崩溃类型,死亡人数,速度限制

VIC,01-Jan-2015,1,January,2015,Thursday,Multiple vehicle,1,100 VIC,2015年1月1日,1,2015年1月,星期四,多辆车,1,100

QLD,01-Jan-2015,1,January,2015,Thursday,Multiple vehicle,1,60 昆士兰州,2015年1月1日,1,2015年1月,星期四,多辆车,1,60

VIC,01-Jan-2015,1,January,2015,Thursday,Single vehicle,1,70 VIC,2015年1月1日,1,2015年1月,星期四,单车,1,70

WA,01-Jan-2015,1,January,2015,Thursday,Single vehicle,1,110 WA,2015年1月1日,2015年1月1日,星期四,单车,1,110

TAS,01-Jan-2015,1,January,2015,Thursday,Single vehicle,1,40 TAS,2015年1月1日,1,2015年1月,星期四,单车,1,40

SA,02-Jan-2015,2,January,2015,Friday,Multiple vehicle,1,60 SA,2015年1月2日,2015年1月2日,星期五,多辆车,1,60

QLD,03-Jan-2015,3,January,2015,Saturday,Multiple vehicle,1,100 昆士兰州,2015年1月3日,2015年1月3日,星期六,多辆车,1,100

NSW,04-Jan-2015,4,January,2015,Sunday,Single vehicle,1,100 新南威尔士州,2015年1月4日,4,2015年1月,星期日,单车,1,100

WA,04-Jan-2015,4,January,2015,Sunday,Multiple vehicle,1,70 WA,2015年1月4日,4,2015年1月,星期日,多辆车,1,70


I converted above csv data to nested data using .nest() and .rollup() function. 我使用.nest()和.rollup()函数将上述csv数据转换为嵌套数据。 Converted data sample is as below 转换后的数据样本如下

 Object { key: "VIC", // group by state values: [ { key: "January", // group by month values: [ { key: "40" // group by speed limit values: { accidents: 1 fatalities: 1 } }, {..}, {..} ] }, {..}, {..}, {..}, {..}, {..} ... ] 

My question is how can I consume data directly ? 我的问题是如何直接使用数据?

  1. Can I consume directly ? 我可以直接食用吗?
  2. Do I have to flatten this nested data to consume in d3 ? 我是否必须展平此嵌套数据以在d3中使用?

If possible please provide some example ... 如果可能,请提供一些示例...

Thanks 谢谢

I have figured it. 我已经知道了。 I could not answer anywhere. 我无法在任何地方回答。 People have posted half answers but no one has put a solution that show you a way to flatten nested data. 人们已经发布了一半的答案,但是没有人提出一种解决方案来向您展示一种扁平化嵌套数据的方法。

Q-1. Q-1。 Can I consume directly nested data? 我可以使用直接嵌套的数据吗?

A-1. A-1。 Yes it can be consumed nested data by d3. 是的,d3可以使用嵌套数据。 You have to add a <svg:g></svg:g> element for each level and that will hold the __d__ value for the next level. 您必须为每个级别添加一个<svg:g></svg:g>元素,该元素将保留下一级别的__d__值。 And at any level, if you want to access the parent node data var parentData = d3.select(this.parentNode).datum(); 并且在任何级别上,如果要访问父节点数据var parentData = d3.select(this.parentNode).datum();

Q-2. 问题2。 Do I have to flatten this data to consume in d3? 我是否必须展平此数据以在d3中使用? And how to flatten nested data to consume? 以及如何扁平化嵌套数据以使用?

A-2. A2。 This is much better approach, as it is one off operation, and all data available at each elements entered on SVG. 这是一种更好的方法,因为它是一次性操作,并且所有数据都可在SVG上输入的每个元素上获得。 Use .map() function to iterate though each level and flatten it. 使用.map()函数迭代每个级别并将其展平。

Exmaple 枫树

 var flatdata = []; data.map(function (d) { d.values.map(function (s) { var tdata = s.values.map(function (t) { return { state: d.key, month: s.key, speed: t.key, accidents: t.values.accidents, fatalities: t.values.fatalities } }) flatdata = flatdata.concat(tdata); }) }); 

I dont know that is the best solution but it is working perfectly fine. 我不知道这是最好的解决方案,但它工作得很好。

Hope this will be helpful. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM