Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

echarts

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.

like image 366
Tina Chen Avatar asked Aug 31 '16 09:08

Tina Chen


2 Answers

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;
      })
    }
}],
like image 122
Conor Avatar answered Oct 19 '22 05:10

Conor


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

Use this as reference: https://ecomfe.github.io/echarts-doc/public/en/option.html#xAxis.axisLabel.formatter

like image 22
Mijago Avatar answered Oct 19 '22 07:10

Mijago