如何使用数据对象中的键设置 c3.js 图表数据类型?
How can I set c3.js chart data type using keys from my data object?
我正在尝试为对象中的每个键动态设置数据类型。
对象看起来像:
{"x":["2016-01-1", "2016-01-02", etc], "uniqueKey":[4234, 4324, etc], "uniqueKey2":[432, 543, etc], ... }
键的数量和名称会根据我抓取的数据而有所不同,但我需要为每个键设置图表数据类型。
function show_chart1(data) {
var json = JSON.parse(data);
var chart = c3.generate({
bindto: '#chart',
data: {
x: 'x',
xFormat: '%Y-%m-%d',
json: json,
#### need these to be the unique key names
types: {
uniqueKey: 'area-spline',
uniqueKey2: 'area-spline'
...
},
groups: [[uniqueKey, uniqueKey2, etc]]
},
axis: {
x: {
type: 'timeseries',
tick: {
format: '%Y-%m-%d'
}
}
}
});
};
我该怎么做?
您可以为您的 types/groups 构建另一个对象。如果您没有一些图表变体,您可以只使用 type: 'area-spline'
而不是为每个条目提供相同的类型。
当您知道您的唯一键是什么或它们看起来像什么(参考正则表达式)时,您可以这样做:
var typeDefinition = {}
for(var key in json){
// here you can use if or switch to apply certain types
if( key <your statement> ){
typeDefinition[key] = 'area-spline';
}
}
<...>
types: typeDefinition
<...>
我正在尝试为对象中的每个键动态设置数据类型。
对象看起来像:
{"x":["2016-01-1", "2016-01-02", etc], "uniqueKey":[4234, 4324, etc], "uniqueKey2":[432, 543, etc], ... }
键的数量和名称会根据我抓取的数据而有所不同,但我需要为每个键设置图表数据类型。
function show_chart1(data) {
var json = JSON.parse(data);
var chart = c3.generate({
bindto: '#chart',
data: {
x: 'x',
xFormat: '%Y-%m-%d',
json: json,
#### need these to be the unique key names
types: {
uniqueKey: 'area-spline',
uniqueKey2: 'area-spline'
...
},
groups: [[uniqueKey, uniqueKey2, etc]]
},
axis: {
x: {
type: 'timeseries',
tick: {
format: '%Y-%m-%d'
}
}
}
});
};
我该怎么做?
您可以为您的 types/groups 构建另一个对象。如果您没有一些图表变体,您可以只使用 type: 'area-spline'
而不是为每个条目提供相同的类型。
当您知道您的唯一键是什么或它们看起来像什么(参考正则表达式)时,您可以这样做:
var typeDefinition = {}
for(var key in json){
// here you can use if or switch to apply certain types
if( key <your statement> ){
typeDefinition[key] = 'area-spline';
}
}
<...>
types: typeDefinition
<...>