Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set minimum step size in chart js

Tags:

graph

chart.js

I am using Chart.js 2.0 version to draw graphs, i want to define minimum step size in bar graph

var myNewChart = new Chart(grapharea, {                 type: 'bar',                 data: barData,                 options: {                     responsive: true,                                             scales: {                         yAxes: [                             {                                 ticks: {                                     min: 0, // it is for ignoring negative step.                                     beginAtZero: true,                                     stepSize: 1  // if i use this it always set it '1', which look very awkward if it have high value  e.g. '100'.                                 }                             }                         ]                     }                 }             }); 

this time i am using

stepSize: 1

i am using this step size to ignore the point value e.g. '0.5', it shows when the max graph values id less e.g '2'.
if i use this it always set the step it '1', which look very awkward if it have high value e.g. '100'.

I am looking for such thing: suggestedMin = 1

Is there any thing to define thie minimum step size which should not be fixed in higher value cases.

like image 672
Syed Uzair Uddin Avatar asked Feb 09 '17 11:02

Syed Uzair Uddin


People also ask

What is step size in chart js?

Step Size. If set, the scale ticks will be enumerated by multiple of stepSize , having one tick per increment. If not set, the ticks are labeled automatically using the nice numbers algorithm. This example sets up a chart with a y axis that creates ticks at 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5 .

Which is better D3 or chart js?

Chart. js provides a selection of ready to go charts which can be styled and configured while D3 offers building blocks which are combined to create virtually any data visualisation.

Does chart js use SVG?

Chart. js still uses the HTML Canvas element, so charts generated with it look blurred when printed or zoomed in. Nowadays I prefer Apexcharts. js, which renders to SVG.

Does chart js use canvas?

Chart. js charts are rendered on user provided canvas elements. Thus, it is up to the user to create the canvas element in a way that is accessible.


1 Answers

If you don't want to show point value (e.g. 0.5) labels you could write a callback function to filter the point value labels instead of using stepSize.

Like this:

ticks: {     min: 0, // it is for ignoring negative step.     beginAtZero: true,     callback: function(value, index, values) {         if (Math.floor(value) === value) {             return value;         }     } } 

Working fiddle here: https://jsfiddle.net/ma7h611L/

Update:

As noted by Atta H. and Lekoaf below, Chart.js added the precision property to ticks. It is available since version 2.7.3. See Lekoaf's answer how to use this.

like image 86
Daantie Avatar answered Oct 04 '22 13:10

Daantie