2017-01-08 48 views
1

我正在使用Google Charts API開展一個小型學生項目(將樹莓派的溫度發送到服務器)。在我的服務器的HTML-Response中,返回一個計量圖。除了一件小事情之外,一切都可以正常工作:大約10秒鐘後,兩個量表中的第一個量程跳轉到其最大值(一個值爲高的值,而不是數據集的一部分)。Google Charts:Gauge-Graph在幾秒鐘後更改爲最大值

在另一個項目中,我已經有了同樣的錯誤(4格規)。奇怪的是,它總是隻發生在第一個量表圖上。

你可以在http://141.28.105.163/records下自己看到它......大約10秒後,第一個圖從大約21跳到51或類似的東西。

這是代碼:

<html> 
<head> 
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> 
    <script type="text/javascript"> 
     google.charts.load('current', {'packages':['gauge']}); 
     google.charts.setOnLoadCallback(drawChart); 
     function drawChart() { 

      var data = google.visualization.arrayToDataTable([ 
       ['Label', 'Value'], 
       ['Min', 17], 
       ['Max', 23] 
      ]); 

      var options = { 
       minorTicks: 10, 
       min: 0, 
       max: 40 
      }; 

      var chart = new google.visualization.Gauge(document.getElementById('temp_chart')); 

      setInterval(function() { 
       data.setValue(0, 1, 40 + Math.round(60 * Math.random())); 
       chart.draw(data, options); 
      }, 13000); 

      chart.draw(data, options); 
     } 
    </script> 
</head> 
<body> 
    <div id="temp_chart" style="width: 100%; height: 100%"></div> 
</body> 
</html> 
+1

40 + Math.round(60 * Math.random())。那該怎麼辦? –

+0

謝謝,整個部分是不必要的! – pixelstuermer

回答

0

謝謝,僅此而已。這是我剛纔複製&從谷歌粘貼的整體的一部分:

setInterval(function() { 
    data.setValue(0, 1, 40 + Math.round(60 * Math.random())); 
    chart.draw(data, options); 
}, 13000); 

工作完全沒有它!

+0

複製+粘貼,不理解永遠不會好...... –

+0

的確如此,但它只是一開始就要快速且骯髒地工作;-) – pixelstuermer