Google Charts Custom Tooltip - 从PHP生成JSON(访问mySQL Server)


Google Charts Custom Tooltip - Generating the JSON from PHP (accessing mySQL Server)

我正在使用Google图表绘制每日构建完成时间(x轴:构建日期为字符串,y轴:构建完成时间为数字);数据使用PHP脚本从mySQL数据库中检索,然后编码为JSON。

由于我已将生成完成时间绘制为数字(十进制格式),因此我想自定义工具提示,以便当查看者将鼠标悬停在其上时,他们会看到格式化的时间(和一些字符串信息)(上午 12:34 等),而不是绘制的小数(12.5 等)。

我知道如果您直接从客户端传递行数据,您可以使用下面的代码创建一个工具提示列,

dataTable.addColumn({type: 'string', role: 'tooltip'});

。但是如果我从 php 脚本编码 JSON,我不确定如何为工具提示列获取正确的 JSON 格式,如下所示:

$table['cols'] = array
(
    array('label' => 'Day', 'type' => 'string'),
    array('label' => 'Arrival Time', 'type' => 'number'),
    array('label' => 'Expected Time', 'type' => 'number'),
);

while($row = mysql_fetch_array($result)) 
{ 
// some data parsing
        $temp = array();
        $temp[] = array('v' => $row['current_formatted']);
        //$temp[] = array('v' => $time_units_splited[0]);
        $temp[] = array('v' => $time_in_decimal);
        $temp[] = array('v' => $time_expected_in_decimal);
        $rows[] = array('c' => $temp);
}
    $table['rows'] = $rows;
    echo json_encode($table);

更具体地说,我不确定如何在 PHP 中设置列数组。当然不是:

array('label' => 'tooltip', 'type' => 'source'), ...

JSON目前如下:

{"cols":[{"label":"Day","type":"string"},{"label":"Arrival Time","type":"number"},{"label":"Expected Time","type":"number"}],"rows":[{"c":[{"v":"22'/08'/13"},{"v":"1.19"},{"v":"1.00"}]},{"c":[{"v":"26'/08'/13"},{"v":"3.01"},{"v":"1.00"}]},{"c":[{"v":"27'/08'/13"},{"v":"2.30"},{"v":"1.00"}]},{"c":[{"v":"28'/08'/13"},{"v":"2.37"},{"v":"1.00"}]},{"c":[{"v":"29'/08'/13"},{"v":"2.36"},{"v":"1.00"}]},{"c":[{"v":"30'/08'/13"},{"v":"2.40"},{"v":"1.00"}]},{"c":[{"v":"03'/09'/13"},{"v":"2.25"},{"v":"1.00"}]},{"c":[{"v":"04'/09'/13"},{"v":"2.33"},{"v":"1.00"}]},{"c":[{"v":"05'/09'/13"},{"v":"3.06"},{"v":"1.00"}]},{"c":[{"v":"06'/09'/13"},{"v":"3.29"},{"v":"1.00"}]},{"c":[{"v":"09'/09'/13"},{"v":"3.34"},{"v":"1.00"}]},{"c":[{"v":"10'/09'/13"},{"v":"3.41"},{"v":"1.00"}]},{"c":[{"v":"11'/09'/13"},{"v":"3.34"},{"v":"1.00"}]},{"c":[{"v":"12'/09'/13"},{"v":"3.33"},{"v":"1.00"}]}]}

任何帮助将不胜感激,谢谢!

通过 php 指定工具提示以稍后编码为 JSON 您可以使用

array('type' => 'string', 'role' => 'tooltip'), 

据我所知,这将覆盖整行的工具提示,你的意思是看到你的其他评论

因此,您的新代码将如下所示:

$table['cols'] = array
(
  array('label' => 'Day', 'type' => 'string'),
  array('label' => 'Arrival Time', 'type' => 'number'),
  array('label' => 'Expected Time', 'type' => 'number'),
  array('label' => 'tooltip', 'type' => 'string', 'role' => 'tooltip')
);

请注意,标签不是指定列应具有的角色的方法,如果您希望使用域、确定性和其他 google 图表角色等角色,这也适用于此。