简体   繁体   English

Google Charts自定义工具提示-从PHP生成JSON(访问mySQL Server)

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

I'm using the Google Charts to plot daily build completion times (x-axis: date-of-build as string, y-axis: build-completion-time as number); 我正在使用Google图表绘制每日构建完成时间(x轴:构建日期为字符串,y轴:构建完成时间为数字); the data is retrieved from a mySQL database with a PHP script that then encodes into JSON. 使用PHP脚本从mySQL数据库中检索数据,然后将其编码为JSON。

Because I have plotted the build completion times as a number (decimal format), I want to customize the tooltip such that when a viewer hovers over it, they see a formatted time (and some string info) (12:34am, etc.) as opposed to the plotted decimal (12.5, etc.). 因为我已将构建完成时间绘制为数字(十进制格式),所以我想自定义工具提示,以便当查看者将鼠标悬停在工具提示上时,他们会看到格式化的时间(以及一些字符串信息)(上午12:34等)。而不是标出的小数点(12.5等)。

I know you can create a tooltip column with the code below if you are passing the row data directly from client side, 我知道如果您直接从客户端传递行数据,则可以使用以下代码创建工具提示列,

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

... but I'm not sure how to get the right JSON format for the tooltip column if I'm encoding JSON from the php script as below: ...但是如果我从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);

To be more specifically, I'm not sure how to set up the column array in PHP. 更具体地说,我不确定如何在PHP中设置列数组。 It's certainly not: 当然不是:

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

JSON is as per below at the moment: 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"}]}]}

Any help would be greatly appreciated, thanks! 任何帮助将不胜感激,谢谢!

to specify a tooltip via php to encode to JSON later you can use 通过php指定工具提示,以便稍后编码为JSON

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

As far as I know this will override the tooltip for the entire row and you mean to do that seeing you other comments 据我所知,这将覆盖整行的工具提示,您的意思是看到其他评论

So your new code would look something like this: 因此,您的新代码将如下所示:

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

Note that label is not the way to specify what role the column should have, this also applies if you wish to use roles such as Domain, Certainty and other google chart roles. 请注意,标签不是指定列应具有的角色的方法,如果您希望使用诸如域,确定性和其他Google图表角色之类的角色,该方法也适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM