Highcharts PHP JSON-正确的数据格式
这里是Highcharts/PHP/JS新手。我在格式化所需的数据数组($data)以输入Highcharts时遇到问题。嵌入的php代码输出表单的datetime/decimal数据数组Highcharts PHP JSON-正确的数据格式,php,json,highcharts,Php,Json,Highcharts,这里是Highcharts/PHP/JS新手。我在格式化所需的数据数组($data)以输入Highcharts时遇到问题。嵌入的php代码输出表单的datetime/decimal数据数组 [1388680500000, 11215], [1388680800000, 11334], [1388681100000, 11487], [1388681400000, 11600], [1388681700000, 11641], [1388682000000, 11648], [1388682300
[1388680500000, 11215], [1388680800000, 11334], [1388681100000, 11487], [1388681400000, 11600], [1388681700000, 11641], [1388682000000, 11648], [1388682300000, 11692], [1388682600000, 11641], [1388682900000, 11715], [1388683200000, 11808], [1388683500000, 11845], [1388683800000, 11813], [1388684100000, 11771], [1388684400000, 11746], [1388684700000, 11808], [1388685000000, 11784], [1388685300000, 11727], [1388685600000, 11608], [1388685900000, 11648], [1388686200000, 11587], [1388686500000, 11555], [1388686800000, 11596], [1388687100000, 11541], [1388687400000, 11441], [1388687700000, 11393], [1388688000000, 11340], [1388688300000, 11152], [1388688600000, 11031], [1388688900000, 10920], [1388689200000, 10681], [1388689500000, 10221], [1388689800000, 9895]
我目前正在手动将此数据(在[]中)复制到序列:数据:字段中,该字段成功生成图表(见下文)。我已尝试用替换数据字段
系列:[{
数据:[]
但是,不再创建图表。(根据)
我也在php和JS中尝试了多次json_encode($data)迭代,但都没有成功。如果有人能向我展示php输出到Highcharts数据字段的准确翻译,我将非常感激,最好是使用json
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<?php
//the usual stuff
error_reporting(-1);
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$result = mysql_query("SELECT unix_timestamp(DATETIMES), TEST FROM PT");
$data = array();
while ($row = mysql_fetch_array($result)) {
extract($row);
$row[0]=1000*$row[0];
$data[] = "[$row[0], $row[1]]";
}
mysql_close($con);
echo join($data, ', ');
?>
<script>
$(function() {
Highcharts.setOptions({
chart: {
backgroundColor: 'rgba(255, 255, 255, .9)',
borderWidth: 2,
plotBackgroundColor: 'rgba(255, 255, 255, .9)',
plotShadow: true,
plotBorderWidth: 1
}
});
var chart1 = new Highcharts.Chart({
chart: {
renderTo: 'container',
},
xAxis: {
type: 'datetime'
},
series: [{
data: [[1388680500000, 11215], [1388680800000, 11334], [1388681100000, 11487], [1388681400000, 11600], [1388681700000, 11641], [1388682000000, 11648], [1388682300000, 11692], [1388682600000, 11641], [1388682900000, 11715], [1388683200000, 11808], [1388683500000, 11845], [1388683800000, 11813], [1388684100000, 11771], [1388684400000, 11746], [1388684700000, 11808], [1388685000000, 11784], [1388685300000, 11727], [1388685600000, 11608], [1388685900000, 11648], [1388686200000, 11587], [1388686500000, 11555], [1388686800000, 11596], [1388687100000, 11541], [1388687400000, 11441], [1388687700000, 11393], [1388688000000, 11340], [1388688300000, 11152], [1388688600000, 11031], [1388688900000, 10920], [1388689200000, 10681], [1388689500000, 10221], [1388689800000, 9895]],
pointStart: 0,
pointInterval: 3600 * 1000 // one hour
}]
});
});
</script>
</head>
<body>
<div id="container" style="height: 300px"></div>
</body>
</html>
$(函数(){
Highcharts.setOptions({
图表:{
背景颜色:“rgba(255,255,255,.9)”,
边界宽度:2,
plotBackgroundColor:“rgba(255,255,255,.9)”,
是的,
绘图边框宽度:1
}
});
var chart1=新的高点图表。图表({
图表:{
renderTo:'容器',
},
xAxis:{
键入:“日期时间”
},
系列:[{
数据:[138868050000011215],[1388680080000011334],[1388681100011487],[13886814000011600],[13886870000011641],[13886800011648],[13886830000011692],[138868600000111641],[1388688290000011715],[1388688320000011808],[13888350000011845],[13888380000011813],[138841000011771],[13888480000011771],[13888440000011741],[13888440000011746],[1388684700000, 11808], [1388685000000, 11784], [1388685300000, 11727], [1388685600000, 11608], [1388685900000, 11648], [1388686200000, 11587], [1388686500000, 11555], [1388686800000, 11596], [1388687100000, 11541], [1388687400000, 11441], [1388687700000, 11393], [1388688000000, 11340], [1388688300000, 11152], [1388688600000, 11031], [1388688900000, 10920], [1388689200000, 10681], [1388689500000, 10221], [1388689800000, 9895]],
起点:0,
点间隔:3600*1000//1小时
}]
});
});
编辑:
我已经将脚本重新调整为以下格式,但是图表不再打印…我确信JSON格式是正确的。请帮助
<!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: 'Amount'
},
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'];
图表=新的高点图表。图表(选项);
});
});
尝试以下代码从MYSQL获取记录并将其转换为highchart数据
setup.js
var chart;
$(document).ready(function() {
var cursan = {
chart: {
renderTo: 'current_sanand',
defaultSeriesType: 'area',
marginRight: 10,
marginBottom: 20
},
title: {
text: '',
},
subtitle: {
text: '2013/14',
},
xAxis: {
categories: ['Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'Jan', 'Feb', 'Mar']
},
yAxis: {
title: {
text: 'Amount [Billion]'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
crosshairs: true,
shared: true
},
legend: {
layout: 'vertical',
backgroundColor: 'white',
align: 'right',
verticalAlign: 'top',
y: 35,
x: -10,
borderWidth: 1,
borderRadius: 0,
title: {
text: '::::::::::::'
},
floating: true,
draggable: true,
zIndex: 20
},
plotOptions: {
series: {
cursor: 'pointer',
marker: {
lineWidth: 1
}
}
},
series: [{
color: Highcharts.getOptions().colors[2],
name: 'Sanand',
marker: {
fillColor: '#FFFFFF',
lineWidth: 2,
lineColor: null // inherit from series
},
dataLabels: {
enabled: true,
rotation: 0,
color: '#666666',
align: 'top',
x: -10,
y: -10,
style: {
fontSize: '9px',
fontFamily: 'Verdana, sans-serif',
textShadow: '0 0 0px black'
}
}
}],
}
$.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);
});
});
data.php
<html>
<body>
<div id="container" style="height: 300px"></div>
</body>
</html>
$con=mysql_connect(“本地主机”、“根目录”、“根目录”)
index.php
<html>
<body>
<div id="container" style="height: 300px"></div>
</body>
</html>
我已经尽力了,这个方法工作正常。试试看。谢谢@Sameer的回复。php输出的形式为{“名称”:“销售摘要”,“类别”:[13879190000138779220000013877925000001387792800001387931000000],“数据”:[83898478876189809050]}但是,在运行html代码时,图表是构造的,但序列没有绘制。是否需要在.getJSON中创建新的Highcharts.chart,如果新图表已经在代码中指定了?是的,我们正在使用不同的数据创建新图表,因此需要创建。JSON格式包含curly是否也有关系括号在外面?@user3080213不管我们是否从数据库导入数据。请参阅相关主题