Chart.js: v2.0 dev, how to limit number of ticks on Y axis?

Created on 2 Feb 2016  ·  8Comments  ·  Source: chartjs/Chart.js

Hi,

I am trying to limit number of ticks on Y axis. Currently it automatically adjust based on chart height and data set.

See example here: https://jsfiddle.net/wesgvmgu/1/

I would prefer to limit to 6 irrespective of height and data set (I mean if there is no data also, I want to see 6 ticks on Y axis).

How do I do that?

enhancement

Most helpful comment

Is there a way to set a fixed number of ticks?

All 8 comments

You can set ticks.maxTicksLimit in your config but it cannot guarantee a fixed number of ticks.

@etimberg thank you very much. That is exactly I am looking for.

closing as resolved

Is there a way to set a fixed number of ticks?

I was able to get a fixed number of ticks because I only wanted numbers divisible by 100,000 (it's a financial chart). As I only wanted a maximum of 10 y-axis ticks at any time, this worked for me:

options: {
        scales: {
            yAxes: [{
                ticks: {
                    callback: function (value, index, array) {
                        var maxValue = array[0],
                            chunkSize = parseInt(maxValue / 1000000);

                        if (maxValue < 1000000) {
                            return '             $' + abbreviate_number(value);
                        } else if (index % chunkSize === 0) {
                            return '         $' + abbreviate_number(value);
                        }

                        return null;
                    }
               // ...

Upon further testing, my previous solution is not working nearly as well as I'd hoped. Does anyone have a better solution to show a maximum of 10 ticks at a time?

@kneeki Do you got solution for showing max ticks at a time. i always want to display 10 ticks maximum - my ticks on X-axis are dynamic and can expect 20 to 30 for each chart

Is there a way to set a fixed number of ticks?

this did it for me, there will be 10 fixed number of ticks and will only work for positive numbers

```
ticks: {
fontColor: 'white',
stepSize:1,
callback: function(value, index, values) {

                                if(value%Math.round(values[0]/10)==0){
                                   return  value;
                                }
                                else if(value===0){
                                    return value;
                                }

                            }
                    },
Was this page helpful?
0 / 5 - 0 ratings