0

JSON API URL を呼び出してインタラクティブなグラフを作成できる正しいコードを送ってもらえますか?

例: URL の JSON API に基づくグラフ

http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo

Google サイトのhttps://developers.google.com/chart/interactive/docs/php_exampleにサンプルがあります

しかし、以下には何も表示されません(元のJSON PHPファイルの場所を自分のURLに置き換えました)

<html>
  <head>
    <!--Load the AJAX API-->
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript" src="jquery-1.6.2.min.js"></script>
    <script type="text/javascript">

    // Load the Visualization API and the piechart package.
    google.load('visualization', '1', {'packages':['corechart']});

    // Set a callback to run when the Google Visualization API is loaded.
    google.setOnLoadCallback(drawChart);

    function drawChart() {
      var jsonData = $.ajax({
          url: "http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo ",
          dataType:"json",
          async: false
          }).responseText;

      // Create our data table out of JSON data loaded from server.
      var data = new google.visualization.DataTable(jsonData);

      // Instantiate and draw our chart, passing in some options.
      var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
      chart.draw(data, {width: 400, height: 240});
    }

    </script>
  </head>

  <body>
    <!--Div that will hold the pie chart-->
    <div id="chart_div"></div>
  </body>
</html>
4

1 に答える 1

2

指定した URL (http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo) から返された JSON を見ると、そうではないようです。 ' DataTable JSON 形式に準拠していません。

https://developers.google.com/chart/interactive/docs/basic_preparing_dataから:

すべてのグラフにはデータが必要です。Google Chart Tools のグラフでは、データを google.visualization.DataTable という JavaScript クラスにラップする必要があります。このクラスは、前に読み込んだ Google Visualization ライブラリで定義されています。DataTable は行と列を持つ 2 次元のテーブルで、各列にはデータ型、オプションの ID、およびオプションのラベルがあります。

彼らが提供するサンプル JSON データを見ると、DataTable 形式に準拠していることがわかります。

{
  "cols": [
        {"id":"","label":"Topping","pattern":"","type":"string"},
        {"id":"","label":"Slices","pattern":"","type":"number"}
      ],
  "rows": [
        {"c":[{"v":"Mushrooms","f":null},{"v":3,"f":null}]},
        {"c":[{"v":"Onions","f":null},{"v":1,"f":null}]},
        {"c":[{"v":"Olives","f":null},{"v":1,"f":null}]},
        {"c":[{"v":"Zucchini","f":null},{"v":1,"f":null}]},
        {"c":[{"v":"Pepperoni","f":null},{"v":2,"f":null}]}
      ]
}
于 2013-01-10T21:28:30.173 に答える