dsh84723 2017-04-23 14:20
浏览 36
已采纳

来自MySQL的高清实时数据

I found this Highchart live data example Live data. I try to use my own data which is come from MySQL so, I change the $y in live-server-data.php to receive the data after use function fetch_assoc().

HTML Code

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Highcharts Example</title>

        <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.js"></script>
        <script type="text/javascript" src="http://code.highcharts.com/highcharts.js"></script>

    </head>
    <body>
        <div id="container" style="width: 800px; height: 400px; margin: 0 auto"></div>
    </body>
</html>

JS

 var chart; // global
            
            function requestData() {
                $.ajax({
                    url: 'live-server-data.php', 
                    success: function(point) {
                        var series = chart.series[0],
                            shift = series.data.length > 20; // shift if the series is longer than 20
            
                        chart.series[0].addPoint(eval(point), true, shift);
                        
                        // call it again after one second
                        setTimeout(requestData, 1000);  
                    },
                    cache: false
                });
            }
                
            $(document).ready(function() {
                chart = new Highcharts.Chart({
                    chart: {
                        renderTo: 'container',
                        defaultSeriesType: 'spline',
                        events: {
                            load: requestData
                        }
                    },
                    title: {
                        text: 'Live random data'
                    },
                    xAxis: {
                        type: 'datetime',
                        tickPixelInterval: 150,
                        maxZoom: 20 * 1000
                    },
                    yAxis: {
                        minPadding: 0.2,
                        maxPadding: 0.2,
                        title: {
                            text: 'Value',
                            margin: 80
                        }
                    },
                    series: [{
                        name: 'Random data',
                        data: []
                    }]
                });     
            });

PHP code

<?php 

$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error){
    die("Connection failed: ".$conn->connect_error);
}

$sql_1 = "SELECT SensorData AS power FROM $tbname where SensorID = '1'";

$result_1=$conn->query($sql_1);
while($row = $result_1->fetch_assoc()){
    $y = $row['power'];
}

$conn->close();

 header("Content-type: text/json");
 multiplied by 1000.
 $x = time() * 1000;
 $ret = array($x, $y);
 echo json_encode($ret);
?>


The graph is moved but it not shows any data.

enter image description here

So I press F12 on Chrome Browser and I found this thing.

enter image description here

I think "14.600" may be the cause of my problem. Please tell me if you know the solution to solve this problem. Thank you so much.

</div>
  • 写回答

1条回答 默认 最新

  • doudeng9425 2017-04-23 14:29
    关注

    I think the graph required an array that contains numeric values in it. In your case one value is numeric but another one is string so convert all the values to numeric i.e. either integer or float. (Float is preferred here) and pass that array to the graph.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘