本文介绍了为 Google Charts Api 编写自定义格式化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个图表来显示我在 10k 运行中的时间.一切正常,我唯一的问题是,我想格式化时间的显示方式.

I want to create a chart to display my time over a 10k run. Everything is working as it should, the only problem that I have is, that I want to format how the time is displayed.

当前时间显示为代表秒的数字.例如,30 分钟的运行时间可缩短为 10800 秒.谷歌提供的格式化程序确实涵盖了很多内容,但我对它们提供的内容并不满意.

Currently the time is displayed as a number which represents the seconds. For example a 30 minute run comes down to 10800 seconds. The formatters provided by google do cover a lot of stuff, but I'm not really happy with what they provide.

http://code.google.com/apis/chart/interactive/docs/reference.html#formatters

遗憾的是,没有关于如何实现您自己的格式化程序的信息.是否有机会扩展格式化程序或者是否有需要实现的接口?

Sadly there is no information on how to implement your own formatter. Is there any chance I can extend a formatter or is there an interface that needs to be implemented?

我要做的第一件事就是将数字 10800 解析为一个不错的时间,例如 30:00.00(30 分 0 秒,0 毫秒).也许使用模式格式化程序已经可以做到这一点,但我不知道如何实现,因为涉及计算并且不知道如何在模式格式化程序中实现这一点.

The first thing I would do would do parse the number 10800 to a nice time like 30:00.00 (30 minutes, 0 seconds, 0 millisecons).Maybe this is already possible with a patternformatter, but i don't see how, as there is calculating involved and don't know how to implement this in the patternformatter.

感谢您的帮助.约翰尼

推荐答案

使用 DateFormat 带有自定义模式.例如:

Use a DateFormat with a custom pattern. E.g.:

google.visualization.DateFormat({pattern: "mm:ss.SSS"});

需要注意的是,这会显示 JS Date 对象的时间,因此您需要提供运行时间作为 JS 日期.因此,要么指定采样数据的实际时间.或者,如果您只想显示自运行开始以来的相对时间,您可以执行以下操作:

The caveat is that this displays the time-of-day of a JS Date object, so you'll need to provide your run times as JS dates. So either specify the actual time-of-day at which your data was sampled. Or, if you want to just show the relative time since the start of your run, you could do something like this:

new Date(new Date('Jan 01 2000').getTime() + sampleTime)

... 其中 sampleTime 是每个采样点的时间(以毫秒为单位).(这只是将时间设置为自 2000 年 1 月 1 日午夜以来的毫秒数,因此小时/分钟/秒将反映您的运行时间)

... where sampleTime is the time of each sample point in milliseconds. (This just sets the time as milliseconds since midnight 01/01/2000, so the hour/minute/second will reflect your run time)

这篇关于为 Google Charts Api 编写自定义格式化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:59