2013-10-09 73 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