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

scaleIntegersOnly: true does not seem to work

Open gmagill opened this issue 12 years ago • 8 comments

The following graph is appearing with the Y-Axis showing decimal values even though I have set the scaleIntegersOnly: true flag. Any ideas why would be much appreciated. Thanks.

untitled

var ctx = document.getElementById("canvas").getContext("2d"); window.myLine = new Chart(ctx).Line(lineChartData, { scaleShowGridLines: true , scaleIntegersOnly: true, responsive: true });

var lineChartData = { labels : ["2014-09-29", "2014-10-01", "2014-10-02", "2014-10-03", "2014-10-10", "2014-10-18", "2014-10-20", "2014-10-21", "2014-10-22", "2014-10-24", "2014-10-25", "2014-10-26", "2014-10-27", "2014-10-28", "2014-10-29", "2014-10-30", "2014-10-31", "2014-11-01", "2014-11-02", "2014-11-03", "2014-11-04", "2014-11-05", "2014-11-06", ], datasets : [ { label: "Number of Tenants", fillColor : "rgba(146,208,80,0.5)", strokeColor : "rgba(220,220,220,1)", pointColor : "rgba(146,208,80,0.5)", pointStrokeColor : "rgba(146,208,80,1)", pointHighlightFill : "#fff", pointHighlightStroke : "rgba(220,220,220,1)", data : [1, 1, 4, 42, 1, 1, 8, 6, 3, 5, 20, 11, 19, 242, 356, 142, 59, 7, 12, 33, 46, 28, 14, ] }, { label: "Number of Users", fillColor : "rgba(250,254,28,0.2)", strokeColor : "rgba(250,254,28,0.2)", pointColor : "rgba(250,254,28,0.2)", pointStrokeColor : "rgba(250,254,28,0.2)", pointHighlightFill : "#fff", pointHighlightStroke : "rgba(151,187,205,1)", data : [0, 1, 1722, 646, 5, 22, 26, 20, 6, 10, 42, 75, 74, 771, 920, 262, 98, 28, 28, 79, 87, 62, 22, ] } ]

}

gmagill avatar Nov 06 '14 18:11 gmagill

I have the same issue. Did anybody found a solution?

vladkorzun avatar Jan 11 '15 08:01 vladkorzun

Found the answer if it is of interest to anyone:

chart.js can not do what is shown above. However, there is a chartnew.js version on the internet - google it. This version can do it. It is done like this:

Add to chart options to show only integers:

yAxisMinimumInterval : 1

gmagill avatar Mar 06 '15 13:03 gmagill

https://github.com/FVANCOP/ChartNew.js/

gmagill avatar Mar 06 '15 13:03 gmagill

Nice, I will try that one!

vladkorzun avatar Mar 06 '15 13:03 vladkorzun

Tested in v1.1.0 The result is: https://jsfiddle.net/gzj6yz6z/

etimberg avatar Apr 03 '16 00:04 etimberg

This doesn't seams to work for version 2?

glennkarlsen avatar Jul 25 '16 16:07 glennkarlsen

My temporary solution for filtering float numbers:

options: {
    scales: {
        yAxes: [{
            ticks: {
                callback: function(val) {
                    return Number.isInteger(val) ? val : null;
                }
            }
        }]
    }
}

Be aware of bad browser support for "isInteger".

glennkarlsen avatar Jul 25 '16 16:07 glennkarlsen

Thank you @glennkarlsen this solve my problem.

Just cleaning you code:

callback: ((val:any) => Number.isInteger(val) ? val : null)

Nice solution.

dekonunes avatar Apr 28 '17 07:04 dekonunes