-
Notifications
You must be signed in to change notification settings - Fork 40
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
[Bug]: Incompatible with Svelte 5 (pre-release) #158
Comments
I just updated my project to svelte [email protected] and I encountered the same issue. |
I've implemented a fix here: anowell@155042f It's basically a rewrite of src/util/svelte.ts to work directly with the DOM, and then changes to the base Chart component to setup event listeners more explicitly. It's certainly a breaking change, and I don't know that it's "correct", but it seems to work, still supports attaching event listeners (tested on the Bar chart storybook story), and I think it cleans them up. And I didn't mean to bump so many devDependencies, but I'm several years removed from the frontend dev tooling ecosystem so might have run a few too many unneeded pnpm commands. From here, what it would take for this repo/package to support Svelte 5? Is it a new version? Should we wait for Svelte 5 to release? Is there an expectation of Svelte 4 compatibility? Is there some glaring issue with the implementation? Is there some other testing expectation I've completely overlooked? I could put together a PR if the missing pieces are relatively small, or I'm happy to have someone steal whatever they need from my change to put together a better solution. Fwiw, I'm currently using it in a prototype with no intent to maintain beyond that prototype, so I advise against using my fork for anything beyond experimenting/testing. |
@SauravKanchan is this work planned? Would love to continue to use this library in svelte. |
Same issue here, this doesn't work for Svelte 5. |
Svelte 5 was released, this library doesn't work |
@anowell Can you propose a proper PR here (without |
This project isn't very active. If someone forks and fix, please let this thread know. If its not fixed by next month, I will do it. |
Following the repository's source code, I managed to replicate the behavior in Svelte 5 <script lang="ts">
import { BarController, BarElement, CategoryScale, Chart, LinearScale, TimeScale, Tooltip } from "chart.js";
import "chartjs-adapter-date-fns";
let elCanvas: HTMLCanvasElement;
Chart.register(Tooltip, BarElement, BarController, CategoryScale, LinearScale, TimeScale);
let chart: Chart;
onMount(() => {
chart = new Chart(elCanvas, {
type: "bar",
data,
options: {
scales: {
y: {
grace: "5%"
},
x: {
ticks: {
callback(i) {
// ...
},
color
}
}
},
plugins: {
legend: {
display: false
},
tooltip: {
callbacks: {
title(tooltipItem) {
// ...
}
}
}
},
animation: false
}
});
});
const themeInUse = $derived("light");
const color = $derived(themeInUse === "dark" ? "#fffFFF" : "#1B1B1B");
$effect(() => {
if (chart) {
chart.data = data;
chart.update();
}
});
$effect(() => {
if (chart) {
chart.config.options.scales.x.ticks.color = color;
chart.config.options.scales.y.ticks.color = color;
chart.update();
}
});
// https://www.chartjs.org/docs/latest/charts/bar.html
const data = $derived({
datasets: [
{
data: (() => {
// ...
}, []);
})(),
backgroundColor: "#1BE7FF"
}
]
});
</script>
<canvas bind:this={elCanvas}></canvas> |
took inspiration from @avi12 (thank you btw) and here's a complete working component for my hope is that you can use this in any case, here it is: <script lang="ts">
import {
Chart,
Tooltip,
type ChartData,
type ChartOptions,
} from 'chart.js';
import type { HTMLCanvasAttributes } from 'svelte/elements';
import 'chart.js/auto';
import 'chartjs-adapter-date-fns';
interface Props extends HTMLCanvasAttributes {
data: ChartData<'line', number[], string>;
options: ChartOptions<'line'>;
}
const { data, options, ...rest }: Props = $props();
Chart.register(Tooltip);
let canvasElem: HTMLCanvasElement;
let chart: Chart;
$effect(() => {
chart = new Chart(canvasElem, {
type: 'line',
data,
options,
});
return () => {
chart.destroy();
};
});
$effect(() => {
if (chart) {
chart.data = data;
chart.update();
}
});
</script>
<canvas bind:this={canvasElem} {...rest}></canvas> note a couple of things:
IMHO |
Would you like to work on a fix?
Current and expected behavior
Importing a chartjs component (e.g. Bar) with Svelte 5 ([email protected]) yields this error message:
I haven't looked any further than simply confirming the import in this library:
svelte-chartjs/src/utils/svelte.ts
Lines 1 to 6 in 4333128
Reproduction
[did not create shareable repro - merely confirmed svelte/internal is import]
chart.js version
4.4.2
svelte-chartjs version
3.1.5
Possible solution
No response
The text was updated successfully, but these errors were encountered: