kurkle / chartjs-plugin-gradient

Easy gradients for Chart.js
MIT License
37 stars 6 forks source link

Library panics when legend is disabled #30

Closed deen13 closed 2 years ago

deen13 commented 2 years ago

I noticed that the library crashes with a legend disabled by the display property. My first workaround was to set the legend property to false but I think it would be nice to respect the property.

Configuration that causes the error

plugins: {
  legend: {
    display: false,
  },
}

The actual stacktrace

TypeError: Cannot read properties of undefined (reading 'top')
    at buildArea (chartjs-plugin-gradient.js?cb17:175:1)
    at applyGradientToLegendByDatasetIndex (chartjs-plugin-gradient.js?cb17:187:1)
    at updateLegendItems (chartjs-plugin-gradient.js?cb17:227:1)
    at Object.afterUpdate (chartjs-plugin-gradient.js?cb17:342:1)
    at callback (helpers.segment.js?a6be:92:1)
    at PluginService._notify (chart.esm.js?efac:4922:1)
    at PluginService.notify (chart.esm.js?efac:4909:1)
    at Chart.notifyPlugins (chart.esm.js?efac:6047:1)
    at Chart.update (chart.esm.js?efac:5637:1)
    at new Chart (chart.esm.js?efac:5402:1)

The code that might cause the error

https://github.com/kurkle/chartjs-plugin-gradient/blob/c7140dd6410ce4feb2bea3577e98b2d66545f3ac/src/legend.js#L36