Highcharts: Ensure y-Axis 0 value is at chart bottom

后端 未结 8 1802
名媛妹妹
名媛妹妹 2021-02-05 02:18

I\'m outputting a series of highcharts on a page. In some instances, all data for the specified time period may come back with 0 values.

In such a case, the chart looks

相关标签:
8条回答
  • 2021-02-05 02:47

    Since Highcharts 5.0.1, you can also use softMax.

    softMax: Number
    A soft maximum for the axis. If the series data maximum is greater than this, the axis will stay at this maximum, but if the series data maximum is higher, the axis will flex to show all data.

    yAxis: {
        min: 0,
        softMax: 100,
        ...
    }
    

    Here is an example with a column chart : http://jsfiddle.net/84LLsepj/

    Note that currently, in Highcharts 5.0.7, it doesn't seem to be working for all types of charts. As you can see in this other jsfiddle, it doesn't work with line chart.

    0 讨论(0)
  • 2021-02-05 02:54

    I've found another (and very simple) solution:

    You can set y-axis minRange property:

    yAxis: {
        ...
        minRange : 0.1
    }
    

    This makes Highcharts render yAxis min and max values when all data is 0.

    UPDATE:

    Highcharts 4.1.9 fix also needs:

            plotOptions: {
                line: {
                    softThreshold: false
                }
            }
    

    updated js fiddle

    0 讨论(0)
  • 2021-02-05 02:54

    minRange : 1 or whatever you feel so to be displayed in fractions of
    softMax : 100 or whatever you feel so

    yAxis: {
             minRange : 1,
             softMax: 100
           }
    
    0 讨论(0)
  • 2021-02-05 02:56

    Only way I could get it to show it "correctly" was by also providing an yAxis max value. I think this is a HighCharts issue when you provide no data elements with a y-value. It draws the best chart it thinks it can.

    0 讨论(0)
  • 2021-02-05 02:56

    For Highcharts 5.x.x, you should be able to just specify:

    yAxis: {
      softMin: 0,
      softMax: 1,
    };
    

    This won't work for line chart, which requires that you also set minRange in the same config object as above. Experiment with different values, since defining for example minRange: 6, created a range on yAxis between 0 and 4.

    0 讨论(0)
  • 2021-02-05 02:57

    Here is a fix I came up with that doesn't require any preprocessing of the data and just let highcharts do it, like it used to before the update. http://jsfiddle.net/QEK3x/

    if(chart.yAxis[0].dataMax === 0)
    {
       chart.yAxis[0].setExtremes(0, 5);
    }
    

    You only need to check the first series axis, as if the first one is not 0 then the problem won't occur. I might submit a pull request for this to high-charts, though I kinda feel like they intended for this functionality for some reason.

    0 讨论(0)
提交回复
热议问题