Highcharts getJSON-图表未打印
在此之前的帮助使我的Highcharts项目进入了当前阶段,但是我仍然遇到了一个主要的绊脚石。Highcharts图表未加载使用.getJSON检索的数据。我不明白为什么html没有呈现图表并从data.php加载数据。下面所有的代码,有人有什么想法吗@帕韦夫斯 phpHighcharts getJSON-图表未打印,highcharts,getjson,Highcharts,Getjson,在此之前的帮助使我的Highcharts项目进入了当前阶段,但是我仍然遇到了一个主要的绊脚石。Highcharts图表未加载使用.getJSON检索的数据。我不明白为什么html没有呈现图表并从data.php加载数据。下面所有的代码,有人有什么想法吗@帕韦夫斯 php <?php $con = mysql_connect("ip_address","root",""); if (!$con) { die('Could not connect: ' . mysql_error()
<?php
$con = mysql_connect("ip_address","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$result = array();
$sql = mysql_query("SELECT unix_timestamp(DATETIMES), TEST FROM PR");
$result['name'] = 'TEST';
while($r = mysql_fetch_array($sql)) {
$datetime = $r[0]*1000;
$result['category'][] = $datetime;
$result['data'][] = round($r[1],2) ;
}
$json = array();
array_push($json,$result);
print json_encode($json);
?>
html
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Highcharts Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var options = {
chart: {
renderTo: 'container',
type: 'line',
marginRight: 130,
marginBottom: 25
},
title: {
text: 'test',
x: -20 //center
},
subtitle: {
text: '',
x: -20
},
xAxis: {
categories: []
},
yAxis: {
title: {
text: 'test'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
this.x +': '+ this.y;
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -10,
y: 100,
borderWidth: 0
},
series: []
}
$.getJSON("data.php", function(json) {
options.xAxis.categories = json['category'];
options.series[0].name = json['name'];
options.series[0].data = json['data'];
chart = new Highcharts.Chart(options);
});
});
</script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
</head>
<body>
<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>
</body>
</html>
海图示例
$(文档).ready(函数(){
变量选项={
图表:{
renderTo:'容器',
键入:“行”,
marginRight:130,
marginBottom:25
},
标题:{
文本:“测试”,
x:-20/中心
},
副标题:{
文本:“”,
x:-20
},
xAxis:{
类别:[]
},
亚克斯:{
标题:{
文本:“测试”
},
绘图线:[{
值:0,
宽度:1,
颜色:'#808080'
}]
},
工具提示:{
格式化程序:函数(){
返回“+this.series.name+”
+
this.x+':'+this.y;
}
},
图例:{
布局:“垂直”,
对齐:“右”,
垂直排列:“顶部”,
x:-10,
y:100,
边框宽度:0
},
系列:[]
}
$.getJSON(“data.php”,函数(json){
options.xAxis.categories=json['category'];
options.series[0].name=json['name'];
options.series[0]。data=json['data'];
图表=新的高点图表。图表(选项);
});
});
您的系列数组(在选项中)没有对象,因此无法使用系列[0]。名称。其次,您的json使用不正确,但请尝试替换以下行:
options.xAxis.categories = json['category'];
options.series[0].name = json['name'];
options.series[0].data = json['data'];
与
感谢Seb的回复。替换选项行会导致部分成功。图表现在正在渲染(使用正确的序列名称),但是数据仍然不显示。这里是一个实例,data.php输出存储在这里。Chrome控制台显示highcharts.js错误19。不熟悉Highcharts调试,有什么见解吗?你使用这个json还是其他的?原始帖子中的json用于测试目的,是链接json的一个子集。啊。。。也许可以解释一下,我们会尝试减少查询中的数据点数量,但是有没有办法绕过datetime/类别限制,或者有没有其他方法可以加载大量json datetime数据?
options.xAxis.categories = json['category'];
options.series[0].name = json['name'];
options.series[0].data = json['data'];
options.xAxis.categories = json[0]['category'];
options.series[0] = {};
options.series[0].name = json[0]['name'];
options.series[0].data = json[0]['data'];