How do I set a minimum upper bound for an axis in Highcharts?

后端 未结 5 841
无人共我
无人共我 2021-01-12 07:04

I\'m trying to set a minimum upper bound, specifically:

  • The Y axis should start at 0
  • The Y axis should go to at least 10, or higher (automatically sca
5条回答
  •  独厮守ぢ
    2021-01-12 07:34

    Adding to Julian D's very good answer, the following avoids a potential re-positioning problem if your calculated max varies in number of digits to your desired upper bound.

    In my case I had percentage data currently going into the 20's but I wanted a range of 0 to at least 100, with the possibility to go over 100 if required, so the following sets min & max in the code, then if dataMax turns out to be higher, reassigns max up to it's value. This means the graph positioning is always calculated with enough room for 3-digit values, rather than calculated for 2-digits then broken by squeezing "100" in, but allows up to "999" before there would next be a problem.

    var chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            events: {
                load: function(event) {
                    thisSetMax = this.yAxis[0].getExtremes().max;
                    thisDataMax = this.yAxis[0].getExtremes().dataMax;
                    if (thisDataMax > thisSetMax) {
                        this.yAxis[0].setExtremes(0, thisDataMax);
                        alert('Resizing Max from ' + thisSetMax + ' to ' + thisDataMax);
                    }
                }
            }        
        },
        title: {
            text: 'My Graph'
        },
        xAxis: {
            categories: ['Jan 2013', 'Feb 2013', 'Mar 2013', 'Apr 2013', 'May 2013', 'Jun 2013']
        },
        yAxis: {
            min: 0,
            max: 100,
            title: {
                text: '% Due Tasks Done'
            }
        }
        //Etc...
    });
    

提交回复
热议问题