chartist-js / chartist

Simple responsive charts
https://chartist.dev
MIT License
13.34k stars 2.53k forks source link

Series with only 1s or 0s hangs when onlyInteger set to true #318

Closed anttirautanen closed 9 years ago

anttirautanen commented 9 years ago

If a chart gets a series of only 1s or 0s, the whole page hangs and no charts are drawn. The following code hangs at least on Chrome 42.0.2311.135 and Firefox 35.0.1.

new Chartist.Bar(chartSelector, {
    labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    series: [[0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1]]
  }, {
    axisY: {
    onlyInteger: true,
  }
})

These series would hang too:

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

If you remove the option onlyInteger: true then there are no problems.

gionkunz commented 9 years ago

Hi there

Thanks for the bug. I was able to reproduce. We need to look into this and it's most likely due to an endless loop in the Chartist.getBounds function.

danieldiekmeier commented 9 years ago

I also encountered this error. Would appreciate a fix. <3

nteath commented 9 years ago

+1 on this bug. Great library by the way, thank you

antonigiske commented 9 years ago

The issues seems to be in the Pollard Rho algorithm (Chartist.rho). Seems to work great if I add this snippet of code:

Chartist.rho = function(num) {

        if (num == 1) {
            return 1;
        }

But that is probably not a legit fix.

antonigiske commented 9 years ago

On line 518 in chartist.js

do {
    x1 = f(x1) % num;
    x2 = f(f(x2)) % num;
    divisor = gcd(Math.abs(x1 - x2), num);

    console.log(divisor) // This will always return 1 and thus endlessly loop.

} while (divisor === 1);
gionkunz commented 9 years ago

Maybe you can try to find why the condition of only having data of 0 and 1 causes the issue? Ill look into it this weekend otherwise and plan a 0.8.1 release. Id also appreciate more investigation and a PR if anyone finds the time.

gionkunz commented 9 years ago

@antonigiske Actually after looking at the problem, your proposed fix is absolutely fine. Chartist.rho should return 1 immediately if called with 1.

gionkunz commented 9 years ago

I've added the fix to develop.