Skip to content

Commit f25f6d5

Browse files
authored
fix(AreaChart|BarChar|LineChart): Use value axis (typically y) property name/accessor for tooltip label if defined as string (ex. <AreaChart x="date" y="visitors"> will use visitors instead of value) (#523)
1 parent 91b6213 commit f25f6d5

5 files changed

Lines changed: 45 additions & 2 deletions

File tree

.changeset/better-pets-divide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'layerchart': patch
3+
---
4+
5+
fix(AreaChart|BarChar|LineChart): Use value axis (typically y) property name/accessor for tooltip label if defined as string (ex. `<AreaChart x="date" y="visitors">` will use `visitors` instead of `value`)

packages/layerchart/src/lib/components/charts/AreaChart.svelte

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,14 @@
135135
136136
const series = $derived(
137137
seriesProp === undefined
138-
? [{ key: 'default', value: y, color: 'var(--color-primary)' }]
138+
? [
139+
{
140+
key: 'default',
141+
label: typeof y === 'string' ? y : 'value',
142+
value: y,
143+
color: 'var(--color-primary)',
144+
},
145+
]
139146
: seriesProp
140147
);
141148

packages/layerchart/src/lib/components/charts/BarChart.svelte

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@
159159
? [
160160
{
161161
key: 'default',
162+
label:
163+
orientation === 'vertical'
164+
? typeof yProp === 'string'
165+
? yProp
166+
: 'value'
167+
: typeof xProp === 'string'
168+
? xProp
169+
: 'value',
162170
value: orientation === 'vertical' ? yProp : xProp,
163171
},
164172
]

packages/layerchart/src/lib/components/charts/LineChart.svelte

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,14 @@
141141
142142
const series = $derived(
143143
seriesProp === undefined
144-
? [{ key: 'default', value: yProp, color: 'var(--color-primary)' }]
144+
? [
145+
{
146+
key: 'default',
147+
label: typeof yProp === 'string' ? yProp : 'value',
148+
value: yProp,
149+
color: 'var(--color-primary)',
150+
},
151+
]
145152
: seriesProp
146153
);
147154
const seriesState = new SeriesState(() => series);

packages/layerchart/src/routes/docs/components/AreaChart/+page.svelte

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@
4242
};
4343
})
4444
);
45+
const dataVisits = $derived(
46+
dateSeriesData.map((d) => {
47+
return {
48+
...d,
49+
visits: d.value,
50+
};
51+
})
52+
);
4553
4654
const now = new Date();
4755
const denseDateSeriesData = randomWalk({ count: 1000 }).map((value, i) => ({
@@ -173,6 +181,14 @@
173181
</div>
174182
</Preview>
175183

184+
<h2>Default series label</h2>
185+
186+
<Preview data={dataVisits}>
187+
<div class="h-[300px] p-4 border rounded-sm">
188+
<AreaChart data={dataVisits} x="date" y="visits" {renderContext} {debug} />
189+
</div>
190+
</Preview>
191+
176192
<h2>Gradient</h2>
177193
<Preview data={dateSeriesData}>
178194
<div class="h-[300px] p-4 border rounded-sm">

0 commit comments

Comments
 (0)