0

I want to create and display a google gauge chart on a webpage for a remote monitoring program I am developing. I am using an eWON Flexy 201 to do this. I am just having trouble getting the gauge to display on the webpage.

Basically what I have to do is use a form of VBScript on the server to grab the oil temperature and return that value to the gauge to display the temperature. I have been able to return that value to the page in various ways correctly, however I am having trouble displaying and updating the gauge every second like I want to.

In the current script I am receiving no errors in the console, however nothing is rendering. I get just a blank space where the gauge should appear.

<script type='text/javascript'>
  google.load('visualization', '1', {packages:['gauge']});
  google.setOnLoadCallback(drawChart);
function drawChart() {

var x = '<%#TagSSI,Target_Pressure_Setting%>';


var data = new google.visualization.DataTable([
  ['Label', 'Value'],
  ['Oil Temp', x],
]);
var options = {
  width: 450, height: 240,
  greenFrom: 100, greenTo: 150,
  redFrom: 275, redTo: 325,
  yellowFrom:225, yellowTo: 275,
  minorTicks: 5,
  max: 350
};

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

function refreshData () {
    var json = $.ajax({
        url: 'gauge.shtm', // make this url point to the data file
        dataType: 'number',
        async: true 
    }).responseText;
    //alert(json)

    data = new google.visualization.DataTable(json);

    chart.draw(data, options);
}

refreshData();
setInterval(refreshData, 1000);
}

</script>

and here is the gauge.shtm file

<%#TagSSI,Oil_Temp%>
4

2 回答 2

0

在您的情况下,我建议您进行以下重新排列,看看它是否适合您。google.setOnLoadCallback()设置在 Google Visualization API 加载时执行的回调函数。但是你可以在这里省略它,因为你的结果取决于success调用的ajax

<script type='text/javascript'>
    google.load('visualization', '1', {packages:['gauge']});
</script>

<script type='text/javascript'>

    function drawChart(result) {

        var x = '<%#TagSSI,Target_Pressure_Setting%>';

        // this may not required here because it anyway get replaced down the line..
        var data = new google.visualization.DataTable([
          ['Label', 'Value'],
          ['Oil Temp', x],
        ]);

        var options = {
          width: 450, height: 240,
          greenFrom: 100, greenTo: 150,
          redFrom: 275, redTo: 325,
          yellowFrom:225, yellowTo: 275,
          minorTicks: 5,
          max: 350
        };

        var chart = new google.visualization.Gauge(document.getElementById('chart_div'));
        data = new google.visualization.DataTable(result);
        chart.draw(data, options);
    }

    function refreshData(callBack) {
        $.ajax({
            url: 'gauge.shtm',
            dataType: 'number',
            async: true 
        }).done(function(result) {
            callBack(result);           
        });
    }

    refreshData(drawChart);

</script>

如果您注意到很少的修改,我正在调用refreshData()并将drawChart()在成功时作为回调函数执行,例如done(function(result) { })。ajax 成功的结果传递给drawChart()用作回调的函数。如果它不起作用,请告诉我结果。

更多关于回调函数的阅读。

于 2015-11-10T17:20:09.627 回答
0

我发现了我的问题。我不太确定答案@Rohit416给我出了什么问题,但是在网上进行更多搜索后,我发现了一个解决方案并根据我的需要对其进行了修改。

这是最终为我工作的解决方案

<script type="text/javascript">

      google.setOnLoadCallback(drawChart);

      function drawChart() {

        var data = google.visualization.arrayToDataTable([
          ['Label', 'Value'],
          ['Oil', 80],
        ]);

        var options = {
          width: 400, height: 140,
          redFrom: 275, redTo: 350,
          yellowFrom:200, yellowTo: 275,
          greenFrom: 100, greenTo: 150,
          minorTicks: 5,
          max: 350
        };

        var chart = new google.visualization.Gauge(document.getElementById('chart_div'));
        chart.draw(data, options);

       function getData () {
        $.ajax({
            url: 'oil.shtm',
            success: function (response) {
                data.setValue(0, 1, response);
                chart.draw(data, options);
                setTimeout(getData, 1000);

            }
        });
    }
    getData();
}
google.load('visualization', '1', {packages: ['gauge'], callback: drawChart});

我的oil.shtm文件如下

这成功地每秒更新我的仪表,我一次用多个仪表测试了这个方法,它也能正常工作。

于 2015-11-24T21:46:12.180 回答