Skip to content

Tooltip data when using multiple series #673

@teobouvard

Description

@teobouvard

Hey 👋

I'm using layerchart==2.0.0-next.42.

I'm trying to get the tooltip data when hovering over a LineChart using multiple series, but the returned values only contain a single series.

Using the following piece of code (adapted from the "Series (separate data)" of LineChart docs:

<script lang="ts">
	import { group } from "d3-array";
	import { startOfToday, subDays } from "date-fns";
	import { ChartContextValue, LineChart, pivotLonger } from "layerchart@next";

	let chartContext: ChartContextValue | undefined = $state();
	let tooltipData = $derived(chartContext?.tooltip?.data);
	$effect(() => {
		console.log($state.snapshot(tooltipData));
	});

	export function getRandomInteger(min: number, max: number, includeMax = true) {
		min = Math.ceil(min);
		max = Math.floor(max);
		return Math.floor(Math.random() * (max - min + (includeMax ? 1 : 0)) + min);
	}

	export function createDateSeries<TKey extends string>(options: {
		count?: number;
		min: number;
		max: number;
		keys?: TKey[];
		value?: "number" | "integer";
	}) {
		const now = startOfToday();

		const count = options.count ?? 10;
		const min = options.min;
		const max = options.max;
		const keys = options.keys ?? ["value"];

		return Array.from({ length: count }).map((_, i) => {
			return {
				date: subDays(now, count - i - 1),
				...Object.fromEntries(
					keys.map((key) => {
						return [
							key,
							options.value === "integer" ? getRandomInteger(min, max) : getRandomNumber(min, max)
						];
					})
				)
			} as { date: Date } & { [K in TKey]: number };
		});
	}

	const keys = ["apples", "bananas", "oranges"];
	const multiSeriesData = createDateSeries({
		count: 30,
		min: 10,
		max: 100,
		value: "integer",
		keys
	});
	const multiSeriesFlatData = pivotLonger(multiSeriesData, keys, "fruit", "value");
	const multiSeriesDataByFruit = group(multiSeriesFlatData, (d) => d.fruit);
</script>

<div class="h-[300px] w-full p-4 border rounded-sm">
	<LineChart
		x="date"
		y="value"
		bind:context={chartContext}
		series={[
			{
				key: "apples",
				data: multiSeriesDataByFruit.get("apples"),
				color: "var(--color-danger)"
			},
			{
				key: "bananas",
				data: multiSeriesDataByFruit.get("bananas"),
				color: "var(--color-success)"
			},
			{
				key: "oranges",
				data: multiSeriesDataByFruit.get("oranges"),
				color: "var(--color-warning)"
			}
		]}
	/>
</div>

I get this output in the console:

{ seriesKey: "oranges" ,date: 2025-10-11T22:00:00.000Z ,fruit: "oranges" ,value: 19 }
{ seriesKey: "oranges" ,date: 2025-10-11T22:00:00.000Z ,fruit: "oranges" ,value: 19 }
{ seriesKey: "oranges" ,date: 2025-10-11T22:00:00.000Z ,fruit: "oranges" ,value: 19 }
{ seriesKey: "oranges" ,date: 2025-10-11T22:00:00.000Z ,fruit: "oranges" ,value: 19 }
{ seriesKey: "oranges" ,date: 2025-10-11T22:00:00.000Z ,fruit: "oranges" ,value: 19 }
...

Am I doing something wrong ? How can I get the tooltip data for all series in the chart ?

Svelte playground repro (need to manually enable tailwind)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions