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

前端 未结 5 819
灰色年华
灰色年华 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];
        },
    
        ...
    }
    
    0 讨论(0)
  • 2021-02-13 04:00

    Just in case someone is searching,

    One option more. I ended up in a similar situation. Follows my solution:

    tickPositioner: function () {
        var dataMin,
        dataMax = this.dataMax;
        var positivePositions = [], negativePositions = [];
    
        if(this.dataMin<0) dataMin = this.dataMin*-1;
        if(this.dataMax<0) dataMax = this.dataMax*-1;
    
        for (var i = 0; i <= (dataMin)+10; i+=10) {
            negativePositions.push(i*-1)
        }
    
        negativePositions.reverse().pop();
    
        for (var i = 0; i <= (dataMax)+10; i+=10) {
            positivePositions.push(i)
        }
    
        return negativePositions.concat(positivePositions);
    
    },
    

    http://jsfiddle.net/j3NTM/21/

    0 讨论(0)
  • 2021-02-13 04:04

    You can do this with the getExtremes and setExtremes methods

    • http://api.highcharts.com/highcharts#Axis.getExtremes%28%29
    • http://api.highcharts.com/highcharts#Axis.setExtremes%28%29

    example:

    http://jsfiddle.net/jlbriggs/j3NTM/1/

    var ext = chart.yAxis[0].getExtremes();
    
    0 讨论(0)
  • 2021-02-13 04:12

    I know this is an old post, but thought I would post my solution anyway (which is inspired from the one macserv suggested above in the accepted answer) as it may help others who are looking for a similar solution:

    tickPositioner: function (min, max) {
                var maxDeviation = Math.ceil(Math.max(Math.abs(this.dataMax), Math.abs(this.dataMin)));
                return this.getLinearTickPositions(this.tickInterval, -maxDeviation, maxDeviation);
            }
    
    0 讨论(0)
  • 2021-02-13 04:13

    Here is my solution. The nice thing about this is that you can maintain the tickInterval.

      tickPositioner(min, max) {
        let { tickPositions, tickInterval } = this;
        tickPositions = _.map(tickPositions, (tickPos) => Math.abs(tickPos));
        tickPositions = tickPositions.sort((a, b) => (b - a));
    
        const maxTickPosition = _.first(tickPositions);
        let minTickPosition = maxTickPosition * -1;
    
        let newTickPositions = [];
        while (minTickPosition <= maxTickPosition) {
          newTickPositions.push(minTickPosition);
          minTickPosition += tickInterval;
        }
    
        return newTickPositions;
      }
    
    0 讨论(0)
提交回复
热议问题