最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - D3: Create a grouped bar chart from json objects - Stack Overflow

programmeradmin4浏览0评论

I have some data like this:

[
 { id: 12, value1: "2.92", value2: "4.22", value3: "3.69" }
, 
 { id: 23, value1: "2.69", value2: "4.24", value3: "3.77" }
,
  ....
]

I want to create a horizontal grouped bar chart, so that there are 3 groups of bars, first all value1 bars (labeled as Value1), followed by all value2 bars and finally all value3 bars.

How can I do this - keeping in mind that the data will be updated dynamically in the future, so new data objects will be added and others will be removed. Guess I could use id as a key.

I have some data like this:

[
 { id: 12, value1: "2.92", value2: "4.22", value3: "3.69" }
, 
 { id: 23, value1: "2.69", value2: "4.24", value3: "3.77" }
,
  ....
]

I want to create a horizontal grouped bar chart, so that there are 3 groups of bars, first all value1 bars (labeled as Value1), followed by all value2 bars and finally all value3 bars.

How can I do this - keeping in mind that the data will be updated dynamically in the future, so new data objects will be added and others will be removed. Guess I could use id as a key.

Share Improve this question edited Sep 12, 2012 at 12:51 paxRoman 2,0623 gold badges19 silver badges32 bronze badges asked Aug 29, 2012 at 14:20 HokaschaHokascha 2,2892 gold badges28 silver badges45 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

First, supply or convert the data to an ordinary array or arrays eg

data = [ [ 2.92, 4.22, 3.69], [2.69, 4.24, 3.77] ]

Now you can use d3.transpose to pivot the data so you get

  var tdata = d3.transpose(data);

gives you

   [ [2.92, 2.69], [4.22, 4.24], [3.69, 3.77]]

then here is a group bar from iaindillingham to use as a model (I've fixed his version to use the latest d3 library). See it working here: http://bl.ocks/3532324

发布评论

评论列表(0)

  1. 暂无评论