简体   繁体   English

来自Array数据的D3堆栈图表

[英]D3 stack chart from Array data

In all the examples to generate a Stacked Chart using D3 (v4) I've seen so far, the input is an Array of JSON objects, like below 在所有使用D3(v4)生成堆积图表的示例中,我到目前为止看到,输入是一个JSON对象数组, 如下所示

var data = [
  {month: new Date(2015, 0, 1), apples: 3840, bananas: 1920, cherries: 960, dates: 400},
  {month: new Date(2015, 1, 1), apples: 1600, bananas: 1440, cherries: 960, dates: 400},
  {month: new Date(2015, 2, 1), apples:  640, bananas:  960, cherries: 640, dates: 400},
  {month: new Date(2015, 3, 1), apples:  320, bananas:  480, cherries: 640, dates: 400}
];

How can I generate a stack if I have an array (with implicit keys) instead? 如果我有一个数组(带有隐式键),我怎么能生成一个堆栈呢? Something like... 就像是...

var data = {
  "2015, 0, 1": [3840, 1920, 960, 400],
  "2015, 1, 1": [1600, 1440, 960, 400],
  "2015, 2, 1": [ 640,  960, 640, 400],
  "2015, 3, 1": [ 320,  480, 640, 400]
};

The reason for such an input structure is that, I have to really draw several stacks with the data that looks like this: 这种输入结构的原因是,我必须使用如下所示的数据绘制几个堆栈:

// need to draw one stack per row below
var data = {
  "1": {"A":[100,200], "B":[200,300]},
  "2": {"X":[34,90], "A":[210,90], "C":[56,67]},
  "3": {"B":[50,40], "C":[0,100], "Z":[50,500], "Q":[78,87]},
  "4": {"Z":[40,50]},
  ...
}

The API is clear: you have to pass an array to the stack generator. API很明确:您必须将数组传递给堆栈生成器。 According to it, d3.stack() ... 根据它, d3.stack() ......

... generates a stack for the given array of data , returning an array representing each series. ...为给定的数据数组生成一个堆栈,返回一个表示每个系列的数组。 (emphasis mine) (强调我的)

Right now you have an object , not an array. 现在你有一个对象 ,而不是一个数组。

So, I believe the best solution here is simply converting that object to the array structure expected by the stack generator, which you shared in your very question (the first array). 所以,我认为这里最好的解决方案就是将该对象转换为堆栈生成器所期望的数组结构,您在这个问题中共享(第一个数组)。

This involves just vanilla JavaScript. 这只涉及vanilla JavaScript。 This is an example, intentionally verbose so you can see the steps: 这是一个例子,故意冗长,所以你可以看到以下步骤:

 var data = { "2015, 0, 1": [3840, 1920, 960, 400], "2015, 1, 1": [1600, 1440, 960, 400], "2015, 2, 1": [640, 960, 640, 400], "2015, 3, 1": [320, 480, 640, 400] }; var dataArray = []; for (var key in data) { var obj = {}; obj.month = key; data[key].forEach(function(d, i) { obj["value" + i] = d; }) dataArray.push(obj) } console.log(dataArray) 

It will generate this array: 它会生成这个数组:

[
    {month: "2015, 0, 1", value0: 3840, value1: 1920, value2: 960, value3: 400},
    {month: "2015, 1, 1", value0: 1600, value1: 1440, value2: 960, value3: 400},
    {month: "2015, 2, 1", value0: 640, value1: 960, value2: 640, value3: 400},
    {month: "2015, 3, 1", value0: 320, value1: 480, value2: 640, value3: 400}
]

Which has the same structure of the first array in your question. 其中您的问题与第一个数组的结构相同。

You can change month and value + i to the key names you want. 您可以将monthvalue + i更改为所需的键名称。

Using arrays in order to render stack bars (or other stack representations) can be achieved when data is an array by generating the keys object to contain the indexes of the values as such: 当数据是数组时,通过生成键对象以包含值的索引,可以使用数组来渲染堆栈条(或其他堆栈表示):

const serverData = {
  "2015, 0, 1": [3840, 1920, 960, 400],
  "2015, 1, 1": [1600, 1440, 960, 400],
  "2015, 2, 1": [ 640,  960, 640, 400],
  "2015, 3, 1": [ 320,  480, 640, 400]
};

const serverDataKeys = Object.keys(serverData)
const data = serverDataKeys.map(key => serverData[key]);

/**
data = [
 [3840, 1920, 960, 400],
 [1600, 1440, 960, 400],
 [640, 960, 640, 400],
 [320, 480, 640, 400]
]
*/

const keys = serverData[serverDataKeys[0]].map( (d, index) => index);

/** keys:  [0, 1, 2, 3] */

svg.append('g')
  .selectAll('g')
  .data(d3.stack().keys(keys)(data))
  .enter().append('g')

In the following case: 在以下情况中:

{month: new Date(2015, 0, 1), apples: 3840, bananas: 1920, cherries: 960, dates: 400}

data is the entire object and the keys are ['apples', 'bannanas', 'cherries', 'dates'] . 数据是整个对象,键是['apples', 'bannanas', 'cherries', 'dates']

When using an array [3840, 1920, 960, 400] the keys should be [0,1,2,3] 当使用数组[3840, 1920, 960, 400] ,键应为[0,1,2,3]

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

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