javascript - Highcharts 使用 json 将点添加到折线图

标签 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);
            }
        });
    } 

最佳答案

我觉得是这样的

    $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);

关于javascript - Highcharts 使用 json 将点添加到折线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30233041/

相关文章:

Javascript 函数检查偶数/奇数

php - htmlentities() 与 htmlspecialchars()

php - 如何使用与表单输入和验证相同的脚本将 PHP 表单输入保存在 MySQL 数据库中

mysql - 连接表问题

javascript - 如何将所有选定的文本包装到span元素中?

javascript - JavaScript 函数之间的区别

javascript - 在 AngularJS 上模拟提交和验证到单独的表单

php - MYSQL 查询中的两次计数

php - 如何获取未在逗号分隔字符串的第一个或最后一个索引但在 MySQL 中的字符串中间索引的值

MySQL - 多表选择