D3:从json对象创建分组条形图

时间:2022-03-10 20:28:13

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.

我想创建一个水平分组条形图,这样就有3组条形图,首先是所有value1条形图(标记为Value1),然后是所有value2条形图,最后是所有value3条形图。

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.

我该怎么做 - 请记住,将来会动态更新数据,因此将添加新数据对象,并删除其他数据对象。猜猜我可以使用id作为密钥。

1 个解决方案

#1


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

现在,您可以使用d3.transpose来转动数据,以便获得

  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.org/3532324

然后这里是来自iaindillingham的团体酒吧用作模型(我已修复他的版本以使用最新的d3库)。看到它在这里工作:http://bl.ocks.org/3532324

#1


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

现在,您可以使用d3.transpose来转动数据,以便获得

  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.org/3532324

然后这里是来自iaindillingham的团体酒吧用作模型(我已修复他的版本以使用最新的d3库)。看到它在这里工作:http://bl.ocks.org/3532324