Skip to content

Commit 292143d

Browse files
committed
add defaultLineParams within the line chart, so users can edit basicLineParams without interfering
1 parent 2ddfe98 commit 292143d

File tree

3 files changed

+91
-44
lines changed

3 files changed

+91
-44
lines changed

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

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@
3030
.x((d) => xFunction(d[x]))
3131
.y((d) => yFunction(d[y]))
3232
.curve(curveLinear),
33-
labelText,
33+
labelText = "labelText",
3434
35-
basicLineParams, // contains functions
36-
tooltipContent, // snippet
35+
tooltipContent = "tooltipContent",
3736
3837
onClickSeries = (series, tier) => {
3938
if (clickedSeries === dataId) {
@@ -55,15 +54,23 @@
5554
hoveredTier = tier;
5655
}
5756
},
58-
onClickMarker,
59-
onMouseEnterMarker,
60-
onMouseLeaveMarker,
57+
onClickMarker = (event, marker, markerId) => {
58+
activeMarkerId = marker;
59+
},
60+
onMouseEnterMarker = (event, marker, markerId) => {
61+
activeMarkerId = marker;
62+
},
63+
onMouseLeaveMarker = (event, marker, dataId) => {
64+
activeMarkerId = null;
65+
},
6166
6267
// Optional
6368
clickedSeries = $bindable(undefined),
6469
hoveredSeries = undefined,
6570
overrideLineParams = () => ({}),
66-
getLine = () => true,
71+
getLine = (key, el) => {
72+
return true;
73+
},
6774
nothingSelected = true,
6875
svgWidth = $bindable(500),
6976
svgHeight = 500,
@@ -88,8 +95,24 @@
8895
tieredLineParams = {
8996
all: {},
9097
},
98+
99+
basicLineParams = {},
91100
} = $props();
92101
102+
let defaultLineParams = $derived({
103+
xFunction,
104+
yFunction,
105+
lineFunction,
106+
onClickSeries,
107+
onMouseEnterSeries,
108+
onMouseLeaveSeries,
109+
onClickMarker,
110+
onMouseEnterMarker,
111+
onMouseLeaveMarker,
112+
haloColor: chartBackgroundColor,
113+
invisibleStrokeWidth: 20,
114+
});
115+
93116
let chartWidth = $derived(svgWidth - paddingLeft - paddingRight);
94117
let chartHeight = $derived(svgHeight - paddingTop - paddingBottom);
95118
// let areaFunction = $derived(
@@ -100,12 +123,7 @@
100123
// .curve(curveLinear),
101124
// );
102125
103-
let selectedLine = $derived([
104-
hoveredSeries,
105-
clickedSeries,
106-
hoveredSeries,
107-
clickedSeries,
108-
]);
126+
let selectedLine = $derived([hoveredSeries, clickedSeries]);
109127
110128
function handleClickOutside(event) {
111129
if (
@@ -123,10 +141,12 @@
123141
overrideLineParams,
124142
tieredLineParams,
125143
basicLineParams,
144+
defaultLineParams,
126145
) {
127146
const listOfProperties = [
128147
...new Set([
129-
...Object.keys(basicLineParams),
148+
...Object.keys(defaultLineParams),
149+
...Object.keys(basicLineParams ?? {}),
130150
...Object.keys(tieredLineParams[tier] ?? {}),
131151
...Object.keys(overrideLineParams(tier, line)),
132152
]),
@@ -137,7 +157,8 @@
137157
key,
138158
overrideLineParams(tier, line)[key] ??
139159
tieredLineParams[tier]?.[key] ??
140-
basicLineParams[key],
160+
basicLineParams[key] ??
161+
defaultLineParams[key],
141162
]),
142163
);
143164
@@ -160,9 +181,9 @@
160181
overrideLineParams,
161182
tieredLineParams,
162183
basicLineParams,
184+
defaultLineParams,
163185
),
164186
);
165-
166187
return acc;
167188
}, {}),
168189
);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,5 +899,5 @@ DONOTTOUCH *
899899
&& Creates a list of examples where the component is used (if any examples exist).
900900
-->
901901
<div id="examples" data-role="examples-section">
902-
<Examples></Examples>
902+
<Examples {data}></Examples>
903903
</div>
Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
11
<script>
22
import { AccordionItem, Accordion } from "flowbite-svelte";
3+
import Template from "$lib/package-wrapping/templates/Template.svelte";
34
45
import CodeBlock from "$lib/package-wrapping/CodeBlock.svelte";
56
import * as codeBlocks from "./codeBlocks.js";
67
78
import LineChart from "$lib/components/data-vis/line-chart/LineChart.svelte";
89
10+
let { data } = $props();
11+
const lineChartData = (() => {
12+
const found = data.dataInFormatForLineChart.find(
13+
(el) => el.metric === "Household waste recycling rate",
14+
);
15+
return found ? { ...found, lines: found.lines.slice(0, 5) } : null;
16+
})();
17+
918
let accordionSnippetSections = [
1019
{
1120
id: "1",
12-
heading: "1. Example 1 - describe the use case here",
21+
heading: "1. Static line chart",
1322
content: Example1,
1423
},
24+
{
25+
id: "2",
26+
heading: "2. Interactive line chart",
27+
content: Example2,
28+
},
29+
{
30+
id: "3",
31+
heading: "3. Chart with different tiers",
32+
content: Example3,
33+
},
34+
{
35+
id: "4",
36+
heading: "4. Chart with markers",
37+
content: Example4,
38+
},
1539
];
1640
</script>
1741

@@ -37,32 +61,34 @@
3761

3862
{#snippet Example1()}
3963
<div class="p-5 bg-white">
40-
<Template
41-
componentNameProp="Example 1"
42-
checkboxProp={true}
43-
dropdownProp="Dragonfruit"
44-
jsObjectProp={[
45-
{
46-
name: "Borussia Dortmund",
47-
country: "Germany",
48-
color: "#fdff7d",
49-
},
50-
{ name: "Liverpool FC", country: "UK", color: "#f59fad" },
51-
{
52-
name: "SSC Napoli",
53-
country: "Italy",
54-
color: "#69bfff",
55-
},
56-
{
57-
name: "S.L. Benfica",
58-
country: "Portugal",
59-
color: "#ff8c96",
60-
},
61-
]}
62-
functionProp={function () {
63-
window.alert(`Example 1 functionProp has been triggered.`);
64-
}}
65-
></Template>
64+
<LineChart {lineChartData} x="x" y="y" series="areaCode"></LineChart>
65+
</div>
66+
<CodeBlock code={codeBlocks.codeBlock1} language="svelte"></CodeBlock>
67+
{/snippet}
68+
69+
{#snippet Example2()}
70+
<div class="p-5 bg-white">
71+
<LineChart {lineChartData} x="x" y="y" series="areaCode"></LineChart>
72+
</div>
73+
<CodeBlock code={codeBlocks.codeBlock1} language="svelte"></CodeBlock>
74+
{/snippet}
75+
76+
{#snippet Example3()}
77+
<div class="p-5 bg-white">
78+
<LineChart {lineChartData} x="x" y="y" series="areaCode"></LineChart>
79+
</div>
80+
<CodeBlock code={codeBlocks.codeBlock1} language="svelte"></CodeBlock>
81+
{/snippet}
82+
83+
{#snippet Example4()}
84+
<div class="p-5 bg-white">
85+
<LineChart
86+
{lineChartData}
87+
x="x"
88+
y="y"
89+
series="areaCode"
90+
basicLineParams={{ markers: true, interactive: true }}
91+
></LineChart>
6692
</div>
6793
<CodeBlock code={codeBlocks.codeBlock1} language="svelte"></CodeBlock>
6894
{/snippet}

0 commit comments

Comments
 (0)