0

出于某种原因,以下代码未提供预期的谷歌散点图。JSON url 具有两个表变量的数字输出。任何线索(顺便说一下,JSON 确实适用于饼图,包括一个字符串变量而不是散点图的数字变量)。

$(function() { 
     // when document loads, grab the json
     $.getJSON(jsonurl, function(data) {
         // once grabbed, we run this callback

         // setup the new table and its data
         var data = new google.visualization.DataTable();
             data.addRows(data.rows.length);  // length gives us the number of results in our returned data
             data.addColumn('number', 'meterprijs');
             data.addColumn('number', 'perceeloppervlak');

         // now we need to build the map data, loop over each result
         $.each(data.rows, function(i,v) {
             // set the values for both the name and the population
             data.setValue(i, 0, v.a1_meterprijs);
             data.setValue(i, 1, v.a1_buitenopp);
         });
         // finally, create the map!

var options = {
      title: 'Age vs. Weight comparison',
      hAxis: {title: 'Meterprijs', minValue: 1500, maxValue: 6000},
      vAxis: {title: 'Perceelopp', minValue: 10, maxValue: 1000},
      legend: 'none'
    };        


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

          });
        });
       });
      });

4

1 回答 1

0

好的,这是正在运行的代码。初始设置很好,但我改变了:

1)图表地图命名为'mapje'而不是数据,这是为了合理(图表不是数据,而是数据本身) 2)表格需要填充数字,所以当json解析数字时作为字符串(在 " 和 " 之间),这些必须通过在变量前添加 + 来读取为数字(例如 v.a1_meterprijs)。

所以毕竟最初的代码可能对某些人有用,但这个对我有用:

$(function() { 
     // when document loads, grab the json
     $.getJSON(jsonurl, function(data) {
         // once grabbed, we run this callback

         // setup the new table and its data
         var mapje = new google.visualization.DataTable();
             mapje.addRows(data.rows.length);  // length gives us the number of results 
             in our returned data
             mapje.addColumn('number', 'Perceelopp');
             mapje.addColumn('number', 'Meterprijs');

         // now we need to build the map data, loop over each result
         $.each(data.rows, function(i,v) {
             // set the values for both the name and the population
             mapje.setValue(i, 0, + v.a1_buitenopp);
             mapje.setValue(i, 1, + v.a1_meterprijs);
         });
         // finally, create the map!

var options = {
      title: 'Meterprijs vs Perceeloppervlak',
      hAxis: {title: 'Perceelopp', minValue: 10, maxValue: 1000},
      vAxis: {title: 'Meterprijs', minValue: 1500, maxValue: 6000},
      legend: 'none'
    };  



         var chart = new google.visualization.ScatterChart(
           document.getElementById('visualization2'));
              chart.draw(mapje, options);

          });
        });
       });
      });

于 2012-10-18T19:37:44.380 回答