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 badges1 Answer
Reset to default 4First, 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