|
9 | 9 | import LineChart from "$lib/components/data-vis/line-chart/LineChart.svelte";
|
10 | 10 |
|
11 | 11 | let { data } = $props();
|
| 12 | +
|
12 | 13 | const lineChartData = (() => {
|
13 | 14 | const found = data.dataInFormatForLineChart.find(
|
14 | 15 | (el) => el.metric === "Household waste recycling rate",
|
|
23 | 24 | return found ? { ...found, lines: found.lines.slice(0, 1) } : null;
|
24 | 25 | })();
|
25 | 26 |
|
| 27 | + const theData = (() => { |
| 28 | + const found = data.dataInFormatForLineChart.find( |
| 29 | + (el) => el.metric === "Household waste recycling rate", |
| 30 | + ); |
| 31 | + return found; |
| 32 | + })(); |
| 33 | +
|
26 | 34 | let accordionSnippetSections = [
|
27 | 35 | {
|
28 | 36 | id: "1",
|
|
49 | 57 | heading: "5. Line chart with shaded area",
|
50 | 58 | content: Example5,
|
51 | 59 | },
|
| 60 | + { |
| 61 | + id: "6", |
| 62 | + heading: "6. Line chart with custom labels", |
| 63 | + content: Example6, |
| 64 | + }, |
52 | 65 | ];
|
53 | 66 |
|
54 | 67 | let activeMarkerId = $state();
|
55 | 68 | let clickedSeries = $state();
|
56 | 69 | let hoveredSeries = $state();
|
57 | 70 | let clickedTier = $state();
|
58 | 71 | let hoveredTier = $state();
|
| 72 | + $inspect({ clickedTier, hoveredTier }); |
| 73 | + let nothingSelected = $derived( |
| 74 | + [clickedSeries, hoveredSeries].every((item) => item == null), |
| 75 | + ); |
59 | 76 | </script>
|
60 | 77 |
|
61 | 78 | <div>
|
|
175 | 192 | </div>
|
176 | 193 | <CodeBlock code={codeBlocks.codeBlock1} language="svelte"></CodeBlock>
|
177 | 194 | {/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