简体   繁体   English

将JSON数据输出到Chart.js标签和数据

[英]Outputting JSON data to Chart.js labels and data

I'm tring to output JSON data to a Chart using Chart.js , querying a MySQL database for the data. 我使用特林输出JSON数据图表Chart.js ,查询数据MySQL数据库。

Basically what I did to query was (data.php): 基本上我要查询的是(data.php):

<?php

header('Content-Type: application/json');

$con = //Database connection

// Check connection
if (mysqli_connect_errno($con))
{
    echo "Failed to connect to DataBase: " . mysqli_connect_error();
}
else
{
    $data_points = array();
    $result = mysqli_query($con, "SELECT * FROM condicao order by id desc limit 10");

    while($row = mysqli_fetch_array($result))
    {        
        $point = array($row['timestamp'], $row['temperatura']);
        array_push($data_points, $point);        
    }

    echo json_encode($data_points, JSON_NUMERIC_CHECK);

}

mysqli_close($con);

?>

Which gave me the following array ( timestamp , temperature ): 这给了我以下数组( timestamptemperature ):

[
    ["2014-08-04 23:06:01",16.9],
    ["2014-08-04 23:03:02",17.1],
    ["2014-08-04 23:00:02",17.1],
    ["2014-08-04 22:57:01",17.1],
    ["2014-08-04 22:54:01",17.1],
    ["2014-08-04 22:51:02",17.1],
    ["2014-08-04 22:48:01",17.2],
    ["2014-08-04 22:45:02",17.2],
    ["2014-08-04 22:42:01",17.2],
    ["2014-08-04 22:39:02",17.2]
]

Now I'm trying to output this to a Chart, but I don't know how to take the first object as the label (timestamp) and the second object as the datapoints (temperature). 现在我试图将其输出到图表,但我不知道如何将第一个对象作为标签(时间戳),将第二个对象作为数据点(温度)。

Here's the code in which I'm trying to output the Chart: 这是我正在尝试输出图表的代码:

<script type="text/javascript">
$(document).ready(function() {

    $.getJSON("data.php", function (result) {

        var tempData = {
            labels : result,
            datasets : [{
                fillColor : "rgba(172,194,132,0.4)",
                strokeColor : "#ACC26D",
                pointColor : "#fff",
                pointStrokeColor : "#9DB86D",
                data : result
            }]
        }

        var temp = document.getElementById('compradores').getContext('2d');
        new Chart(temp).Line(tempData);
    });
});
</script>

How can I identify result as the first object for the label (timestamp), and the second object on the array as the datapoints (temperature)? 如何将结果标识为标签的第一个对象(时间戳),将数组中的第二个对象标识为数据点(温度)?

In the way I'm doing above, I'm getting both timestamp and temperature as the labels. 按照我上面的方式,我将时间戳和温度都作为标签。

I've already tried result[0] , tried defining a label for the timestamp and calling result['timestamp'] and so on, with no luck. 我已经尝试过result[0] ,尝试为时间戳定义一个标签并调用result['timestamp']等等,没有运气。

Thanks! 谢谢!

The issue is you have to provide timeStamp/temperature in separate collection as in labels and dataset associated with it. 问题是你必须在单独的集合中提供timeStamp / temperature,就像在与之关联的标签和数据集中一样。

Either you can respond back "result" separately into two two arrays like: 您可以将“结果”分别回复到两个两个数组中,例如:

     [["16.9", "17.1", "17.1", "17.1", "17.1", "17.1", "17.2", "17.2", "17.2", "17.2"],                                                                  
      ["2014-08-04 23:06:01", "2014-08-04 23:03:02", "2014-08-04 23:00:02", "2014-08-04 22:57:01", "2014-08-04 22:54:01", "2014-08-04 22:51:02", "2014-08-04 22:48:01", "2014-08-04 22:45:02", "2014-08-04 22:42:01", "2014-08-04 22:39:02"]];

And then you can simply do, 然后你可以简单地做,

    var tempData = {
        labels : result[0],
        datasets : [{
            fillColor : "rgba(172,194,132,0.4)",
            strokeColor : "#ACC26D",
            pointColor : "#fff",
            pointStrokeColor : "#9DB86D",
            data : result[1]
        }]
    };

OR you can convert it into two arrays on front-end side. 或者你可以将它转换成前端的两个数组。

For example: 例如:

     <script type="text/javascript">
     $(document).ready(function() {

     $.getJSON("data.php", function (result) {

         var labels = [],data=[];
         for(var item in result){
              labels.push(result[item].slice(0,1).toString());
              data.push(result[item].slice(1,2).toString());
          }

    var tempData = {
        labels : labels,
        datasets : [{
            fillColor : "rgba(172,194,132,0.4)",
            strokeColor : "#ACC26D",
            pointColor : "#fff",
            pointStrokeColor : "#9DB86D",
            data : data
        }]
    };

    var temp = document.getElementById('compradores').getContext('2d');
    var lineChart = new Chart(temp).Line(tempData);

     });
 });
 </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM