我正在使用 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);
};
这是我的 php 生成的 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;
现在,当我连接这两个时,我只得到一张空白地图。里面没有数据。但是..奇怪的部分来了。如果我直接浏览到我的 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,而只有 1 段可以使用 Google Charts?