Getting Cannot read properties of undefined (reading 'xAxisID') when trying to create a basic line chart #12192
-
|
I am trying to create a line chart in Chart JS 4.5.1. It's complaining that I didn't define xAxisID (I don't) in the dataset I am passing in. AFAICT, though, defining it should not be necessary and it should use the default value of "x". Here's the error: My config for the chart looks like Here's the function in the ChartJS code where it pukes: It fails when trying to compare |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
@TWDBrob the issue isn't your // your map produces: [{ data, label, ... }, undefined, { data, label, ... }, undefined]
config.data.datasets.filter((d) => d.xAxisID === id)
// ^ crashes on undefined elementfix: use const getServiceChartData = () =>
servicesReport['report-data'][0]
.filter((dataSet) => isServiceSelected(dataSet.resourceURI))
.map((dataSet) => ({
data: dataSet.data,
label: prettyServiceName(dataSet.resourceURI),
borderWidth: 1,
}));or if you want to keep the single-pass approach, use const getServiceChartData = () =>
servicesReport['report-data'][0].flatMap((dataSet) =>
isServiceSelected(dataSet.resourceURI)
? [{ data: dataSet.data, label: prettyServiceName(dataSet.resourceURI), borderWidth: 1 }]
: []
);you don't need to set |
Beta Was this translation helpful? Give feedback.
@TWDBrob the issue isn't
xAxisID, it's yourgetServiceChartData()function returningundefinedelements in the datasets array.your
.map()only returns a dataset object whenisServiceSelected()is true. when it's false, there's no explicit return, so the array slot isundefined. chart.js then iterates the datasets and tries to read.xAxisIDonundefined:fix: use
.filter()before.map()so you never have undefined entries: