Highcharts PHP JSON - 正确的数据格式 [英] Highcharts PHP JSON - correct data format

查看:117
本文介绍了Highcharts PHP JSON - 正确的数据格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Highcharts / PHP / JS新手在这里。我无法格式化所需的数据数组($ data)以馈入Highcharts。嵌入式php代码输出格式为

  [1388680500000,11215],[1388680800000,11334]的日期时间/小数数组, [1388681100000,11487],[1388681400000,11600],[1388681700000,11641],[13886.82亿,11648],[1388682300000,11692],[1388682600000,11641],[1388682900000,11715],[1388683200000,11808],[1388683500000 ,11845],[1388683800000,11813],[1388684100000,11771],[1388684400000,11746],[1388684700000,11808],[13886.85亿,11784],[1388685300000,11727],[1388685600000,11608],[1388685900000,11648 ],[1388686200000,11587],[1388686500000,11555],[1388686800000,11596],[1388687100000,11541],[1388687400000,11441],[1388687700000,11393],[13886.88亿,11340],[1388688300000,11152] [1388688600000,11031],[1388688900000,10920],[1388689200000,10681],[1388689500000,10221],[1388689800000,9895] 

我目前手动将这些数据(在[]中)复制到series:data:弹出图表(见下文)。我试图用
系列替换数据字段:[{
data:[]
但是图表不再被创建。 (根据 http://www.highcharts。 com / docs / working-with-data / preprocessing-data-from-a-database

我也尝试了许多json_encode($ data )在PHP和JS但无济于事。我会非常感谢任何能够向我显示php输出到Highcharts数据字段的确切翻译的人,最好使用JSON。

 <!DOCTYPE html> 
< html>
< head>
< title>< / title>
< script type =text / javascriptsrc =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
//通常的东西
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({
图表:{
renderTo:'container',
},

xAxis :{
类型:'datetime'
},

系列:[{
data:[[1388680500000,11215],[1388680800000,11334],[1388681100000 ,11487],[1388681400000,11600],[1388681700000,11641],[13886.82亿,11648],[1388682300000,11692],[1388682600000,11641],[1388682900000,11715],[1388683200000,11808],[1388683500000,11845 ],[1388683800000,11813],[1388684100000,11771],[1388684400000,11746],[1388684700000,11808],[1388685000000,11784],[13886 85300000,11727],[1388685600000,11608],[1388685900000,11648],[1388686200000,11587],[1388686500000,11555],[1388686800000,11596],[1388687100000,11541],[1388687400000,11441],[1388687700000, 11393],[13886.88亿,11340],[1388688300000,11152],[1388688600000,11031],[1388688900000,10920],[1388689200000,10681],[1388689500000,10221],[1388689800000,9895],
pointStart:0,
pointInterval:3600 * 1000 //一小时
}]
});
});
< / script>

< / head>
< body>

< div id =containerstyle =height:300px>< / div>

< / body>
< / html>

编辑:



脚本转换为以下格式,但图表不再显示...我相信JSON格式是正确的。请帮助!

 <!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 $ {
title:{
text:'Amount'
},
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>


解决方案

尝试下面的代码从MYSQL获取记录并将其转换到高级数据。

setup.js

  var chart; 
$(document).ready(function(){
var cursan = {
chart:{
renderTo:'current_sanand',
defaultSeriesType:'area',
marginRight:10,
marginBottom:20
},
title:{
text:'',
},
副标题:{
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:{

系列:{
cursor:'pointer',
marker:{
lineWidth:1
}
}
},

系列:[{
color:Highcharts.getOptions()。colors [2],
name:'Sanand',
marker:{
fillColor:'#FFFFFF',
lineWidth:2,
lineColor:null //从系列
继承,
dataLabels:{
启用:true,
旋转: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.categorie s = json ['category'];
options.series [0] .name = json ['name'];
options.series [0] .data = json ['data'];
图表= new Highcharts.Chart(options);
});

});

data.php



$ con = mysql_connect(localhost,root,);

  if(!$ con ){
die('Could not connect:'。mysql_error());
}

mysql_select_db(test,$ con);
$ sql = mysql_query(SELECT unix_timestamp(DATETIMES),TEST FROM PT);
$ result ['name'] ='销售汇总';
while($ r = mysql_fetch_array($ sql)){
$ datetime = $ r ['inv_dt'];
$ result ['category'] [] = $ datetime;
$ result ['data'] [] = round($ r ['amount'],2);
}
print json_encode($ result);

index.php

 < html> 
< body>
< div id =containerstyle =height:300px>< / div>
< / body>
< / html>

我尽了最大努力,并且此方法正常运行。试试看。


Highcharts/PHP/JS newbie here. I am having trouble formatting the required data array ($data) to feed into Highcharts. The embedded php code outputs a datetime/decimal data array of the form

[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]

I am currently manually copying this data (in []) into the series: data: field which successfully generates the chart (see below). I have tried to replace the data field with series: [{ data: [] however the chart is no longer created. (as per http://www.highcharts.com/docs/working-with-data/preprocessing-data-from-a-database)

I have also tried many iterations of json_encode($data) in the php and JS but to no avail. I would be very grateful to anyone who could show me the exact translation of of the php output into the Highcharts data field, preferably using 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>

EDIT:

I have rejigged the script into the following format, however the chart no longer plots at all... I am sure that the JSON format is correct. Please help!

<!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>

解决方案

Try Below Code to get records from MYSQL and convert it to highchart data.

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

$con = mysql_connect("localhost","root","");

    if (!$con) {
      die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("test", $con);
    $sql = mysql_query("SELECT unix_timestamp(DATETIMES), TEST FROM PT");
    $result['name'] = 'Sales Summary';
    while($r = mysql_fetch_array($sql)) {
    $datetime = $r['inv_dt'];
    $result['category'][] = $datetime;
    $result['data'][] = round($r['amount'],2) ;
}
print json_encode($result);

index.php

 <html>
    <body>
       <div id="container" style="height: 300px"></div>
    </body>
 </html>

I done my best it and this method is working properly. Try It.

这篇关于Highcharts PHP JSON - 正确的数据格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆