【问题标题】:React vis - Formatting time tick on the x axisReact vis - x 轴上的格式化时间刻度
【发布时间】:2024-05-18 13:45:01
【问题描述】:

我已将xType="time" 添加到图表中,以在 x 轴上显示时间刻度。我只显示 25 秒范围内的数据。目前,x 轴显示时间格式为:SS

所以 x 轴以以下格式显示时间(数据显示每秒):

:23, :24, :25

我从数据库中得到的是时间字符串,格式如下: 2019-07-01T10:42:38.621Z

我尝试了以下方法:

new Date(item.date) // shows ':023'
new Date(item.date).getTime() // shows ':023'
new Date(item.date.substring(19, 0)).getTime() // shows ':023'
    oscilloscope.oscilloscope.map((item, index) => {
        return {
            x: new Date(item.date.substring(19, 0)).getTime(),
            y: item.data.fuelInjection
        }
    })

总是得到相同的结果。

我希望将 x 轴格式化为 HH:MM:SS 格式。

所以 x 轴显示的数据如下: 11:42:05, 11:42:06, 11:42:07

我显示的范围是相隔 25 秒。这似乎是由图表自动设置的,就好像我将范围更改为包括几分钟在内的程度,x 轴上的时间显示更改为MM:SS 格式。我仍然需要HH:MM:SS 格式。这能做到吗?

【问题讨论】:

    标签: react-vis


    【解决方案1】:

    为了在一周后回答我自己的问题,我在 Stack Overflow 上找到了关于 react-vis 的不同问题的答案: show date in( MM-DD) format in x-axis using react-vis

    在我的情况下,解决方案是:

    <XAxis 
      tickFormat={function tickFormat(d){
        const date = new Date(d)
        return date.toISOString().substr(11, 8)
       }}
    />
    

    这就完成了。希望它会节省别人的时间。

    【讨论】: