Highcharts .getJSON - 图表不绘制 [英] Highcharts .getJSON - chart not plotting
本文介绍了Highcharts .getJSON - 图表不绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
php
< ;?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);
打印json_encode($ json);
?>
php返回
[{ 名称: CCGT, 类别:[1387791900000,1387792200000,1387792500000,1387792800000], 数据:[8389,8478,8761,8980,9050 ]}]
html
<!DOCTYPE HTML>
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8>
< title> Highcharts示例< / title>
< script type =text / javascriptsrc =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
$,
字幕:{
text:'',
x:-20
},
xAxis:{
categories:[]
$ b yAxis:{
title:{
text:'test'
},
plotLines:[{
value:0,
宽度:1,
颜色:'#808080'
}]
},
tooltip:{
formatter:function(){
return'< b> + this.series.name +'< / b>< ; br />'+
this.x +':'+ this.y;
}
},
图例:{
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 =containerstyle =min-width:400px; height:400px; margin:0 auto>< / div>
< / body>
< / html>
解决方案
所以你不能使用系列[0] .name。其次,你的json没有被正确使用,但是尝试替换这些行:
options.xAxis.categories = json ['category' ]。
options.series [0] .name = json ['name'];
options.series [0] .data = json ['data'];
with
options.xAxis.categories = json [0] ['category'];
options.series [0] = {};
options.series [0] .name = json [0] ['name'];
options.series [0] .data = json [0] ['data'];
Previous help here has got me to the current stage with my Highcharts project, however I am still hitting a major stumbling block. Highcharts chart is not loading data retrieved using .getJSON. I am at a loss as to why the html is not rendering the chart and load the data from data.php. All code below, does anyone have any ideas? @PawełFus
php
<?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);
?>
php return
[{"name":"CCGT","category":[1387791900000,1387792200000,1387792500000,1387792800000],"data":[8389,8478,8761,8980,9050]}]
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>
解决方案
Your series array (in options) has no object, so you cannot use series[0].name. Secondly your json is not used correct, but try to replace this lines:
options.xAxis.categories = json['category'];
options.series[0].name = json['name'];
options.series[0].data = json['data'];
with
options.xAxis.categories = json[0]['category'];
options.series[0] = {};
options.series[0].name = json[0]['name'];
options.series[0].data = json[0]['data'];
这篇关于Highcharts .getJSON - 图表不绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文