2017-02-22 49 views
1

即時檢索使用thingspeak API的數據。我想在融合圖表中顯示數據。如何在圖表中顯示動態數據?此代碼適用於其他第三方apis,但不適用於此api。無法從API獲取數據以顯示圖表中的數據

樣本數據 172.70

API

https://api.thingspeak.com/channels/23037xxx/fields/field1/last?api_key=EG628M4J9SP76xxx

<script> 
     FusionCharts.ready(function() { 
      LoadChart(); 
     }); 
     function LoadChart() { 
      $.ajax({ 
       url: 'https://api.thingspeak.com/channels/230xxx/fields/field1/last?api_key=EG628M4J9SP76xxx', // local address 
       type: 'GET', 
       crossDomain: true, 
       success: function (data) { 
        if (data.success) { 
         var chlorine = data; 
         var phfusioncharts = new FusionCharts({ 
          type: 'angulargauge', 
          renderAt: 'ph-container', 
          width: '450', 
          height: '300', 
          dataFormat: 'json', 
          dataSource: { 
           "chart": { 
            "caption": "Chlorine ", 
            "lowerLimit": "0", 
            "upperLimit": "14", 
            "showValue": "1", 
            "valueBelowPivot": "1", 
            "theme": "fint" 
           }, 
           "colorRange": { 
            "color": [{ 
             "minValue": "0", 
             "maxValue": "5", 
             "code": "#6baa01" 
            }, { 
             "minValue": "5", 
             "maxValue": "10", 
             "code": "#f8bd19" 
            }, { 
             "minValue": "10", 
             "maxValue": "14", 
             "code": "#e44a00" 
            }] 
           }, 
           "dials": { 
            "dial": [{ 
             "value": chlorine 
            }] 
           } 
          } 
         }); 

         phfusioncharts.render(); 
        } 
       } 
      }); 
     } 
    </script> 
</head> 
<body> 

    <table class=""> 
     <tr> 
      <td><div id="ph-container" style="float:left;"></div></td> 

     </tr> 
    </table> 
+0

你能不能在 「_this代碼不working_」 更具體?你會得到什麼錯誤?請編輯您的問題並添加更多信息。 – Lucky

+0

即時通訊不會從console.its中顯示任何錯誤,不會顯示任何圖表。 – Swapna

+0

請在jsfiddle.net重新創建您的代碼並在此處發佈鏈接。 – Lucky

回答

1

你並不需要檢查,如果data.success。你的api只響應一個浮點值。所以data.success是未定義的。它從不進入if條件。

檢查演示:http://jsfiddle.net/x5FBh/1201/

JS:

FusionCharts.ready(function() { 
    LoadChart(); 
}); 

function LoadChart() { 
    $.ajax({ 
    url: 'https://api.thingspeak.com/channels/230372/fields/field1/last?api_key=EG628M4J9SP769UT', // local address 
    type: 'GET', 
    crossDomain: true, 
    success: function (data) { 
     console.log('xhr success') 
     //if (data.success) { 
     console.log("success"); 
     var chlorine = data; 
     var phfusioncharts = new FusionCharts({ 
      type: 'angulargauge', 
      renderAt: 'ph-container', 
      width: '450', 
      height: '300', 
      dataFormat: 'json', 
      dataSource: { 
      "chart": { 
       "caption": "Chlorine ", 
       "lowerLimit": "0", 
       "upperLimit": "14", 
       "showValue": "1", 
       "valueBelowPivot": "1", 
       "theme": "fint" 
      }, 
      "colorRange": { 
       "color": [{ 
       "minValue": "0", 
       "maxValue": "5", 
       "code": "#6baa01" 
       }, { 
       "minValue": "5", 
       "maxValue": "10", 
       "code": "#f8bd19" 
       }, { 
       "minValue": "10", 
       "maxValue": "14", 
       "code": "#e44a00" 
       }] 
      }, 
      "dials": { 
       "dial": [{ 
       "value": "22" 
       }] 
      } 
      } 
     }); 

     phfusioncharts.render(); 
     //} 
    } 
    }); 
} 
+0

謝謝,它爲我工作... – Swapna

+0

可以請你告訴我如何刷新每30秒的圖表 – Swapna

+0

不客氣。你只需要在javascript中使用'setInterval()'方法,並且每'n'秒調用'LoadChart()'方法。請參閱http://stackoverflow.com/questions/21638574/run-a-function-every-30-seconds-javascript它就像'setInterval(function(){LoadChart()},30000)'Don忘了查看我答案左側的刻度線以接受它。 – Lucky

相關問題