【问题标题】:Google Charts Custom Tooltip - Generating the JSON from PHP (accessing mySQL Server)Google Charts Custom Tooltip - 从 PHP 生成 JSON(访问 mySQL Server)
【发布时间】:2015-06-15 16:58:41
【问题描述】:

我正在使用 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"}]}]}

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

【问题讨论】:

  • 使用“tooltip”列角色会完全覆盖tooltips的内容,是你想做的,还是想改变现有tooltip格式的显示值?
  • 我想完全覆盖工具提示的内容。

标签: php json google-visualization


【解决方案1】:

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

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

据我所知,这将覆盖整行的工具提示,您的意思是看到其他 cmets 这样做

所以你的新代码看起来像这样:

$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')
);

请注意,标签不是指定列应具有什么角色的方式,如果您希望使用诸如域、确定性和其他谷歌图表角色之类的角色,这也适用。

【讨论】:

  • 非常感谢您的回答! :)
猜你喜欢
  • 2016-12-07
  • 1970-01-01
  • 2017-03-22
  • 2021-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多