谷歌地理图表

时间:2013-07-04 13:57:41

标签: php json google-visualization

这很难解释所以请光临我。

我使用Google的Geo Charts API,并使用json更新数据。

这是我的地图js(map.php):

 google.load('visualization', '1', {'packages': ['geochart']});
 google.setOnLoadCallback(drawRegionsMap);

  function drawRegionsMap() {

  var jsonData = $.ajax({
      url: "includes/_ajax_home_map.php?metric=Clicks",
      dataType:"json",
      async: false
      }).responseText;  

    var data = new google.visualization.DataTable(jsonData);      
    var options = {
        //region: '009',
        backgroundColor: 'EAF7FE',
        colorAxis: {colors: ['910101']}
        };

    var chart = new google.visualization.GeoChart(document.getElementById('chart_div'));
    chart.draw(data, options);
};

这是我生成的json(_ajax_home_map.php):

$data = '{
  "cols": [
        {"id":"","label":"Country","pattern":"","type":"string"},
        {"id":"","label":"Popularity","pattern":"","type":"number"}
      ],
  "rows": [';


$metric = $_REQUEST['roi_tag'];

$sql = "SELECT SUM($metric) as '$metric',tag_1 FROM Dashboard_ClientsCampaigns_Today_HoH WHERE tag_1 != '' GROUP BY tag_1";
$sql = mysql_query($sql);

$count = 1;


while($row = mysql_fetch_array($sql)){
    $tag_1 = strtoupper($row["tag_1"]);
    if($tag_1 == "UK")$tag_1 = "GB";
    if($count != 1)$data .= ",";
    $count++;
    $data .= '{"c":[{"v":"'.$tag_1.'","f":null},{"v":'.$row[$metric].',"f":null}]}';
}

$data .= ']}';

echo $data;

现在当我连接这些2时,我只得到一张空白地图。那里没有数据。但是......有一个奇怪的部分。如果我直接浏览我的PHP JSON,我会得到这个:

{ "cols": [ {"id":"","label":"Country","pattern":"","type":"string"}, {"id":"","label":"Popularity","pattern":"","type":"number"} ], "rows": [{"c":[{"v":"AU","f":null},{"v":139,"f":null}]},{"c":[{"v":"CZ","f":null},{"v":3,"f":null}]},{"c":[{"v":"DE","f":null},{"v":4,"f":null}]},{"c":[{"v":"DK","f":null},{"v":978,"f":null}]},{"c":[{"v":"ES","f":null},{"v":32,"f":null}]},{"c":[{"v":"HU","f":null},{"v":2,"f":null}]},{"c":[{"v":"IE","f":null},{"v":65,"f":null}]},{"c":[{"v":"IT","f":null},{"v":5081,"f":null}]},{"c":[{"v":"PT","f":null},{"v":4452,"f":null}]},{"c":[{"v":"RO","f":null},{"v":16,"f":null}]},{"c":[{"v":"RS","f":null},{"v":0,"f":null}]},{"c":[{"v":"RU","f":null},{"v":0,"f":null}]},{"c":[{"v":"SE","f":null},{"v":69,"f":null}]},{"c":[{"v":"GB","f":null},{"v":28123,"f":null}]}]}

如果我拿这个输出的JSON,复制它,把它放在它自己的文件(data2.php)中,然后让我的地图js访问它,它会完美地填充地图。

我怎么能拥有2个相同的JSON,只有一个可以使用Google Charts?

如果有人有任何想法可以帮助我重新开始,那就太可爱了。

由于

1 个答案:

答案 0 :(得分:1)

可能是因为您尝试使用错误的参数加载PHP

_ajax_home_map.php?metric=Clicks

但在您要查找的代码中

$metric = $_REQUEST['roi_tag'];

尝试

$metric = $_REQUEST['metric'];

...来代替。顺便说一下你是对的 - JSON工作得很好!那不是问题。