Highchart - 显示JSON数据 - MYSQL / PHP [英] Highchart - Display JSON Data - MYSQL / PHP

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

问题描述

我正尝试在Highchart中显示JSON数据( http://highcharts.com



问题是xAxis的数据。我真的很困惑如何获取日期格式,以便它显示在xAxis中。



我发现我必须将日期数据从数据库转换为毫秒。



这是我的javascript:

  var chart; 
$ b $图表= new Highcharts.Chart({
图表:{
renderTo:'container',
defaultSeriesType:'spline',
events:{
load:requestData
}},
xAxis:{
type:'datetime'
},
yAxis:{
title:
text:'Value'
},
plotLines:[{
value:0,
width:1,
color:'#808080'
}]
},

系列:[{
name:'Random data',
data:[]
}]
});



函数requestData(){
$ .ajax({
url:'../controller/charter/data.php',
datatype:json,
success:function(data){

alert(data);

chart.series [0] .setData(数据);

},
cache:false
});
}

这是生成JSON的PHP:

 <?php 
header(Content-type:text / json);
//连接到数据库
$ dbhost =localhost;
$ dbuser =cccccc;
$ dbpassword =ccccccc;
$ database =ccccccccc;
$ tablename =ccccccc;
$ db = mysql_connect($ dbhost,$ dbuser,$ dbpassword)
或die(Connection Error:。mysql_error());

mysql_select_db($ database)或死(错误连接到db。);
$ result = mysql_query(SELECT COUNT(*)AS count from $ tablename);
$ row = mysql_fetch_array($ result,MYSQL_ASSOC);

$ SQL =SELECT aed,savedate FROM $ tablename ORDER BY savedate;

$ result = mysql_query($ SQL)或die(Couldn?t execute query。。mysql_error());

$ i = 0;
while($ row = mysql_fetch_array($ result,MYSQL_ASSOC)){

$ row [aed] =(int)$ row [aed];

$ rows [$ i] = array($ row [savedate],$ row [aed]);

$ i ++;
}

echo json_encode($ rows);

;
?>

JSON数据:

  [[2011-03-20 18:53:47,40],[2011-03-21 18:53:47,300],[2011-03-22 18 :53:47,450],[2011-03-23 18:53:47,40],[2011-03-24 18:53:47,300],[2011-03-25 18:53:47,450],[2011-03-26 18:53:47,40],[2011-03-29 18:53:47,120],[2011-03- 29 18:53:47,80],[2011-03-29 18:53:47,300],[2011-03-29 18:53:47,450],[2011-03 -29 18:53:47,40],[2011-03-29 18:53:47,100],[2011-03-29 18:53:47,120],[2011- 03-29 18:53:47,80],[2011-03-29 18:53:47,300],[2011-03-29 18:53:47,450],[2011 -03-29 18:53:47,100],[2011-03-29 18:53:47,40],[2011-03-29 18:53:47,120],[ 2011-03-29 18:53:47,80],[2011-03-29 18:53:47,300],[2011-03-29 18:53:47,450],[ 2011-03-29 18:53:47,40],[2011-03-29 18:53:47,100],[2011-03-29 18:53:47,120], [2011-03-29 18:53:47,80],[2011-03-29 18:53:47,300],[2011-03-29 18:53:47,450] ,[2011-03-29 18:53:47,40],[2011-03-29 18:53:47,300],[2011-03-29 18:53:47,450 ],[2011-03-29 18:53:47,40],[2011-03-2 9 18:53:47,300],[2011-03-29 18:53:47,450],[2011-03-29 18:53:47,100]] 


解决方案

首先尝试解析数据(JSON),然后将其作为参数传递给 setData -

  chart.series [0] .setData(eval '('+ data +')')); 

更新:

  [[2011-03-20 18:53:47,40],[2011-03-21 18:53:47,300],... ] 

2011-03-20 18:53:47对于日期时间系列而言不是有效的x值。它必须以毫秒为单位表示日期。



您可以通过更改以下其中一项来修复它:


  1. Javascript(客户端)。
  2. 在您的PHP代码中

  3. 在您的SQL查询中。

在SQL查询或PHP中执行该操作,以便您不必混淆Javascript并且您的JSON将显示例如 -

  [[1318605385652,40],[1318605385652,300],...] 

然后,只要做 eval


I am trying to display JSON DATA in a Highchart (http://highcharts.com)

The problem are the data for the xAxis. I am really confused how to get the date format so that it is displayed in the xAxis.

I found out that I have to convert the date data from the DB to milliseconds.

Somebody got an idea how to realize that?

This is my javascript:

    var chart;

 chart = new Highcharts.Chart({
    chart: {
            renderTo: 'container',
            defaultSeriesType: 'spline',
            events: {
                load: requestData
            }},
    xAxis: {
         type: 'datetime'
      },
      yAxis: {
         title: {
            text: 'Value'
         },
         plotLines: [{
            value: 0,
            width: 1,
            color: '#808080'
         }]
      },

    series: [{
         name: 'Random data',
         data: []
        }]
});



function requestData() {
    $.ajax({
        url: '../controller/charter/data.php',
        datatype: "json",
        success: function(data) {

            alert(data);

            chart.series[0].setData(data);

        },
        cache: false
    });
}

This is the PHP which produces the JSON:

 <?php
header("Content-type: text/json");
// connect to the database
$dbhost = "localhost";
$dbuser = "cccccc";
$dbpassword = "ccccccc";
$database = "ccccccccc";
$tablename = "ccccccc";
$db = mysql_connect($dbhost, $dbuser, $dbpassword)
or die("Connection Error: " . mysql_error());

mysql_select_db($database) or die("Error conecting to db.");
$result = mysql_query("SELECT COUNT(*) AS count FROM $tablename");
$row = mysql_fetch_array($result,MYSQL_ASSOC);

$SQL = "SELECT aed,savedate FROM $tablename ORDER BY savedate";

$result = mysql_query( $SQL ) or die("Couldn?t execute query.".mysql_error());

$i=0;
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {

    $row[aed] = (int) $row[aed];

    $rows[$i]=array($row[savedate],$row[aed]);

    $i++;
}

echo json_encode($rows);

;
?>

The JSON data:

[["2011-03-20 18:53:47",40],["2011-03-21 18:53:47",300],["2011-03-22 18:53:47",450],["2011-03-23 18:53:47",40],["2011-03-24 18:53:47",300],["2011-03-25 18:53:47",450],["2011-03-26 18:53:47",40],["2011-03-29 18:53:47",120],["2011-03-29 18:53:47",80],["2011-03-29 18:53:47",300],["2011-03-29 18:53:47",450],["2011-03-29 18:53:47",40],["2011-03-29 18:53:47",100],["2011-03-29 18:53:47",120],["2011-03-29 18:53:47",80],["2011-03-29 18:53:47",300],["2011-03-29 18:53:47",450],["2011-03-29 18:53:47",100],["2011-03-29 18:53:47",40],["2011-03-29 18:53:47",120],["2011-03-29 18:53:47",80],["2011-03-29 18:53:47",300],["2011-03-29 18:53:47",450],["2011-03-29 18:53:47",40],["2011-03-29 18:53:47",100],["2011-03-29 18:53:47",120],["2011-03-29 18:53:47",80],["2011-03-29 18:53:47",300],["2011-03-29 18:53:47",450],["2011-03-29 18:53:47",40],["2011-03-29 18:53:47",300],["2011-03-29 18:53:47",450],["2011-03-29 18:53:47",40],["2011-03-29 18:53:47",300],["2011-03-29 18:53:47",450],["2011-03-29 18:53:47",100]]

解决方案

First of try parsing the data (JSON) before passing it as an argument to setData -

chart.series[0].setData( eval( '(' + data + ')' ) );

Update:

[["2011-03-20 18:53:47",40],["2011-03-21 18:53:47",300],...]

"2011-03-20 18:53:47" is not valid x-value for a datetime series. It must ba a number that represents the date in milliseconds.

You can fix it by making change in one of the followings -

  1. Javascript (client-side).
  2. In your PHP code
  3. In your SQL query.

Do that either in your SQL query or PHP, so that you won't have to mess with Javascript and that your JSON will look something like -

[[1318605385652, 40],[1318605385652,300],...]

Then after that just do eval.

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

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