简体   繁体   English

jqplot replot对条形图不起作用

[英]jqplot replot not working for bar chart

I am taking graph plot points from database and replotting graph after every 10 seconds so that any change in database is reflected on graph. 我从数据库中获取图形绘图点,并每隔10秒重新绘制图形,以便数据库中的任何更改都反映在图形上。 But in my case this is not working and graph changes only when I reload the page. 但是在我的情况下,这是行不通的,仅当我重新加载页面时图形才会更改。 Following is my code 以下是我的代码

 <?php
   $z_data = json_encode($this->testData('123'));
  ?>

javascript code JavaScript代码

$(document).ready(function(){
    $.jqplot.config.enablePlugins = true;
    var s2 = $z_data;
    var plot5 = $.jqplot('big', [s2], {
        seriesDefaults:{
            renderer:$.jqplot.BarRenderer,
        },
        axes: {
            xaxis: {
                renderer: $.jqplot.CategoryAxisRenderer,
            }
        },
        highlighter: { show: false }
    });
    $('a[href="#dash4"]').on('shown', function(g) {
        plot5.replot();
    });

    setInterval(function () {
        var s4 = [];
        var toi = $z_data
        for (var k=0; k<11; k++){ 
            s4.push([k, toi[k]]);
        }


        $('#big').unbind();
        plot5.destroy();
        plot5.series[0].data = s4;
        plot5.resetAxesScale();
        plot5.replot(true);
        }, 
        10000
    );

    $.jqplot.config.enablePlugins = false;

});

Where I am making mistake? 我在哪里犯错? Also database gets updated every 5 seconds so the change should be reflected on graph since I updating it every 10 seconds. 此外,数据库每5秒更新一次,因此更改应该反映在图形上,因为我每10秒更新一次。

Here is the solution: JsFiddle link 解决方法如下: JsFiddle链接

In the example i have assumed dummy data to prove the code concept. 在示例中,我假设使用伪数据来证明代码概念。

$(document).ready(function(){
    $.jqplot.config.enablePlugins = true;
    var s2 = [1,2,4,5,7,23,4,40,7,3,8];
    var plot5 = $.jqplot('big', [s2], {
        seriesDefaults:{
            renderer:$.jqplot.BarRenderer,
        },
        axes: {
            xaxis: {
                renderer: $.jqplot.CategoryAxisRenderer,
            }
        },
        highlighter: { show: false }
    });
    $('a[href="#dash4"]').on('shown', function(g) {
        plot5.replot();
    });

    setInterval(function () {
        var s4 = [];
        s2.shift();
        var toi = s2;
        toi.push(Math.round(Math.random()*10));
        for (var k=0; k<11; k++){ 
            s4.push([k+1, toi[k]]);
        }

        plot5.destroy();
        plot5.series[0].data = s4;
        plot5.replot(true);
        }, 
        10000
    );

});

您必须将plot5定义为全局变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM