$co-6ren">
gpt4 book ai didi

php - 尝试将 JSON 数据传递给 Highcharts 但没有成功

转载 作者:太空宇宙 更新时间:2023-11-03 12:35:09 25 4
gpt4 key购买 nike

我正在尝试将 php 文件创建的 json 数据解析为另一个脚本,并将其值显示到 Highcharts 。

我创建 json 数据的 data.php 是这样的:

<?php
header("Content-type: application/json");

$dbhost = "localhost";
$dbuser = "db";
$dbpass = "xxxxx";
$dbname = "db";
$db = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname,$db);
$query = "SELECT * FROM monitor_total";

$result = mysql_query($query,$db);

while($row = mysql_fetch_array($result)) {
$date = $row["date"];
$count = $row["count"];
$array[] = array("date" =>$date,"count"=>$count);
}
echo json_encode($array);
?>

data.php 的输出是:

[{"date":"2012-11-23","count":"582311"},{"date":"2012-11-24","count":"582322"},{"date":"2012-11-22","count":"582121"},{"date":"2012-11-21","count":"581321"},{"date":"2012-11-19","count":"572821"},{"date":"2012-11-20","count":"581321"},{"date":"2012-11-18","count":"582421"},{"date":"2012-11-17","count":"579321"},{"date":"2012-11-16","count":"581321"},{"date":"2012-11-25","count":"558178"}]

内部<script> :

var monitor_graph; // globally available
monitor_graph = new Highcharts.Chart({
chart: {
renderTo: 'graph',
type: 'spline',
events: {
load: requestData
}
},
title: {
text: 'Registered and total players by date'
},
xAxis: {
categories: []
},
yAxis: {
title: {
text: 'Players'
}
},
series: [{
name: 'Total Players',
data: []
}]
});
function requestData() {
$.ajax({
url: 'data.php',
success: function(data) {
$.each(data, function(i,item){

monitor_graph.series[0].setData(??????????);

});
setTimeout(requestData, 1000);
},
});
}

我如何创建一个名为 total players 的系列,x 轴为“item.date”,y 轴为“item.count”???

请帮帮我!

更新:我在每个里面添加这个:

monitor_graph.xAxis[0].setCategories([item.date]);
monitor_graph.series[0].setData([parseFloat(item.count)]);

现在我得到了我需要的 1 分,但出现以下错误:

Unexpected value NaN parsing y attribute.

最佳答案

你需要将你的成功回调修改成这样

var categories=[];
var seriesData=[];
$.each(data,function(){
categories.push(this.date);
seriesData.push(parseInt(this.count));
});
chart.xAxis[0].setCategories(categories);
chart.series[0].setData(seriesData);

Series.setData() 方法需要一个 int/float 数组而不是字符串,应该用整个数据集而不是每个点调用。您在 $.each 中执行此操作应该在此循环之外完成。类别也是如此

Demo @ jsFiddle

关于php - 尝试将 JSON 数据传递给 Highcharts 但没有成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13554074/

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