如何在 TIME 类型中设置 xAxis 并在 Echarts 中设置为 {hh:mm} 格式?

Tin*_*hen 5 echarts

我想将 xAxis 设置为 TIME 类型并格式化为 {hh:mm} ,例如 17:45。

在这个演示中,配置工作:

xAxis: {
    type: "time",
},

value: [
    [now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/'),
    Math.round(value)
]
Run Code Online (Sandbox Code Playgroud)

但这失败了,这是在 Echarts 库中的演示

xAxis: {
    type: "time",
},

value: [
    [now.getHours(), now.getMinutes()].join(":"),
    Math.round(value)
]
Run Code Online (Sandbox Code Playgroud)

我试过了type: "value",还是不行。

Mij*_*ago 6

使用xAxis.axisLabel.formatter。这可以是格式化字符串或函数。

将此用作参考: https ://echarts.apache.org/en/option.html#xAxis.axisLabel.formatter


Con*_*nor 5

如上所述,您需要使用 xAxis.axisLabel.formatter。

这是你的例子。

// Horizontal axis
xAxis: [{
    type: 'time', 
    axisLabel: {
      formatter: (function(value){
        let label;
        if (value.getMinutes() < 10){ 
          label = value.getHours() + ":0" +value.getMinutes();
        }
        else {
          label = value.getHours() + ":" +value.getMinutes();
        }
        return label;
      })
    }
}],
Run Code Online (Sandbox Code Playgroud)