您可以通过以下方式轻松地将您的 json 数据回显到 C3.js 中:
var dataset = <?php echo $lineChartData; ?>;
data: {
json: dataset
}
我正在使用与我的 Web 项目 (mySQL-PHP-C3.js) 相同的管道。重要的是,您的 json 数据完全符合 C3 输入所需的格式。正如您在 C3.js 文档 (https://c3js.org/samples/timeseries.html) 中看到的,时间序列的数据格式应如下所示:
data: {
x: 'x',
// xFormat: '%Y%m%d', // 'xFormat' can be used as custom format of 'x'
columns: [
['x', '2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'],
// ['x', '20130101', '20130102', '20130103', '20130104', '20130105', '20130106'],
['data1', 30, 200, 100, 400, 150, 250],
['data2', 130, 340, 200, 500, 250, 350]
]
},
axis: {
x: {
type: 'timeseries',
tick: {
format: '%Y-%m-%d'
}
}
}
对于我的数据(不是时间序列数据),json 对象如下所示:
{"data1": [0.23, 0.29, 0.19, 0.14, 0.7], "data2": [0, 0.25, 0.61, 0.4, 0.52]}
如果您遵循此结构并将您的时间数据(日期)作为第三维添加到其中,您应该能够实现您想要的。