How to set xAxis in TIME type and formatted like {hh:mm} in Echarts?

Tina Chen picture Tina Chen · Aug 31, 2016 · Viewed 12.1k times · Source

I want to set xAxis in TIME type and formatted like {hh:mm} , such as 17:45.

In this demo, configuration works:

xAxis: {
    type: "time",
},

value: [
    [now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/'),
    Math.round(value)
]

But this fails, here is my demo in Echarts gallery :

xAxis: {
    type: "time",
},

value: [
    [now.getHours(), now.getMinutes()].join(":"),
    Math.round(value)
]

I tried type: "value", still not working.

Answer

Conor picture Conor · May 30, 2017

As mention above you need to use xAxis.axisLabel.formatter.

Here is your example.

// 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;
      })
    }
}],