Skip to content

Commit 500299d

Browse files
committed
add line chart with custom labels
1 parent d947214 commit 500299d

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

src/lib/components/data-vis/line-chart/LineChart.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@
336336
{activeMarkerId}
337337
labelColor="lightgrey"
338338
labelTextColor="black"
339+
{labelText}
339340
{tooltipContent}
340341
{xFunction}
341342
{yFunction}

src/lib/components/data-vis/line-chart/Lines.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
{#each Object.keys(tieredDataObject) as tier}
6767
<g id={tier}>
6868
<g {...globalTierRules[tier]}>
69+
{console.log(globalTierRules[tier])}
6970
{#each tieredDataObject[tier] as line, i}
7071
<Line
7172
{...line}

src/lib/components/data-vis/line-chart/SeriesLabel.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,6 @@
6262
stroke-width={clickedSeries ? 0.7 : 0}
6363
stroke="black"
6464
>
65-
{labelText(dataArray)}
65+
{typeof labelText === "function" ? labelText(dataArray) : labelText}
6666
</text>
6767
</g>

src/wrappers/components/data-vis/line-chart/line-chart/Examples.svelte

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import LineChart from "$lib/components/data-vis/line-chart/LineChart.svelte";
1010
1111
let { data } = $props();
12+
1213
const lineChartData = (() => {
1314
const found = data.dataInFormatForLineChart.find(
1415
(el) => el.metric === "Household waste recycling rate",
@@ -23,6 +24,13 @@
2324
return found ? { ...found, lines: found.lines.slice(0, 1) } : null;
2425
})();
2526
27+
const theData = (() => {
28+
const found = data.dataInFormatForLineChart.find(
29+
(el) => el.metric === "Household waste recycling rate",
30+
);
31+
return found;
32+
})();
33+
2634
let accordionSnippetSections = [
2735
{
2836
id: "1",
@@ -49,13 +57,22 @@
4957
heading: "5. Line chart with shaded area",
5058
content: Example5,
5159
},
60+
{
61+
id: "6",
62+
heading: "6. Line chart with custom labels",
63+
content: Example6,
64+
},
5265
];
5366
5467
let activeMarkerId = $state();
5568
let clickedSeries = $state();
5669
let hoveredSeries = $state();
5770
let clickedTier = $state();
5871
let hoveredTier = $state();
72+
$inspect({ clickedTier, hoveredTier });
73+
let nothingSelected = $derived(
74+
[clickedSeries, hoveredSeries].every((item) => item == null),
75+
);
5976
</script>
6077

6178
<div>
@@ -175,3 +192,80 @@
175192
</div>
176193
<CodeBlock code={codeBlocks.codeBlock1} language="svelte"></CodeBlock>
177194
{/snippet}
195+
196+
{#snippet Example6()}
197+
<div class="p-5 bg-white">
198+
<LineChart
199+
lineChartData={theData}
200+
x="x"
201+
y="y"
202+
series="areaCode"
203+
basicLineParams={{ interactiveLines: true }}
204+
tieredLineParams={{
205+
hover: {
206+
pathStrokeWidth: 5,
207+
pathStrokeColor: hoveredTier === "secondary" ? "grey" : null,
208+
},
209+
clicked: {
210+
pathStrokeWidth: 6,
211+
pathStrokeColor: clickedTier === "secondary" ? "grey" : null,
212+
},
213+
secondary: { pathStrokeColor: "grey", halo: true },
214+
primary: {
215+
halo: true,
216+
pathStrokeWidth: 4,
217+
},
218+
}}
219+
bind:clickedSeries
220+
bind:hoveredSeries
221+
bind:clickedTier
222+
bind:hoveredTier
223+
getLine={(tier, el) => {
224+
if (tier === "primary") {
225+
return ["E09000033"].includes(el.areaCode);
226+
}
227+
if (tier === "secondary") {
228+
return [
229+
"E09000025",
230+
"E09000033",
231+
"E09000030",
232+
"E09000032",
233+
"E06000004",
234+
"E09000030",
235+
].includes(el.areaCode);
236+
}
237+
if (tier === "hover") {
238+
return [hoveredSeries].includes(el.areaCode);
239+
}
240+
if (tier === "clicked") {
241+
return [clickedSeries].includes(el.areaCode);
242+
}
243+
}}
244+
overrideLineParams={(tier, el) => {
245+
return {
246+
placeLabel:
247+
[hoveredSeries, clickedSeries].includes(el.areaCode) ||
248+
(tier === "primary" &&
249+
(nothingSelected ||
250+
[hoveredTier, clickedTier].includes("primary"))),
251+
showLabel:
252+
[hoveredSeries, clickedSeries].includes(el.areaCode) ||
253+
(tier === "primary" && nothingSelected) ||
254+
(!clickedSeries && hoveredTier === "primary" && tier === "primary"),
255+
};
256+
}}
257+
labelText={(dataArray) => {
258+
const areaNames = {
259+
E09000033: "A",
260+
E09000025: "B",
261+
E09000032: "C",
262+
E06000004: "D",
263+
E09000030: "E",
264+
};
265+
return areaNames[dataArray["areaCode"]] ?? dataArray["areaCode"];
266+
}}
267+
{nothingSelected}
268+
></LineChart>
269+
</div>
270+
<CodeBlock code={codeBlocks.codeBlock1} language="svelte"></CodeBlock>
271+
{/snippet}

0 commit comments

Comments
 (0)