2017-02-22 10 views
1

im thingspeak apiを使用してデータを取得しています。私は融合図にデータを表示したい。グラフに動的データを表示する方法は?このコードは他のサードパーティのapisでは動作しますが、このapiでは動作しません。あなたはdata.successかどうかを確認する必要はありませんグラフのデータを表示するために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

imはconsole.itsから何のエラーも得ておらず、グラフを表示していません。 – Swapna

+0

jsfiddle.netでコードを再作成し、ここにリンクを投稿してください。 – Lucky

答えて

1

。あなたの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)'のようになります。それを受け入れるために私の答えの左に目盛りをチェックすることを忘れないでください。 – Lucky

関連する問題