带有MySQL数据的Highcharts动态图表不会重新加载 [英] Highcharts Dynamic Chart with MySQL Data doesn't reload

查看:82
本文介绍了带有MySQL数据的Highcharts动态图表不会重新加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家晚上好

此刻,我尝试使用HighChart/HighStock,几周后我就能够在图表中显示我的MySQL数据.但是:我总是想要更多.

this moment I try to work with HighChart / HighStock and after several weeks I were able to display my MySQL-data in the charts. But: I always want more.

好吧,我尝试获得一个动态图表,该图表令人耳目一新,就像您可能从网站上获得的示例一样. http://jsfiddle.net/sdorzak/HsWF2/

Well, I try to reach a dynamic chart which is refreshing like the examples you may know from the website. http://jsfiddle.net/sdorzak/HsWF2/

我以示例代码为指导.它不起作用,但认为问题是缺少y轴,因为y轴数据来自我的MySQL数据库,而x轴应该是当前时间.

I used the sample code as a guide. It doesn't work, but think the problem is the missing y-axis because y-axis data come from my MySQL database while x-axis should be the current time.

  <!DOCTYPE HTML>
  <html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>POS RESULT</title>

    <script type="text/javascript"   src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script src="http://code.highcharts.com/highcharts.js"></script>
    <script src="http://code.highcharts.com/modules/exporting.js"></script>

</head>

        <body>

    <?php
include "config.php";

$SQL1 =     "SELECT * FROM Prices WHERE ticker ='FB'";

$result1 = mysql_query($SQL1);
$data1 = array();
while ($row = mysql_fetch_array($result1)) {
   $data1[] = $row['Close'];
}


?>

<script type="text/javascript">
$(function () {
    var chart;
    $(document).ready(function() {
        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                type: 'line',
                marginRight: 130,
                marginBottom: 25,
                events: {
                  load: function() 
                  {

                     var series = this.series[0];
                     setInterval(function() {
                     var x = (new Date()).getTime();
                     series.addPoint([x], true);
                  }, 1000);
            },
            title: {
                text: 'Monthly Average Temperature',
                x: -20 //center
            },
            subtitle: {
                text: 'Source: WorldClimate.com',
                x: -20
            },
            xAxis: {
                                type: 'datetime',
                tickPixelInterval: 150

                //categories: [<?php echo join(',', $data2); ?>],
            },
            yAxis: {
                title: {
                    text: 'Temperature (°C)'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                formatter: function() {
                        return '<b>'+ this.series.name +'</b><br/>'+
                        this.x +': '+ this.y +'°C';
                }
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'top',
                x: -10,
                y: 100,
                borderWidth: 0
            },
            series: [{
                name: 'Tokyo',
                data: [<?php echo join(',', $data1);  ?>]
            }]
        });
    });

});
    </script>

<div id="container" style="min-width: 500px; height: 400px; margin: 0 auto"></div>

</body>
   </html>

我正在谈论的段落以及示例中显示的段落是

The passage I am talking about and which is shown in the samples is

    chart: {
        renderTo: 'container',
        type: 'spline',
        marginRight: 10,
        events: {
            load: function() {

                // set up the updating of the chart each second
                var series = this.series[0];
                setInterval(function() {
                    var x = (new Date()).getTime(), // current time
                        y = Math.random();
                    series.addPoint([x, y], true, true);
                }, 1000);
            }
        }

但是,正如我已经写过的,我不需要随机的y轴,而只是x轴.

But, as I already wrote, I don't need a random y-axis, just x-axis.

也许您可以提供帮助. 感谢您的帮助.

Maybe you can help. Thanks for your help.

live-server-data.php

live-server-data.php

<?php
// Set the JSON header
header("Content-type: text/json");


include "config.php";

$SQL1 =     "SELECT * FROM Prices WHERE Ticker ='TSLA'";

$result1 = mysql_query($SQL1);

while ($row = mysql_fetch_array($result1)) {
   $data1[] = $row['Close'];
}

// The x value is the current JavaScript time, which is the Unix time multiplied 
// by 1000.
$x = time() * 1000;
// The y value is a random number
$y = $data1;

// Create a PHP array and echo it as JSON
$ret = array($x, $y);
echo json_encode($ret);
?>

"Close"值为十进制数24,4.

The value "Close" is decimal 24,4.

推荐答案

查看该解决方案: http://www.highcharts.com/studies/live-server.htm

    var chart; // global

    /**
     * Request data from the server, add it to the graph and set a timeout to request again
     */
    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

                // add the point
                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: []
            }]
        });     
    });

这篇关于带有MySQL数据的Highcharts动态图表不会重新加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆