Highcharts - Keep Zero Centered on Y-Axis with Negative Values

前端 未结 5 828
灰色年华
灰色年华 2021-02-13 03:18

I have an area chart with negative values. Nothing insanely different from the example they give, but there\'s one twist: I\'d like to keep zero centered on the Y axis.

5条回答
  •  花落未央
    2021-02-13 03:58

    I ended up finding a way to do this through configuration after digging even further into the Highcharts API. Each axis has a configuration option called tickPositioner for which you provide a function which returns an array. This array contains the exact values where you want ticks to appear on the axis. Here is my new tickPositioner configuration, which places five ticks on my Y axis, with zero neatly in the middle and the max at both extremes :

    yAxis: {
    
        tickPositioner: function () {
    
            var maxDeviation = Math.ceil(Math.max(Math.abs(this.dataMax), Math.abs(this.dataMin)));
            var halfMaxDeviation = Math.ceil(maxDeviation / 2);
    
            return [-maxDeviation, -halfMaxDeviation, 0, halfMaxDeviation, maxDeviation];
        },
    
        ...
    }
    

提交回复
热议问题