Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

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

Open gouthamrv opened this issue 10 years ago • 8 comments

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?

gouthamrv avatar Feb 02 '16 20:02 gouthamrv

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

etimberg avatar Feb 03 '16 01:02 etimberg

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

gouthamrv avatar Feb 03 '16 01:02 gouthamrv

closing as resolved

etimberg avatar Feb 15 '16 19:02 etimberg

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

AlexLomm avatar Jan 09 '18 07:01 AlexLomm

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

DaveTacker avatar Feb 07 '18 17:02 DaveTacker

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?

DaveTacker avatar Feb 08 '18 19:02 DaveTacker

@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

jwds avatar Apr 25 '18 17:04 jwds

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

samlimbu avatar Sep 12 '18 12:09 samlimbu