在 PHP 中,我查询了我的 mySQL 数据库并生成了一个 JSON 对象,格式为 Google Chart API 需要接收,以便随着时间的推移制作水平线,显示名称 onHover,如下所示:
$conn = mysql_connect("x","y","z");
mysql_select_db("a",$conn);
$sql = "SELECT year,d_mayor,d_council1,d_council2,d_council3
FROM metrics WHERE year
IN ('1998','1999','2000','2001','2002','2003','2004','2005','2006','2007','2008','2009','2010','2011','2012')";
$sth = mysql_query($sql, $conn) or die(mysql_error());
//start the json data in the format Google Chart js/API expects to receive it change
$JSONdata = "{
\"cols\": [
{\"label\":\"Year\",\"type\":\"string\"},
{\"label\":\"City Council 1\",\"type\":\"number\"},
{\"label\":\"City Council 2\",\"type\":\"number\"},
{\"label\":\"City Council 3\",\"type\":\"number\"},
{\"label\":\"Mayor\",\"type\":\"number\"}
],
\"rows\": [";
//loop through the db query result set and put into the chart cell values (note last ojbect in array has "," behind it but its working)
while($r = mysql_fetch_assoc($sth)) {
$JSONdata .= "{\"c\":[{\"v\": " . $r['year'] . "}, {\"v\": 1, \"f\": \"" . $r['d_council1'] . "\"}, {\"v\": 2, \"f\": \"" . $r['d_council2'] . "\"}, {\"v\": 3, \"f\": \"" . $r['d_council3'] . "\"}, {\"v\": 10, \"f\": \"" . $r['d_mayor'] . "\"},]},";
}
//end the json data/object literal with the correct syntax
$JSONdata .= "]}";
echo $JSONdata;
mysql_close($conn);