8

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.

Tina Chen
  • 2,010
  • 5
  • 41
  • 73

4 Answers4

10

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;
      })
    }
}],
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Conor
  • 441
  • 6
  • 12
6

Use xAxis.axisLabel.formatter. This can be a formatter string or a function.

Use this as reference: https://echarts.apache.org/en/option.html#xAxis.axisLabel.formatter

mfit
  • 807
  • 13
  • 28
Mijago
  • 1,569
  • 15
  • 18
2

I made your demo working. I've changed the value like this:

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

Please see this screenshot:

Fixed image

Patrick Mevzek
  • 10,995
  • 16
  • 38
  • 54
Jin Lin
  • 191
  • 2
  • 6
0

For anyone looking for the answer, the link provided in the accepted answer is correct but has 404'd.

Here's the current working one: https://echarts.apache.org/en/option.html#xAxis.axisLabel.formatter

Lumen27
  • 81
  • 1
  • 1