Skip to content

Commit eeed6cd

Browse files
committed
WIP render labels outside tier system, using HTML-formatted text
1 parent c0e1950 commit eeed6cd

File tree

3 files changed

+105
-77
lines changed

3 files changed

+105
-77
lines changed

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

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script>
22
import Line from "./Line.svelte";
33
import Marker from "./Marker.svelte";
4+
import ValueLabel from "./ValueLabel.svelte";
45
import SeriesLabel from "./SeriesLabel.svelte";
56
import labelplacer from "labelplacer";
67
import { onMount } from "svelte";
@@ -14,8 +15,8 @@
1415
chartWidth,
1516
xFunction,
1617
yFunction,
17-
labelClicked = $bindable(),
18-
labelHovered = $bindable(),
18+
labelClicked,
19+
labelHovered,
1920
lineHovered,
2021
lineClicked,
2122
chartHeight,
@@ -90,43 +91,52 @@
9091
{activeMarkerId}
9192
/>
9293
{/each}
93-
{#if tier == "hover"}
94-
{#if true}
95-
<!-- <ValueLabel
96-
{marker}
97-
labelColor="grey"
98-
labelTextColor="black"
99-
textContent={parseInput(marker, marker[y])}
100-
></ValueLabel> -->
101-
{/if}
102-
{/if}
10394
</g>
10495

10596
<g>
10697
{#each tieredDataObject[tier] as line, i}
10798
{#if line.showLabel}
108-
{@const newY = labelsPlaced.find(
109-
(el) => el.datum[series] === line[series],
110-
)?.[y]}
111-
<SeriesLabel
112-
id={`label-${line[series]}`}
113-
bind:labelClicked
114-
bind:labelHovered
115-
{chartWidth}
116-
{lineFunction}
117-
dataArray={line}
118-
{xFunction}
119-
{yFunction}
120-
{newY}
121-
{onClickLabel}
122-
{onMouseEnterLabel}
123-
{onMouseLeaveLabel}
124-
{labelText}
125-
{series}
126-
{y}
127-
></SeriesLabel>
99+
{console.log("@", labelClicked == line[series])}
100+
{#if !labelClicked || (labelClicked && line[series] == labelClicked)}
101+
{@const newY = labelsPlaced.find(
102+
(el) => el.datum[series] === line[series],
103+
)?.[y]}
104+
<SeriesLabel
105+
id={`label-${line[series]}`}
106+
{labelClicked}
107+
{labelHovered}
108+
{chartWidth}
109+
{lineFunction}
110+
dataArray={line}
111+
{xFunction}
112+
{yFunction}
113+
{newY}
114+
{onClickLabel}
115+
{onMouseEnterLabel}
116+
{onMouseLeaveLabel}
117+
{labelText}
118+
{series}
119+
{y}
120+
></SeriesLabel>
121+
{/if}
128122
{/if}
129123
{/each}
130124
</g>
131125
</g>
132126
{/each}
127+
128+
{#if activeMarkerId}
129+
<ValueLabel
130+
{activeMarkerId}
131+
labelColor="grey"
132+
labelTextColor="black"
133+
textContent={`this string contains some <strong>HTML!!!</strong>`}
134+
{xFunction}
135+
{yFunction}
136+
{x}
137+
{y}
138+
></ValueLabel>
139+
{/if}
140+
141+
<!-- if markerhovered get the id render the tooltip find in the tieredDataObject gets
142+
the data for that line -->

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

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,11 @@
1515
markerFill = "grey",
1616
markerStroke = "white",
1717
markerStrokeWidth = 1,
18-
activeMarkerId,
1918
} = $props();
20-
21-
function makeList(inputValue) {
22-
let items = inputValue.split("\\n").map((item) => item.trim());
23-
return items;
24-
}
25-
26-
function parseInput(marker, inputValue) {
27-
let items = makeList(inputValue);
28-
let mappedItems = items.map((item) =>
29-
item.replace(/\{(\w+)\}/g, (_, key) => marker[key] ?? `{${key}}`),
30-
);
31-
return mappedItems;
32-
}
3319
</script>
3420

3521
{#each dataArray as marker, i}
3622
{@const markerId = "marker-" + marker[series] + marker[x]}
37-
{console.log(markerId)}
3823
<g
3924
data-id={markerId}
4025
onclick={(event) => onClickMarker(event, marker, markerId)}
@@ -72,15 +57,5 @@
7257
stroke-width={markerStrokeWidth}
7358
></polygon>
7459
{/if}
75-
<!-- {#if true}
76-
{#if activeMarkerId === markerId}
77-
<ValueLabel
78-
{marker}
79-
labelColor="grey"
80-
labelTextColor="black"
81-
textContent={parseInput(marker, marker[y])}
82-
></ValueLabel>
83-
{/if}
84-
{/if} -->
8560
</g>
8661
{/each}
Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,73 @@
11
<script>
2-
let { marker, labelColor, labelTextColor, textContent } = $props();
2+
let {
3+
activeMarkerId,
4+
labelColor,
5+
labelTextColor,
6+
textContent,
7+
xFunction,
8+
yFunction,
9+
x,
10+
y,
11+
} = $props();
312
413
let textDimensions = $state();
514
let lineSpacing = $state(20);
615
let verticalPadding = $state(8);
716
let horizontalPadding = $derived(verticalPadding * 2);
17+
18+
$inspect(activeMarkerId);
19+
20+
let widthWithPadding = $derived(
21+
textDimensions ? textDimensions.width + horizontalPadding : 10,
22+
);
23+
let heightWithPadding = $derived(
24+
textDimensions ? textDimensions.height + verticalPadding : 10,
25+
);
826
</script>
927

10-
<svg width="300" height="300">
11-
{#if textDimensions}
12-
<rect
13-
height={textDimensions.height + verticalPadding}
14-
rx="5"
15-
ry="5"
16-
fill={labelColor}
17-
width={textDimensions.width + horizontalPadding}
18-
></rect>
19-
{/if}
20-
<text
21-
text-anchor="start"
22-
font-size="16"
23-
fill={labelTextColor}
24-
bind:contentRect={textDimensions}
28+
<svg>
29+
<g
30+
transform="translate({xFunction(activeMarkerId[x])},{yFunction(
31+
activeMarkerId[y],
32+
)})"
2533
>
26-
{#each textContent as line, i}
27-
<tspan x={horizontalPadding / 2} dy={lineSpacing}>{line}</tspan>
28-
{/each}
29-
</text>
34+
{#if !textDimensions}
35+
<foreignObject
36+
x="0"
37+
y="0"
38+
width="1000"
39+
height="1000"
40+
style="visibility: hidden"
41+
>
42+
<div
43+
xmlns="http://www.w3.org/1999/xhtml"
44+
bind:contentRect={textDimensions}
45+
style="display: inline-block"
46+
>
47+
{@html textContent}
48+
</div>
49+
</foreignObject>
50+
{/if}
51+
{#if textDimensions}
52+
<rect
53+
height={textDimensions.height + verticalPadding}
54+
rx="5"
55+
ry="5"
56+
fill={labelColor}
57+
width={textDimensions.width + horizontalPadding}
58+
></rect>
59+
{/if}
60+
</g>
61+
{#key textDimensions}
62+
<foreignObject
63+
x={xFunction(activeMarkerId[x])}
64+
y={yFunction(activeMarkerId[y])}
65+
width={widthWithPadding}
66+
height={heightWithPadding}
67+
>
68+
<div xmlns="http://www.w3.org/1999/xhtml" style="display: inline-block">
69+
{@html textContent}
70+
</div>
71+
</foreignObject>
72+
{/key}
3073
</svg>

0 commit comments

Comments
 (0)