score:22

Accepted answer

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];
    },

    ...
}

score:0

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/

score:0

it is an old question but recently i have had the same problem, and here is my solution which might be generalized:

const tick_precision = 2;
const axis_max_expand_rate = 1.2;

function setaxisticks(axis, tickcount) {
    // first you calc the max from the data, then multiply with 1.1 or 1.2 
    // which can expand the max a little, in order to leave some space from the bottom/top to the max value. 
    // toprecision decide the significant number.
    let maxdeviation = (math.max(math.abs(axis.datamax), math.abs(axis.datamin)) * axis_max_expand_rate).toprecision(tick_precision);

    // in case it is not a whole number
    let wholemaxdeviation = maxdeviation * 10 ** tick_precision;

    // halfcount will be the tick counts on each side of 0
    let halfcount = math.floor(tickcount / 2);

    // look for the nearest larger number which can mod the halfcount
    while (wholemaxdeviation % halfcount != 0) {
        wholemaxdeviation++;
    }

    // calc the unit tick amount, remember to divide by the precision
    let unittick = (wholemaxdeviation / halfcount) / 10 ** tick_precision;

    // finally get all ticks
    let tickpositions = [];
    for (let i = -halfcount; i <= halfcount; i++) {
        // there are problems with the precision when multiply a float, make sure no anything like 1.6666666667 in your result
        let tick = parsefloat((unittick * i).tofixed(tick_precision));
        tickpositions.push(tick);
    }
    return tickpositions;
}

so in your chart axis tickpositioner you may add :

tickpositioner: function () {
    return setaxisticks(this, 7);
},

score:1

you can do this with the getextremes and setextremes methods

example:

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

var ext = chart.yaxis[0].getextremes();

score:1

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;
  }

score:3

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);
        }

Related Query

More Query from same tag