在堆积条形图c3js中动态指定"groups"
Specifying "groups" dynamically in stacked bar chart c3js
//以这段代码为例
这里我在组中指定了yvalue[0],yvalue[1]..
但我需要一个通用设计,我不知道我必须创建的组数(即段数)这根据 json 数据而变化。
考虑这个例子,我有总数,总数 1 因此我只有 2 values.But 如果第三个变量说 total2 在 json 中指定,我应该在我的条形图中有一个段因此 on.This 必须在每次字段为 added.Is 时不改变组来完成,有什么办法可以实现这个目标吗??
谢谢
var datajson = [ {
country : "china",
total : 20,
total1 : 10
}, {
country : "India",
total : 40,
total1 : 20
}, {
country : "aus",
total : 10,
total1 : 30
}, {
country : "nxz",
total : 50,
total1 : 40
}
];
var xvalue;
var yvalue = [];
var i = 0;
var obj = datajson[0]
for ( var key in obj) {
if (xvalue === undefined)
xvalue = key;
else {
yvalue[i] = key;
i++;
}
}
var chart = c3.generate({
bindto : '#chart',
data : {
json : datajson,
type : 'bar',
keys : {
x : xvalue, // it's possible to specify 'x' when category axis
value : [yvalue[0],yvalue[1]],
},
groups : [ [yvalue[0],yvalue[1]] ]
},
bar : {
width : {
ratio : 0.3
// this makes bar width 50% of length between ticks
}
},
axis : {
x : {
type : 'category'
},
}
});
你的问题似乎有大部分答案(你已经从对象属性生成了 yvalue 数组)。
不需要一一指定,直接传入数组即可。
groups: [yvalue]
//以这段代码为例
这里我在组中指定了yvalue[0],yvalue[1].. 但我需要一个通用设计,我不知道我必须创建的组数(即段数)这根据 json 数据而变化。
考虑这个例子,我有总数,总数 1 因此我只有 2 values.But 如果第三个变量说 total2 在 json 中指定,我应该在我的条形图中有一个段因此 on.This 必须在每次字段为 added.Is 时不改变组来完成,有什么办法可以实现这个目标吗??
谢谢
var datajson = [ {
country : "china",
total : 20,
total1 : 10
}, {
country : "India",
total : 40,
total1 : 20
}, {
country : "aus",
total : 10,
total1 : 30
}, {
country : "nxz",
total : 50,
total1 : 40
}
];
var xvalue;
var yvalue = [];
var i = 0;
var obj = datajson[0]
for ( var key in obj) {
if (xvalue === undefined)
xvalue = key;
else {
yvalue[i] = key;
i++;
}
}
var chart = c3.generate({
bindto : '#chart',
data : {
json : datajson,
type : 'bar',
keys : {
x : xvalue, // it's possible to specify 'x' when category axis
value : [yvalue[0],yvalue[1]],
},
groups : [ [yvalue[0],yvalue[1]] ]
},
bar : {
width : {
ratio : 0.3
// this makes bar width 50% of length between ticks
}
},
axis : {
x : {
type : 'category'
},
}
});
你的问题似乎有大部分答案(你已经从对象属性生成了 yvalue 数组)。
不需要一一指定,直接传入数组即可。
groups: [yvalue]