我有C3图表,当列直接提供时有效,但当作为字符串参数提供时不起作用。
这是jsfiddle: http://jsfiddle.net/q8h39/79/
Javascript代码:
dataval = "[['x', '2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'],['data1', 30, 200, 100, 400, 150, 250], ['data2', 130, 340, 200, 500, 250, 350]]";
var chart = c3.generate({
bindto: '#chart1',
data: {
x: 'x',
xFormat: '%Y-%m-%d %H:%M:%S', // 'xFormat' can be used as custom format of 'x'
columns: dataval
},
axis: {
x: {
type: 'timeseries',
tick: {
format: '%Y-%m-%d %H:%M:%S'
}
}
}
});
答案 0 :(得分:1)
原因是这是一个字符串(用双引号括起来)
"[['x', '2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'],['data1', 30, 200, 100, 400, 150, 250], ['data2', 130, 340, 200, 500, 250, 350]]"
这是一个数组对象:
[['x', '2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'],['data1', 30, 200, 100, 400, 150, 250], ['data2', 130, 340, 200, 500, 250, 350]]
在c3中columns
期望数组对象不是字符串
修改强>
要将传递给JSON数组的字符串转换为:
dataval = "[['x', '2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'],['data1', 30, 200, 100, 400, 150, 250], ['data2', 130, 340, 200, 500, 250, 350]]";
dataval = dataval.replace(/'/g, "\"");//replacing single quotes with double quotes
dataval = JSON.parse(dataval);
工作代码here
希望这有帮助!