我最近刚开始使用JSON,我无法从Highcharts绘制任何类型的图表。自上周五以来,我一直在互联网上阅读和阅读,我当然学到了很多东西,但现在我很绝望。我只想要一个简单的酒吧和一个馅饼!
从我的php文件中,Json_encode打印以下内容:
[["January",4],["February",9]]
我认为这是正确的格式,带有"的字符串和不带它的 int。
这是我正在尝试的代码(我从我找到的某个网络上放置了整个示例):
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<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: 'column',
marginRight: 130,
marginBottom: 25
},
title: {
text: 'Project Requests',
x: -20 //center
},
subtitle: {
text: '',
x: -20
},
xAxis: {
categories: []
},
yAxis: {
title: {
text: 'Requests'
},
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("myphpname.php", function(json) {
options.xAxis.categories = json[0]['data'];
options.series[0] = json[1];
chart = new Highcharts.Chart(options);
});
});
</script>
</head>
<body>
<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>
</body>
</html>
谢谢大家!!
对此进行排序的一种方法是分别设置类别和数据:
$(function () {
var options = {
chart: {
renderTo: 'container',
type: 'column'
},
xAxis: {
categories: ["A", "B"]
},
series: [{
data: [
4,9
]
}]
};
chart = new Highcharts.Chart(options);
});
你的 getJson 可以返回一个对象,如下所示:
{
categories:["January","February"],
data:[4,9]
}
然后你的JavaScript可以设置图表选项,如下所示:
options.xAxis.categories = json.categories;
options.series[0] = json.data;
代码没问题。问题是我使用的是免费服务器。
使用 XAMPP,在localhost
它完美运行!
谢谢大家!