donglin9717 2015-05-31 10:54
浏览 96

使用highcharts绘制mysql数据

I'm trying to plot data from a mysql table on document load with highcharts: the html looks like that:

function FetchData(){
        //alert("Fetching");
        $.ajax({
            url: 'php/reports/fetch_data.php',
            success: function(data) {
                dataTemp = [];
                for(i=0;i<data.length;i++){
                    dataTemp.push(data[i][0]);  // '1' index for getting that temp value. '0' for date.     
                }

                c_temperature.series[0].data = dataTemp;
                for(i=0;i<data.length;i++){
                    dataTemp.push(data[i][1]);  // '1' index for getting that temp value. '0' for date.     
                }
                c_temperature.series[1].data = dataTemp;
                }

            });

function DrawCharts(){
        c_temperature = new Highcharts.Chart({
            chart: {
                renderTo: 'dashboard',
                defaultSeriesType: 'spline',
            },
            title: {
                text: 'Temperatur'
            },
            xAxis: {
                type: 'datetime',
                tickPixelInterval: 150,
                maxZoom: 20 * 1000
            },
            yAxis: {
                minPadding: 0.2,
                maxPadding: 0.2,
                title: {
                    text: 'Value',
                    margin: 10
                }
            },
                legend:{
                    enabled: false
                },
                credits:{
                    enabled: false
                },
                series: [{
                    name: 'Temperatur',
                    data: []
                }]
        });

$(document).ready(function() {
        DrawCharts();
        FetchDevices();
        FetchData();



    });

<body>
<div id="dashboard">
</div>
<div class="clear"></div>
</body>

And the php I call looks like that:

try {
    $con = new PDO("mysql:host=$servername; dbname=$dbname", $username, $password);#
    echo 'Connected</br>';
    $sql = "select ZEIT,FEUCHTE,TEMPERATUR,LUX,PITCH from ".$mac.
    " order by ID";

    foreach($con - > query($sql) as $row) {
        $x = $row['ZEIT'];
        /*$x = mktime()*1000;*/
        $y_h = (float) $row['FEUCHTE'];
        /*$y_t=(float)$row['TEMPERATUR'];
        $y_l=(float)$row['LUX'];
        $y_a=(float)$row['PITCH'];*/
        $ret = array($x, $y_h, /*$y_t,$y_l,$y_a,$mac*/ );
        echo json_encode($ret);
    }
    $con = null;
}

The php code successfully returns data. But I dont see a graph and debugging with the browser console does not give a clue either. Any suggestions what I'm doing wrong?

Beste Regards

  • 写回答

3条回答 默认 最新

  • douxiuyi6529 2015-05-31 11:07
    关注

    You want to use c_temperature.series[0].setData(dataTemp, true); and c_temperature.series[1].setData(dataTemp, true);. By setting the data, you're not actually telling highcharts to redraw the chart, so nothing is happening when you update the data.

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料