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

pie chart - data labels - ERROR #141

Closed
kevinjamesbaker opened this issue Jul 11, 2019 · 14 comments
Closed

pie chart - data labels - ERROR #141

kevinjamesbaker opened this issue Jul 11, 2019 · 14 comments
Labels

Comments

@kevinjamesbaker
Copy link

"chart.js": "2.8.0",
"chartjs-node-canvas": "2.4.0",
"chartjs-plugin-datalabels": "^0.6.0"

{"type":"pie","data":{"labels":["Managed Ca..","Medicare A","Private"],"datasets":[{"data":[101,102,103],"label":"Daily Census","backgroundColor":["rgba(54, 162, 235, 0.2)","rgba(255, 206, 86, 0.2)","rgba(75, 192, 192, 0.2)"],"borderColor":["rgba(54, 162, 235, 1)","rgba(255, 206,
86, 1)","rgba(75, 192, 192, 1)"],"borderWidth":0,"format":"0.00a"}]},"options":null}

function orient(point, origin) {
var x0 = origin.x; <-- TypeError: Cannot read property 'x' of null
var y0 = origin.y;

@simonbrunel
Copy link
Member

@kevinjamesbaker "options": null looks quite suspect but can't test it, please provide a codepen that reproduces your issue.

@simonbrunel
Copy link
Member

Closing as outdated, feel free to re-open with the requested additional info.

@imajus
Copy link

imajus commented Jun 24, 2021

I've recently faced the same issue. Here's Codepen script which demonstrates the issue in action. See browser console for the errors there.

The chart sometimes renders fine, but if you're lucky enough it will stuck at animation. But even if it's rendered properly the same error occurs whenever you hover the doughnut areas with a mouse pointer.

Hope you'll be able to shed some light on the issue, cause it looks like the one which is very hard to debug properly without a deep knowledge of the library sources.

@jermerf
Copy link

jermerf commented Sep 21, 2021

The issue seems to be that with doughnut/pie charts the vScale is optional, per the Chartjs docs and if there's no scale, the origin is null

function getScaleOrigin(el, context) {
  var scale = context.chart.getDatasetMeta(context.datasetIndex).vScale;

  if (!scale) {
    return null;
  }
...

So when the x/y properties are grabbed off the null origin, explosion

function orient(point, origin) {
  var x0 = origin.x;
  var y0 = origin.y;
...

@KELs7
Copy link

KELs7 commented Oct 26, 2022

So what's the work around of vScale property for pie charts?

@souravpadhee
Copy link

I am using chartjs in nodejs using chartjs-node-canvas to generate charts and facing same issue when trying to generate multiple charts.

First chart rendering perfectly. When I am trying to generate the second chart its throwing error var x0 = origin.x;

@dreamfalcon
Copy link

dreamfalcon commented Mar 29, 2023

Same problem here with pie and doughnut charts.
"chart.js": "4.2.1",
"chartjs-plugin-datalabels": "2.2.0",
"react-chartjs-2": "5.2.0",
Any idea how to solve?


Edit:
The same code works on codepen, but not in my project, the origin is always null.

@diegobill
Copy link

Same problem here when using Pie Chart:

TypeError: Cannot read properties of null (reading 'x')
  at orient(webpack-internal:https:///../../../../node_modules/chartjs-plugin-datalabels/dist/chartjs-plugin-datalabels.esm.js:121:19)
  at Object.fallback(webpack-internal:https:///../../../../node_modules/chartjs-plugin-datalabels/dist/chartjs-plugin-datalabels.esm.js:345:13)
  at coordinates(webpack-internal:https:///../../../../node_modules/chartjs-plugin-datalabels/dist/chartjs-plugin-datalabels.esm.js:841:21)
  at Object.draw(webpack-internal:https:///../../../../node_modules/chartjs-plugin-datalabels/dist/chartjs-plugin-datalabels.esm.js:1012:18)
  at Object.afterDatasetsDraw(webpack-internal:https:///../../../../node_modules/chartjs-plugin-datalabels/dist/chartjs-plugin-datalabels.esm.js:1307:12)
  at callback(webpack-internal:https:///./node_modules/chart.js/dist/chunks/helpers.segment.mjs:172:15)
  at PluginService._notify(webpack-internal:https:///./node_modules/chart.js/dist/chart.mjs:5037:80)
  at PluginService.notify(webpack-internal:https:///./node_modules/chart.js/dist/chart.mjs:5024:25)
  at Chart.notifyPlugins(webpack-internal:https:///./node_modules/chart.js/dist/chart.mjs:6173:26)
  at Chart._drawDatasets(webpack-internal:https:///./node_modules/chart.js/dist/chart.mjs:5920:10)
  at Chart.draw(webpack-internal:https:///./node_modules/chart.js/dist/chart.mjs:5891:10)
  at eval(webpack-internal:https:///./node_modules/chart.js/dist/chart.mjs:113:15)
  at Map.forEach(<anonymous>)
  at Animator._update(webpack-internal:https:///./node_modules/chart.js/dist/chart.mjs:91:18)
  at eval(webpack-internal:https:///./node_modules/chart.js/dist/chart.mjs:82:12)

Any solution?

@alberchou
Copy link

alberchou commented Apr 27, 2023

Hello, I'm having the same issue:

TypeError: Cannot read properties of null (reading 'x')
      at orient (H:\GUI\New Devs\chatbot-bolt\node_modules\chartjs-plugin-datalabels\dist\chartjs-plugin-datalabels.js:121:19)
      at Object.fallback [as positioner] (H:\GUI\New Devs\chatbot-bolt\node_modules\chartjs-plugin-datalabels\dist\chartjs-plugin-datalabels.js:345:13)
      at coordinates (H:\GUI\New Devs\chatbot-bolt\node_modules\chartjs-plugin-datalabels\dist\chartjs-plugin-datalabels.js:841:21)
      at Object.draw (H:\GUI\New Devs\chatbot-bolt\node_modules\chartjs-plugin-datalabels\dist\chartjs-plugin-datalabels.js:1012:18)
      at Object.afterDatasetsDraw (H:\GUI\New Devs\chatbot-bolt\node_modules\chartjs-plugin-datalabels\dist\chartjs-plugin-datalabels.js:1305:12)
      at callback (file:https:///H:/GUI/New%20Devs/chatbot-bolt/node_modules/chart.js/dist/chunks/helpers.segment.js:79:19)
      at PluginService._notify (file:https:///H:/GUI/New%20Devs/chatbot-bolt/node_modules/chart.js/dist/chart.js:5063:17)
      at PluginService.notify (file:https:///H:/GUI/New%20Devs/chatbot-bolt/node_modules/chart.js/dist/chart.js:5046:29)
      at Chart.notifyPlugins (file:https:///H:/GUI/New%20Devs/chatbot-bolt/node_modules/chart.js/dist/chart.js:6301:30)
      at Chart._drawDatasets (file:https:///H:/GUI/New%20Devs/chatbot-bolt/node_modules/chart.js/dist/chart.js:6044:14)

This only happens to me when using the ES2015 syntax (import ChartDataLabels from 'chartjs-plugin-datalabels';).
With CommonJS syntax (const { ChartDataLabels } = require('chartjs-plugin-datalabels');) seems to work properly...

Versions installed:
npm list chart.js chartjs-plugin-datalabels
+-- [email protected]
+-- [email protected]
| -- [email protected] deduped +-- [email protected] | -- [email protected] deduped
-- [email protected] -- [email protected] deduped

@marcusleandro
Copy link

any solution, guys. Don't believe that do a downgrade is the way.

@abforce
Copy link

abforce commented Jun 5, 2023

The problem occurs at the getPositioner function.

function getPositioner(el) {
    if (el instanceof chart_js.ArcElement) {
      return positioners.arc;
    }
    if (el instanceof chart_js.PointElement) {
      return positioners.point;
    }
    if (el instanceof chart_js.BarElement) {
      return positioners.bar;
    }
    return positioners.fallback;
  }

In this function when using Pie or Doughnut charts at the first if should return but it happens that the condition el instanceof chart_js.ArcElement isn't evaluated to true, because chart_js (that is require(chart.js)) is not equal to the chart.js which calling this plugin.

As a workaround I rewrite that function as below and it works fine.

function getPositioner(el) {
    if (el.constructor.name === 'ArcElement') {
      return positioners.arc;
    }
    if (el.constructor.name === 'PointElement') {
      return positioners.point;
    }
    if (el.constructor.name === 'BarElement') {
      return positioners.bar;
    }
    return positioners.fallback;
  }

@libnine
Copy link

libnine commented Jul 21, 2023

The problem occurs at the getPositioner function.

function getPositioner(el) {
    if (el instanceof chart_js.ArcElement) {
      return positioners.arc;
    }
    if (el instanceof chart_js.PointElement) {
      return positioners.point;
    }
    if (el instanceof chart_js.BarElement) {
      return positioners.bar;
    }
    return positioners.fallback;
  }

In this function when using Pie or Doughnut charts at the first if should return but it happens that the condition el instanceof chart_js.ArcElement isn't evaluated to true, because chart_js (that is require(chart.js)) is not equal to the chart.js which calling this plugin.

As a workaround I rewrite that function as below and it works fine.

function getPositioner(el) {
    if (el.constructor.name === 'ArcElement') {
      return positioners.arc;
    }
    if (el.constructor.name === 'PointElement') {
      return positioners.point;
    }
    if (el.constructor.name === 'BarElement') {
      return positioners.bar;
    }
    return positioners.fallback;
  }

I can issue a PR for this, this is the fix

tcmacdonald added a commit to ample/chartjs-plugin-datalabels that referenced this issue Oct 20, 2023
@DerLordi
Copy link

When will the new release be on npmjs. Or is there another way to get this fixed version?

@Whobeu
Copy link

Whobeu commented Dec 12, 2023

I was going to post an issue I am having using chartjs-plugin-datalabels with an ESM project in node. It works fine as a CommonJS module but changing the syntax to ESM standard and I get the same issue in getPositioner with the "instanceof" failing. Patched the module as noted above by @libnine and it all works fine now. Definitely like this patch. It also works with the CommonJS code as well.

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

No branches or pull requests