Highcharts使用json添加指向折线图

时间:2015-05-14 08:48:57

标签: javascript php mysql json highcharts

我需要从我从mysql数据库获取的数据中添加点。在这个时候,我通过json获取数据,但我不知道为什么在输出数组中,数据有双引号(""),如下所示:

["{name:'Chip 3',data:[[moment('2015-05-14 13:26:21','HH:mm:ss').valueOf(),29],[moment('2015-05-14 13
:26:51','HH:mm:ss').valueOf(),29],[moment('2015-05-14 13:27:21','HH:mm:ss').valueOf(),29],[moment('2015-05-14
 13:27:51','HH:mm:ss').valueOf(),29],[moment('2015-05-14 13:28:21','HH:mm:ss').valueOf(),29],[moment('2015-05-14
 14:42:54','HH:mm:ss').valueOf(),32],]}"]

因此,Highcharts无法访问数据并在图表上显示数据。现在我需要从数组中删除双引号或者执行其他操作以使Highcharts可以对数据进行重新调整。

这是我在data.php文件中的代码,用于获取和更新系列数据。

<?php

header("Content-type: text/json");


 include_once 'include/connection.php';
 $db = new DB_Class(); 



     $query = "select distinct idchip from datatable ";
     $result = mysql_query( $query );
     $rows = array();
     $count = 0;
     $getall = array();
     while( $row = mysql_fetch_array( $result ) ) {

         $table = array();

         $query2 = "select datetime,temperature from datatable where idchip=".$row['idchip'].' group by datetime ';

            $dataresult = mysql_query($query2);
            while($datarow = mysql_fetch_array($dataresult))
            {

                $data = '';


                $datatimes .= $datarow['0'].',';


                $data .= "[moment('".$datarow['0']."','HH:mm:ss').valueOf(),".(integer)$datarow['1']."],";
                $stringdata .= $data;

                }

        $newstring = $stringdata ;

        $stringdata = '';
        $stringtime = '';

        $namedata = "{name:'Chip ".$row["idchip"]."',data:[$newstring]}";
        $getall[] = $namedata;


     }


echo json_encode($getall);
?>

这是我用来获取ajax返回数据的代码。

function getData() {
        jQuery.ajax({
            url: 'data.php',
            type: 'GET',
            dataType: 'json',
            mimeType: 'multipart/form-data',
            contentType: false,
            cache: false,
            processData: false,
            success: function( data, jqXHR ) {
                if( data == "null" ) {

                } else {
                  $.getJSON("data.php", function(json) {

            chart = new Highcharts.Chart({
               chart: {
                renderTo: 'container',
                defaultSeriesType: 'spline',
                events: {
               //     load: requestData
                }
            },
 series: json

            });
        });
                }
            },
            error: function( textStatus ) {
                console.log(" error. damm. ");
                //console.log(error);
            }
        });
    } 

1 个答案:

答案 0 :(得分:0)

我觉得这样的事情

    $query = "select distinct idchip from datatable ";
    $result = mysql_query( $query );
    $rows = array();
    $count = 0;
    $getall = array();
    while( $row = mysql_fetch_array( $result ) ) {

        $table = array();
        $stringdata = array();
        $query2 = "select datetime,temperature from datatable where idchip=".$row['idchip'].' group by datetime ';

        $dataresult = mysql_query($query2);

        $stringdata = array();
        while($datarow = mysql_fetch_array($dataresult))
        {
            $stringdata[] = "moment('".$datarow['0']."','HH:mm:ss').valueOf(),".(integer)$datarow['1'];
        }

        $namedata['name'] = "Chip ".$row["idchip"];
        $namedata['data'] =  $stringdata;
        $getall[] = $namedata;
    }

    echo json_encode($getall);