使用面板数据集在DC JS中创建堆叠图 [英] Creating stacked charts in dc js with panel dataset
问题描述
我在创建堆叠的条形图时遇到问题,我无法弄清楚如何正确地将尺寸应用于面板数据集
I'm having trouble creating a stacked barchart, I cannot figure out how to properly apply the dimensions to a panel dataset
我的数据如下:
Date name value
12/1/15 name1 5
12/1/15 name2 6
12/1/15 name3 2
13/1/15 name1 2
13/1/15 name2 7
13/1/15 name3 8
14/1/15 name1 2
14/1/15 name2 5
14/1/15 name3 10
以JSON格式存储
我想创建堆叠的图表,以绘制随时间变化与不同名称关联的值.
I would like to create stacked charts that plot the values associated with the different names over time.
据我了解dc-js,我需要提供一个带有日期维度的图表,然后提供一个具有不同名称的维度,然后将值分组,但是我不确定如何进行操作.
As I understand dc-js I need to provide a chart with the date dimension, and then another dimension by the different names which then groups the values, however I am unsure how to proceed.
有人知道我该怎么做吗?
Does anyone know how I can do this with?
推荐答案
从常见问题解答:
var group = dimension.group().reduce(
function(p, v) { // add
p[v.type] = (p[v.type] || 0) + v.value;
return p;
},
function(p, v) { // remove
p[v.type] -= v.value;
return p;
},
function() { // initial
return {};
});
(在您的情况下为v.name
,而不是v.type
.)
(In your case, v.name
instead of v.type
.)
基本上,您创建一个组,将其简化为包含每个堆栈值的对象.
Basically you create a group which reduces to objects containing the values for each stack.
然后使用.group()
和.stack()
的名称和访问器参数.不幸的是,您必须将其拼写清楚,因为第一个必须为.group
,其余的必须为.stack
.
Then use the name and accessor parameters of .group()
and .stack()
. Unfortunately you have to spell it out, as the first has to be .group
and the rest .stack
.
如此:
chart.group(group, 'name1', function(d) { return d.name1; })
.stack(group, 'name2', function(d) { return d.name2; })
.stack(group, 'name3', function(d) { return d.name3; })
...
(在SO或Web上的某个地方是这种形式的循环,但我找不到ATM.)
(Somewhere on SO or the web is a loop form of this, but I can't find it ATM.)
这篇关于使用面板数据集在DC JS中创建堆叠图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!