Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Float problem on yAxis when all data is decimal #1890

Closed
hutchy2570 opened this issue Jan 11, 2016 · 4 comments
Closed

Float problem on yAxis when all data is decimal #1890

hutchy2570 opened this issue Jan 11, 2016 · 4 comments

Comments

@hutchy2570
Copy link

I'm using the latest 2.0.0-dev build (Jan 9th) which contains fix #1567.

In a line chart, when my data is decimal there are floating point problems with the tick labels.

            var ctx = $("#testchart");
            var myLineChart = new Chart(ctx, {
                type: 'line',
                data: {
                    labels: ["1", "2", "3"],
                    datasets: [
                        {
                            label: "My First dataset",
                            data: [0.3, 1.2, 0]
                        },
                        {
                            label: "My Second dataset",
                            data: [0, 0.3, 1.2]
                        }
                    ]
                }
            });
        });

That data renders:
weather

@hutchy2570
Copy link
Author

Just an addition. I tried overriding the callback on the yAxis tick to log the value being passed in for tickValue and it already had the rounding problemon it. Hope this info helps.

@rulero
Copy link

rulero commented Jan 15, 2016

I have the same problem with a single dataset, the problem seems to go away when the canvas is narrow though!

The problem happens in the function buildTicks() when the ticks are calculated as a multiple of spacing, since in theory they shouldn't have more decimal positions than spacing (because the multiplier is integer) I round them as a workaround:

I use function decimalPlaces() from http:https://stackoverflow.com/questions/10454518/javascript-how-to-retrieve-the-number-of-decimals-of-a-string-number

function decimalPlaces(num) {
  var match = (''+num).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);
  if (!match) { return 0; }
  return Math.max(
       0,
       // Number of digits right of decimal point.
       (match[1] ? match[1].length : 0)
       // Adjust for scientific notation.
       - (match[2] ? +match[2] : 0));
}

And round niceMIn, niceMax and the ticks in buildTicks()

            var niceRange = helpers.niceNum(this.max - this.min, false);
            var spacing = helpers.niceNum(niceRange / (maxTicks - 1), true);
            var roundFactor = Math.pow(10, decimalPlaces(spacing));
            var niceMin = Math.round(Math.floor(this.min / spacing) * spacing * roundFactor) / roundFactor; // Round
            var niceMax = Math.round(Math.ceil(this.max / spacing) * spacing * roundFactor) / roundFactor; // Round

            var numSpaces = Math.ceil((niceMax - niceMin) / spacing);

            // Put the values into the ticks array
            this.ticks.push(this.options.ticks.min !== undefined ? this.options.ticks.min : niceMin);
            for (var j = 1; j < numSpaces; ++j) {
                this.ticks.push(Math.round((niceMin + (j * spacing)) * roundFactor) / roundFactor); // Round
            }
            this.ticks.push(this.options.ticks.max !== undefined ? this.options.ticks.max : niceMax);

Hope this helps.

@hutchy2570
Copy link
Author

As a workaround I configured my yAxes ticks callback to simply be the following which works for my usage (simple graph displaying rainfall rate and accumulated rainfall over 24 hours).

function (tickValue, index, ticks) {
            return Number(tickValue).toFixed(2);
        }

@etimberg
Copy link
Member

etimberg commented Feb 6, 2016

Fixed in #1984

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants