Highcharts数据(使用PHP JSON MYSQL)
问题描述:
当我使用来自文件本身的“data.php”数据不会读取。但是,当我用 “data.json” 数据从文件中显示,highchartsHighcharts数据(使用PHP JSON MYSQL)
$(function() {
var chart;
$(document).ready(function() {
$.getJSON("***data.php***", function(json) {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'column',
marginRight: 130,
marginBottom: 25
},
title: {
text: 'Revenue vs. Overhead',
x: -20 //center
},
subtitle: {
text: '',
x: -20
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
title: {
text: 'No. of Events'
},
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
},
plotOptions: {
column: {
stacking: 'normal',
dataLabels: {
enabled: true,
color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
style: {
textShadow: '0 0 3px black'
}
}
}
},
series: json
});
});
});
data.json ..................... ................
[{
"name": "Revenue",
"data": [23987, 24784, 25899, 25569, 25897, 25668, 24114, 23899, 24987, 25111, 25899, 23221]
}, {
"name": "Overhead",
"data": [21990, 22365, 21987, 22369, 22558, 22987, 23521, 23003, 22756, 23112, 22987, 22897]
}];
data.php ......................
<?php
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("highcharts", $con);
$sth = mysql_query("SELECT revenue FROM projections_sample");
$rows = array();
$rows['name'] = 'Revenue';
while($r = mysql_fetch_array($sth)) {
$rows['data'][] = $r['revenue'];
}
$sth = mysql_query("SELECT overhead FROM projections_sample");
$rows1 = array();
$rows1['name'] = 'Overhead';
while($rr = mysql_fetch_assoc($sth)) {
$rows1['data'][] = $rr['overhead'];
}
$result = array();
array_push($result,$rows);
array_push($result,$rows1);
print json_encode($result, JSON_NUMERIC_CHECK);
mysql_close($con);
?>
答
使用AJAX首先从PHP获取值。如下所示:
$(function() {
var chart;
$(document).ready(function(){
$.ajax({
dataType: 'json',
url: 'data.php',
success: function(jsondata){
$.getJSON("***jsondata***", function(json){
// Your highchart codes here...
});
});
});
});
+0
混淆。我会怎么把*** jsondata ****? – Pau
您是否检查'$ result'是否有值? – rmondesilva
您可以尝试使用'$ .post('data.php',function(data){data = jQuery.parse(data);'调用data.php文件并以JSON样式存储数据,然后使用你的HighCharts –
@rmondesilva我将如何检查是否有值?hehe – Pau