weixin_33721344 2017-03-14 14: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 求指导ADS低噪放设计
    • ¥15 CARSIM前车变道设置
    • ¥50 三种调度算法报错 有实例
    • ¥15 关于#python#的问题,请各位专家解答!
    • ¥200 询问:python实现大地主题正反算的程序设计,有偿
    • ¥15 smptlib使用465端口发送邮件失败
    • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
    • ¥15 对于squad数据集的基于bert模型的微调
    • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
    • ¥20 steam下载游戏占用内存