Search code examples
echartsapache-echarts

When data are zeros show on y axis only zeros. (ECharts)


Hello everyone here is my axisLabel formatter

      yAxis: {
        show: true,
        type: "value",
        min: 0,
        // max: function (value) {
        //   return Math.ceil(value.max);
        // },
        axisLabel: {
          formatter: (val) => {
            console.log(val);
            return props.time
              ? timeFormatter(val / 1000)
              : Math.ceil(val) + ` ${props.units}`;
          },
        },

This is what I get for my y-axis all 1ones because of Math.ceil enter image description here

If I don't use my formatter I get this

enter image description here

What I want is if all my data is zero to show zeros on my y-axis


Solution

  • For completeness I'll write a full answer here:

    You can specify the smallest interval between two ticks with minInterval. If you set minInterval to 1 your axis should only show 0 and 1.

    If you want it to show only 0 you can set showMaxLabel to false.

    The default max value seems to be 1. You could also specify a different max value depending on your current maximum data value via max: (value) => value.max + <some_value>.

    Here is an example showing only 0 on the yAxis:

    import * as echarts from 'echarts';
    
    var chartDom = document.getElementById('main');
    var myChart = echarts.init(chartDom);
    var option;
    
    option = {
      xAxis: {
        type: 'category',
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
      },
      yAxis: {
        type: 'value',
        minInterval: 1,
        axisLabel: { showMaxLabel: false }
      },
      series: [
        {
          data: [0, 0, 0, 0, 0, 0, 0],
          type: 'line'
        }
      ]
    };
    
    option && myChart.setOption(option);