使用数据库数据使用chart.js制作图表

时间:2017-05-03 07:32:09

标签: javascript php html chart.js

所以,我认为标题解释了我想做的事情。我有一个由|组成的数据库tempLog |锅|森|我想使用存储在tempLog和pot中的数据制作图表。所以我做了一个草图,正如我预期的那样,它没有用,我的问题是如何在查询中放入一个列然后在我的图表中使用它?我应该使用阵列吗?那么我如何在我的图表中使用它?请记住,我使用的是PHP 5.6.30,现在我不会将其升级到7.感谢您的帮助:)

这是我的草图,如果你想看看我是一个坏程序员。 (也是一个糟糕的英语演讲者)

    <?php
        import("connect.php");
        $link=Connection();//get connection to the db 
    //here i create my arrays
        $record=mysql_query("SELECT `tempLog` FROM `Tabella1` ORDER BY `tempLog`",$link);
        $datax = array();
    while($row=mysql_fetch_assoc($record)){
        $datax[0] = $row;
    }
    $record=mysql_query("SELECT `potenza` FROM `Tabella1` ORDER BY `tempLog`",$link);
    $datay = array();
    while($row=mysql_fetch_assoc($record)){
        $datay[0] = $row;
    }
    ?>


<html>
<head>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.bundle.js"></script>
</head>
<body>
    <canvas id="grafico-linea"></canvas>
    <script>
    var ctx = document.getElementById('grafico-linea').getContext('2d');
    var myChart = new Chart(ctx, {
        type: 'line',
        data: {
            labels: <?=json_encode(array_values($datax));?>,//put there the arrays
            datasets: [{
                label: 'Potenza',
                data: <?=json_encode(array_values($datay));?>,//end there
                backgroundColor: "rgba(41,128,185,0.8)"
            }]
        }
    });
    </script>
</body>
</html> 

0 个答案:

没有答案