gpt4 book ai didi

javascript - 如何使用数据对象中的键设置 c3.js 图表数据类型?

转载 作者:行者123 更新时间:2023-12-03 04:58:46 25 4
gpt4 key购买 nike

我正在尝试动态设置对象中每个键的数据类型。

该对象看起来像:

{"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'
}
}
}
});
};

我该怎么做?

最佳答案

您可以为您的类型/组构建另一个对象。如果您没有某些图表变体,则可以仅使用 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
<...>

关于javascript - 如何使用数据对象中的键设置 c3.js 图表数据类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42319587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com