chartjs-plugin-annotation: Cannot set properties of undefined (setting 'display') at resolveLabelElementProperties
We seem to be stuck with plugin-annotation v1.3.0 and chartjs v3.7.0 where following example for a line chart with multiple datasets works with multiple annotations.
https://codepen.io/dbauszus-glx/pen/MWOjGzw
Upgrading plugin-annotation to v2.0.0 causes following TypeError: Cannot set properties of undefined (setting 'display') at resolveLabelElementProperties.
Upgrading chartjs to v3.9.1 causes a different TypeError: Cannot read properties of undefined (reading 'lastIndexOf')
The same error persists with chartjs on v3.9.1 and plugin-aanotation on v2.0.0
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 18
I’m trying to use this plugin in an Ember (4.x) app, and I’ve run across this issue as well. From what I can tell, the issue for me stems from the fact that:
chart.js/auto(version 4.1.x) tochart.js/auto/auto.cjs(the CommonJS module), which then importschart.js/dist/chart.cjs, whilechartjs-plugin-annotation(version 2.1.1) gets mapped tochartjs-plugin-annotation/dist/chartjs-plugin-annotation.esm.js(the ESM module), which then importschart.js/dist/chart.js.Minimal Repro
I am able to reproduce the issue in a minimal project with:
using the command line:
and the following
index.jsfile modeled off the basic line sample:And the following
index.html:Analysis
Obviously the issue is that Webpack is choosing inconsistent module types for chart.js vs this plugin.
If I replace the first lines with:
Then the issue goes away, because Webpack uses the ESM versions of each.
It’s clear to me why Webpack is choosing the CJS when using
require('chart.js/auto')but it was less clear to me why it was using ESM forrequire('chartjs-plugin-annotation'). Probably because it prefersmoduletomainby default?Fortunately I just noticed that #832 was merged recently, and if I apply that change to my local
node_modules/chartjs-plugin-annotation/package.json, then this issue is resolved because Webpack correctly chooseschartjs-plugin-annotation/dist/chartjs-plugin-annotation.min.js, which then usesrequire("chart.js"), which Webpack maps tochart.js/dist/chart.cjs, thereby eliminating the inconsistent versions.So I think this issue (at least for me) should be resolved with the next version of this plugin that includes #832.
As a workaround (for now or in perpetuity), I can introduce an intermediate ESM module that uses
importfor all Chart.js plugins, so that Webpack properly and consistently chooses the ESM version of each.hey @stockiNail, I’ve just tried to reproduce this in a sandbox, but it does work there.
In my setup, I’m generating the charts on the backend with nodejs (using ChartJSNodeCanvas). Probably that’s something on their side then…
Sorry for the late reply as I was on holidays. I just checked this now and chart 3.9.1 with plugin annotation 2.0.1 works as expected if imported through skypack. Thanks for looking into this.