所有数据类型字符串的折线图?时间表?

时间:2012-05-10 04:36:51

标签: google-visualization

随着时间的推移,我想在我的城市制作市议会成员的图表。我认为这有点像折线图。 x轴将是年。有九个市议会席位,因此将有九条直线,每个都会显示谁是市议会成员随着时间的推移(可能通过不同的彩色线段或通过显示他们的名字onMouseOver)。也许这就像一个时间线。

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

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

如何制作此图表? (我不仅想要绘制数字数据,如预算盈余或赤字或抢劫数量,而且还与[当时另一张图表]负责人联系起来。)

1 个答案:

答案 0 :(得分:0)

在PHP中,我查询了我的mySQL数据库并以Google Chart API需要接收的格式生成了一个JSON对象,以便随着时间的推移制作水平线,显示名称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);