Closed mewforest closed 2 weeks ago
How are you updating the data to the chart?
Here is part of the code where the datasets are updated (I am using Vue 3 btw):
import type { ChartData } from "chart.js";
import { Chart as ChartJS } from "chart.js";
// chart
let chart: ChartJS<"line"> | null = null;
// chart datasets
const chartData: ChartData<"line", any[], string> = {
datasets: [],
};
// Initializing chart on component start
onMounted(async () => {
const ctxElement = document.getElementById("chart-id");
if (!ctxElement) return;
const ctx = (ctxElement as HTMLCanvasElement).getContext("2d");
if (!ctx) return;
chart = Object.seal(
new ChartJS(ctx, {
type: "line",
data: chartData,
options: {
responsive: true,
maintainAspectRatio: false,
aspectRatio: 2,
},
plugins: [
/* my tooltip plugin goes here */
],
}) as any
);
});
// Updating datasets
async function fetchData(update = false) {
let data = [];
/* getting here `data` from api with fetch */
chartData.datasets = data;
chart.options = {
/* my custom options, including plugins.zoom option, I've sent earlier */
};
chart.update();
}
I've found a temporary solution: when the number of datasets decreases, just set deleted records to `data: null' instead of removing them.
The error is raised from there: https://github.com/chartjs/Chart.js/blob/bb82c8f549374e552cc237593749eb6513d4d534/src/core/core.controller.js#L424
Which suggests this
is undefined. Probably specific to Vue.
If the workaround is not enough, you should open an issue in Chart.js repository with a minimal reproduction.
As there is nothing to be done in this plugin, I'm closing this issue.
This error occurred when zooming a chart and the number of datasets is decreasing:
Here
ChartComponent.vue:917:9
is just achart.update()
with updated dataset I've got from my API.This is how chartjs-plugin-zoom is configured in my case: