简体   繁体   English

Google Gauge图表未显示

[英]Google Gauge chart not displaying

I want to create and display a google gauge chart on a webpage for a remote monitoring program I am developing. 我想为正在开发的远程监控程序在网页上创建并显示Google轨距图。 I am using an eWON Flexy 201 to do this. 我正在使用eWON Flexy 201来执行此操作。 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. 基本上,我要做的是在服务器上使用一种VBScript形式来获取油温并将该值返回到仪表以显示温度。 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 这是gauge.shtm文件

<%#TagSSI,Oil_Temp%>

In your scenario i would advise you to do the following rearrangement and see if it works for you. 在您的情况下,我建议您进行以下重新排列,看看是否适合您。 The google.setOnLoadCallback() set up a callback function to execute when Google Visualization API has loaded. google.setOnLoadCallback()设置了一个回调函数,以在加载Google Visualization API后执行。 But you can omit this here as your result depends upon the success of an ajax call. 但是您可以在此忽略它,因为您的结果取决于ajax调用的success

<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>

If you have notice little modification, i am calling refreshData() and drawChart() will execute as a callback function on success eg done(function(result) { }) . 如果您几乎没有修改,我将调用refreshData()并且drawChart()将在成功时作为回调函数执行,例如done(function(result) { }) The result of the ajax success passed on to drawChart() function which is used as a callback. Ajax成功的结果传递给drawChart()函数,该函数用作回调。 Let me know the result if it did not work. 让我知道结果是否无效。

More to read on callback functions. 有关回调函数的更多信息,请阅读。

I figured out my problem. 我发现了我的问题。 I'm not particularly sure what was going wrong with the answer @Rohit416 was giving me, however upon more searching on the web I came across a solution and amended it to my needs. 我不是特别确定@Rohit416给我的答案@Rohit416了什么问题,但是在网上进行更多搜索后,我遇到了一个解决方案并将其修改为我的需要。

Here is the solution that ended up working for me 这是最终为我工作的解决方案

<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});

and my oil.shtm file is as follows 我的oil.shtm文件如下

This successfully updates my gauge every second, and I tested this method with multiple gauges at once and it works as well. 这样成功地每秒更新一次量规,并且我一次用多个量规测试了此方法,并且效果很好。

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

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