0

我想制作一张我所在城市的市议会成员随时间变化的图表。我设想这有点像折线图。x 轴是年。有九个市议会席位,所以会有九条直线,每条线都会随着时间的推移显示谁是市议会成员(可能通过不同颜色的线段或通过在鼠标悬停上显示他们的名字)。也许这就像一条时间线。

当我绘制城市预算时,由于年份和城市预算都是“数字”类型,这个经典的折线图效果很好。

对于这个新图表,我传递了所有数据类型“字符串”,因为它们是人名,而 Google Charts API 给出了错误:“轴 #0 的数据列不能属于字符串类型”

我怎样才能制作这个图表?(我不仅想绘制预算盈余或赤字或抢劫数量等数字数据,还想将[在另一个图表中]当时负责的人联系起来。)

4

1 回答 1

0

在 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);
于 2012-06-12T05:20:27.770 回答