2013-10-09 66 views
0

如何在固定时间间隔(例如10秒)后更新jqplot条形图?我编写了以下代码,其中y轴数据是随机生成的,但它在固定的时间间隔后不会更新。固定时间间隔后更新jqplot条形图

<?php 
$x = array(); 
$y = array(); 
for($i=0;$i<50; $i++) 
{ 
$x[] = $i; 
$y[] = rand(10, 200); 
} 

$x_data = json_encode($x); 
$y_data = json_encode($y); 

Yii::app()->clientScript->registerScript('chart_big'," 
$(document).ready(function(){ 
    $.jqplot.config.enablePlugins = true; 
    var s2 = $y_data; 
    var ticks = $x_data; 

    plot5 = $.jqplot('chart_big', [s2], { 
     // Only animate if we're not using excanvas (not in IE 7 or IE 8).. 
     animate: !$.jqplot.use_excanvas, 
     seriesDefaults:{ 
      renderer:$.jqplot.BarRenderer, 
      pointLabels: { show: true } 
     }, 
     axes: { 
      xaxis: { 
       renderer: $.jqplot.CategoryAxisRenderer, 
       ticks: ticks 
      } 
     }, 
     highlighter: { show: false } 
    }); 

    $('#chart1').bind('jqplotDataClick', 
     function (ev, seriesIndex, pointIndex, data) { 
      $('#info1').html('series: '+seriesIndex+', point: '+pointIndex+', data: '+data); 
     } 
    ); 
    $('a[href=\"#yw1_tab_4\"]').on('shown', function(g) { 
      if (plot5._drawCount === 0) { 
      plot5.replot(); 
     } 
    }); 


    setInterval(function() { 

     s2 = $y_data; 
     ticks = $x_data; 
     plot5.series[0].data = s2; 
     plot5.replot(); 
    }, 10000); 


}); 
"); 
?> 
+1

请参考这个帖子: [http://stackoverflow.com/questions/18535670/jqplot-animate-data-change-without-reloading-graph/18539858](http:/ /stackoverflow.com/questions/18535670/jqplot-animate-data-change-without-reloading-graph/18539858) – Gyandeep

+0

谢谢Gyandeep它给了我想法如何让它在我的情况下工作。 – prattom

回答

0

尝试$('#chart_big').unbind();plot5.destroy();,然后用新的数据图。重制();可能导致内存泄漏。

请参阅本SO Question

+0

我已经解决了我的问题,我的图形用作replot()的输入的绘图点存在一些问题。他们对代码没有问题。感谢您的回复以及关于destroy()和replot()的建议。 – prattom