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

Fix chart crashing when max is defined but ticks are empty #9641

Merged
merged 2 commits into from Oct 4, 2021

Conversation

KurtPreston
Copy link
Contributor

If tick size is 0, and a max is defined without min, Chart.js would crash with:

react_devtools_backend.js:4049 Uncaught TypeError: Cannot read property 'value' of undefined TypeError: Cannot read property 'value' of undefined
    at generateTicks$1 (chart.esm.js:9359)
    at LinearScale.buildTicks (chart.esm.js:9461)
    at LinearScale.update (chart.esm.js:3810)
    at fitBoxes (chart.esm.js:2852)
    at Object.update (chart.esm.js:2988)
    at Chart._updateLayout (chart.esm.js:5657)
    at Chart.update (chart.esm.js:5638)
    at updateChart (index.modern.js:137)
    at index.modern.js:157

This adds an extra check to prevent ticks[ticks.length - 1] from erroring.

@etimberg
Copy link
Member

The change looks fine. is it possible to write a test case for this?

@KurtPreston
Copy link
Contributor Author

KurtPreston commented Sep 14, 2021

@etimberg I've pushed an update with a new spec.

Without the code change, the following error is thrown when the spec is executing:

        TypeError: Cannot read property 'value' of undefined
            at generateTicks$1 (src/chart.js:11970:46)
            at LinearScale.buildTicks (src/chart.js:12072:19)
            at LinearScale.update (src/chart.js:5035:19)
            at fitBoxes (src/chart.js:2144:9)
            at Object.update (src/chart.js:2280:5)
            at Chart._updateLayout (src/chart.js:6889:13)
            at Chart.update (src/chart.js:6870:8)
            at new Chart (src/chart.js:6626:10)
            at _acquireChart (node_modules/chartjs-test-utils/dist/chartjs-test-utils.js:601:13 <- test/index.js:606:15)
            at acquireChart (node_modules/chartjs-test-utils/dist/chartjs-test-utils.js:1136:15 <- test/index.js:1141:17)

@kurkle
Copy link
Member

kurkle commented Sep 16, 2021

There seems to be another related case:

    var chart = window.acquireChart({
      type: 'line',
      data: {
        datasets: [{
          data: [200]
        }],
      },
      options: {
        scales: {
          y: {
            min: 250
          }
        }
      }
    });

    expect(chart.scales.y.min).toBe(250);
    RangeError: minimumFractionDigits value is out of range.
      at new NumberFormat (<anonymous>)
       at getNumberFormat (src/chart.js:2822:17)
       at formatNumber (src/chart.js:2828:10)
       at LinearScale.numeric (src/chart.js:4589:12)
       at callback (src/chart.js:802:15)
       at LinearScale.generateTickLabels (src/chart.js:5145:20)
       at LinearScale._convertTicksToLabels (src/chart.js:5298:8)
       at LinearScale.update (src/chart.js:5055:8)
       at fitBoxes (src/chart.js:2144:9)
       at Object.update (src/chart.js:2280:5)

Both cases are fixed by making sure min <= max in the getMinMax of core.scale

    // Make sure min <= max when only min or max is defined by user and the data is outside that range
    min = maxDefined && min > max ? max : min;
    max = minDefined && min > max ? min : max;

    return {
      min: finiteOrDefault(min, finiteOrDefault(max, min)),
      max: finiteOrDefault(max, finiteOrDefault(min, max))
    };

@kurkle
Copy link
Member

kurkle commented Sep 16, 2021

So, my question is, are you willing to fix the other case here too, or should it be handled separately?
I think the length check should be included anyway.

Copy link
Member

@etimberg etimberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kurkle had a good comment. I think it'd be good to address the two cases together

@kurkle kurkle added this to the Version 3.6.0 milestone Sep 17, 2021
@kurkle kurkle merged commit 60b094a into chartjs:master Oct 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants