我正在使用JSON从数据库中获取数据,但是当我尝试将2个系列的数据显示为高图表时,图形结果无法正确显示。
fetchdata.php
while($row = sqlsrv_fetch_array($weekly2)) {
$BF1[] = $row['VoltageA'];
$BF2[] = $row['VoltageB'];
$wv3[] = $row['VoltageC'];
$wi4[] = $row['IA'];
$wi5[] = $row['IB'];
$wi6[] = $row['IC'];
}
echo json_encode(array($BF1, $BF2));
我从json_encode获得的数据:
[[0.80299997329712,0.80599999427795,0.80599999427795,0.80800002813339,0.80800002813339,0.80699998140335,0.80099999904633,0.80099999904633,0.78700000047684,0.77799999713898],[0.80599999427795,0.80900001525879,0.80800002813339,0.81000000238419,0.80900001525879,0.81000000238419,0.80299997329712,0.80299997329712,0.78899997472763,0.78299999237061]]
脚本
$.when(
$.getJSON('fetchdata.php')).then(function(BF1, BF2) {
Highcharts.chart('container', {
chart: {
type: 'line'
},
title: {
text: 'Daily Voltage'
},
subtitle: {
text: ''
},
xAxis: {
categories: ['12:00 AM', '1:00 AM', '2:00 AM', '3:00 AM', '4:00 AM', '5:00 AM', '6:00 AM', '7:00 AM', '8:00 AM', '9:00 AM', '10:00 AM', '11:00 AM','12:00 PM', '1:00 PM', '2:00 PM', '3:00 PM', '4:00 PM', '5:00 PM', '6:00 PM', '7:00 PM', '8:00 PM', '9:00 PM', '10:00 PM', '11:00 PM']
},
yAxis: {
title: {
text: 'FEEDER 1'
}
},
plotOptions: {
line: {
dataLabels: {
enabled: true
},
enableMouseTracking: true
}
},
series: [{
name: 'Voltage A',
data: BF1
},
{
name:'Voltage B',
data: BF2
}]
});
});
答案 0 :(得分:0)
您的json数据中只有一个轴。
看看https://graph.metaverse.farm/hashrate.html
数据: https://graph.metaverse.farm/api/data.json
代码: https://graph.metaverse.farm/common.js https://graph.metaverse.farm/hashrate.js
答案 1 :(得分:0)
发生此问题是因为您的$.getJSON()
呼叫使用不当。您已将两个参数传递给.then()
中定义的函数,但应该只是一个,因为您收到的数组包含两个数组,例如:
$.getJSON('fetchdata.php')).then(function(data) {
...
}
然后,您将可以在系列定义中引用data[0]
和data[1]
:
series: [{
name: 'Voltage A',
data: data[0]
},
{
name: 'Voltage B',
data: data[1]
}
]
实时示例: https://jsfiddle.net/abx6cepw/
亲切的问候!