weixin_33721344 2017-03-14 06:37 采纳率: 0%
浏览 18

Chart.js MySQL PHP时间刻度

I would like to draw a graph using RPi, MySQLi, PHP and Chart.js. To do that I followed this tutorial:

For now, my graph is empty. X axis is 'undefined' and Y axis has got a wrong range. In my case X axis should show time format values(HH:MM:SS). Does anybody know how to properly set received time data from MySQL for Chart.js?

I also checked this solution but it did not work for me:

Thank you!


index.php

<?php
$servername = "localhost";
$username = "airqualitysensor";
$password = "password";
$database = "temperatura";

$conn = new mysqli($servername, $username, $password, $database);

if ($conn->connect_error){
    die("Connection failed: ". $conn->connect_error);
    exit();
}

$sql = "SELECT * FROM temperaturaodczyt";
$result = $conn->query($sql);

$data = array();
foreach($result as $row){
    $data[] = $row;
    }
$result->close();
$conn->close();

print json_encode($data);
?>

app.js

$(document).ready(function(){
    $.ajax({
    url:"http://localhost/index.php",
    method: "GET",
    success: function(data) {
        console.log(data);

        var tczas = [];
        var temperatura = [];

        for (var i in data){

            tczas.push(data[i].tczas);
            temperatura.push(data[i].temperatura);

            }


        var chartdata = {
            labels: tczas,
            datasets : [
                {
                    label : "Zmierzona temperatura",
                    fill: false,
                    lineTension: 0.1,
                    backgroundColor: "rgb(200, 200, 200, 0.75)",
                    borderColor: "rgb(200, 200, 200, 0.75)",
                    hoverBackgroundColor: "rgb(200, 200, 200, 1)",
                    hoverBorderColor: "rgb(200, 200, 200, 0.75)",
                    data: temperatura
                }
                ]
            };

        var ctx = $("#mycanvas");

        var lineGraph = new Chart(ctx, {
            type: "line",
            data: chartdata
            });


        },
    error: function(data) {

        }
    });

});

展开全部

  • 写回答

0条回答 默认 最新

      编辑
      预览

      报告相同问题?

      悬赏问题

      • ¥15 PADS Logic 原理图
      • ¥15 PADS Logic 图标
      • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
      • ¥20 气象站点数据求取中~
      • ¥15 如何获取APP内弹出的网址链接
      • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
      手机看
      程序员都在用的中文IT技术交流社区

      程序员都在用的中文IT技术交流社区

      专业的中文 IT 技术社区,与千万技术人共成长

      专业的中文 IT 技术社区,与千万技术人共成长

      关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

      关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

      客服 返回
      顶部