Giter Site home page Giter Site logo

Comments (25)

carlows avatar carlows commented on April 29, 2024 2

It's been 6 years and this is still a valid issue :( why was it closed? If your data only has small integers (I believe less than 10 per data point) then it will display floats instead of integers in the axis

from chart.js.

visioncode avatar visioncode commented on April 29, 2024 1

really annoying :-( i always got 2 decimal points in each of my bar graphs (looks very strange cause it represent "minutes") An update would be amazing 👍

from chart.js.

psyose avatar psyose commented on April 29, 2024 1

After some more testing I found the initial patch caused an infinite loop when using data sets containing zero values.

I created an alternative patch just after the while loop in the calculateScale function:

if (stepValue < 1) {
    stepValue = 1;
} else {
    stepValue = Math.round(stepValue);
}

Would be great if you could specify "integers: true" in the options.

from chart.js.

BC17 avatar BC17 commented on April 29, 2024 1
var max = <?php echo json_encode(max(array_values($data))) ?>;
var option = {
scale: {
          ticks: {
                 beginAtZero: true,
                 steps: max/10 >= 1 ? max/10 : 1,
                 stepValue: max/10 >= 1 ? max/10 : 1,
                 max: max >=10 ? max : 10
                }
         }
}
}

and you will get only integers

from chart.js.

uBaze avatar uBaze commented on April 29, 2024

Waiting for an update, you can do these changes :

replace in the calculateScale function

   stepValue /= 2;

with

   stepValue = Math.round(stepValue / 2);

And replace in the getValueBounds() functions for Bar & Line

var minSteps = Math.floor((scaleHeight / labelHeight*0.5));

with

var minSteps = (upperValue < Math.floor((scaleHeight / labelHeight*0.5))) ? upperValue : Math.floor((scaleHeight / labelHeight*0.5));

This work great for me.
Hope it help.

Cheers.

Val

from chart.js.

erd0s avatar erd0s commented on April 29, 2024

I had this issue but I'm just precalculating the ticks in code (PHP) before passing the data off to chartjs

function get_steps($numberOfTicks, $dataPoints) {
    $max = round(max($dataPoints));
    $steps = ($max + ($numberOfTicks - $max % $numberOfTicks)) / $numberOfTicks;
    return $steps;
}

from chart.js.

psyose avatar psyose commented on April 29, 2024

Resolved my problem! Thanks Val (uBaze)
+1

from chart.js.

 avatar commented on April 29, 2024

+1 "integers: true" in options but could we bulk this up a bit by passing in the float position as a variable.

stepValueX = integer; ( 20.000 = 20 )
stepValueX = float(2); ( 724 = 7.34 )

...and decouple X & Y treatments...

stepValueY = float(2);

As this codebase matures this will become increasingly viable.

from chart.js.

seyhunak avatar seyhunak commented on April 29, 2024

@uBaze @psyose Can you please paste your js file, i tried modifications but still causing infinite loop.

from chart.js.

rakeshtembhurne avatar rakeshtembhurne commented on April 29, 2024

I found this piece of code very helpful, worked for me.

var steps = 3;
new Chart(ctx).Bar(plotData, {
    scaleOverride: true,
    scaleSteps: steps,
    scaleStepWidth: Math.ceil(max / steps),
    scaleStartValue: 0
});

Source: http://stackoverflow.com/questions/15751571/how-to-change-the-y-axis-values-from-real-numbers-to-integer-in-chartjs

from chart.js.

uBaze avatar uBaze commented on April 29, 2024

@seyhunak I've pasted my file here : http://jsfiddle.net/Ase7V/

from chart.js.

 avatar commented on April 29, 2024

I've just add Math.floor to the label drawing function (you need to change for every type of char you want to round the value)

labels.push(tmpl(labelTemplateString, {value: Math.floor((graphMin + (stepValue * i))) }));

@populateLabels

from chart.js.

Netherdrake avatar Netherdrake commented on April 29, 2024

I keep getting infinite loop no matter what. Can anyone gist their entire Chart.js solution?

from chart.js.

uBaze avatar uBaze commented on April 29, 2024

@Netherdrake : The gist of my file, don't know if helpfull https://gist.github.com/uBaze/6962655

from chart.js.

nadimkobeissi avatar nadimkobeissi commented on April 29, 2024

+1

from chart.js.

MardyGit avatar MardyGit commented on April 29, 2024

I have charts with highly variable values. This is how I set the scales so they are always integer values and ~10 steps...

var steps = new Number(model.maxValue);
var stepWidth = new Number(1);
if (model.maxValue > 10) {
    stepWidth = Math.floor(model.maxValue / 10);
    steps = Math.ceil(model.maxValue / stepWidth);
}
var options = { scaleOverride: true, scaleSteps: steps, scaleStepWidth: stepWidth, scaleStartValue: 0 };

from chart.js.

leeDav avatar leeDav commented on April 29, 2024

@uBaze This worked well, but gets stuck in an infinite loop for small numbers <= 1.

I included a simple IF to make it work again =)

stepValue = (stepValue <= 1) ? stepValue : Math.round(stepValue / 2);

from chart.js.

uBaze avatar uBaze commented on April 29, 2024

Ahh okay, nice !
In my case I don't work with small numbers <=1 that's why I didn't see that !

Well done ! :)

from chart.js.

orrd avatar orrd commented on April 29, 2024

Was this issue ever fixed in the master Chart.js code? I'm using the latest version, but I'm still having this issue with decimal numbers on the axis labels. I would imagine this issue must effect a high percent of users who want to us Chart.js.

from chart.js.

orrd avatar orrd commented on April 29, 2024

Here is a function that also does the work to calculate the max value (based on MardyGit's code)...

function wholeNumberAxisFix(data) {
   var maxValue = false;
   for (datasetIndex = 0; datasetIndex < data.datasets.length; ++datasetIndex) {
       var setMax = Math.max.apply(null, data.datasets[datasetIndex].data);
       if (maxValue === false || setMax > maxValue) maxValue = setMax;
   }

   var steps = new Number(maxValue);
   var stepWidth = new Number(1);
   if (maxValue > 10) {
       stepWidth = Math.floor(maxValue / 10);
       steps = Math.ceil(maxValue / stepWidth);
   }
   return { scaleOverride: true, scaleSteps: steps, scaleStepWidth: stepWidth, scaleStartValue: 0 };
}

So that way you can do something like "new Chart(ctx).Line(data, wholeNumberAxisFix(data))".

from chart.js.

karanveerm avatar karanveerm commented on April 29, 2024

@orrd That works great, thanks so much!

from chart.js.

yusrul avatar yusrul commented on April 29, 2024

is this function (wholeNumberAxisFix() ) really work? i just test and seem like start with 0
error

from chart.js.

catcap avatar catcap commented on April 29, 2024

@orrd Works perfectly! Thanks!

from chart.js.

fulldecent avatar fulldecent commented on April 29, 2024

I am not able to reproduce this problem using the new version release candidate.

Could you please check if this has been fixed and close or create a JSBin showing the issue.

Thank you

from chart.js.

orrd avatar orrd commented on April 29, 2024

I think it may have been fixed? This is the code from the original example (I just fixed the source since GitHub disabled hotlinking of JS): http://jsfiddle.net/SNLAw/19/

from chart.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.