Skip to content

Commit 6637239

Browse files
committed
Add legend to pie charts
1 parent 60265d4 commit 6637239

File tree

2 files changed

+84
-29
lines changed

2 files changed

+84
-29
lines changed

desktop-app/src/components/MetricsPieChart.svelte

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,47 @@
1414

1515
{#if data.length > 0}
1616
<article class="metrics-pie-chart">
17-
<PieChart
18-
{data}
19-
key="key"
20-
value="value"
21-
cRange={colors}
22-
{innerRadius}
23-
{outerRadius}
24-
{cornerRadius}
25-
{padAngle}
26-
props={{
27-
tooltip: {
28-
root: {
29-
classes: { root: 'layerchart-tooltip' }
17+
<div class="metrics-pie-chart__chart">
18+
<PieChart
19+
{data}
20+
key="key"
21+
value="value"
22+
cRange={colors}
23+
{innerRadius}
24+
{outerRadius}
25+
{cornerRadius}
26+
{padAngle}
27+
props={{
28+
tooltip: {
29+
root: {
30+
classes: { root: 'layerchart-tooltip' }
31+
}
3032
}
31-
}
32-
}}
33-
>
34-
<svelte:fragment slot="aboveMarks" let:width let:height>
35-
<text
36-
x={0}
37-
y={0}
38-
text-anchor="middle"
39-
dominant-baseline="middle"
40-
class="metrics-pie-chart__label"
41-
>
42-
{label}
43-
</text>
44-
</svelte:fragment>
45-
</PieChart>
33+
}}
34+
>
35+
<svelte:fragment slot="aboveMarks" let:width let:height>
36+
<text
37+
x={0}
38+
y={0}
39+
text-anchor="middle"
40+
dominant-baseline="middle"
41+
class="metrics-pie-chart__label"
42+
>
43+
{label}
44+
</text>
45+
</svelte:fragment>
46+
</PieChart>
47+
</div>
48+
<ul class="metrics-pie-chart__legend" aria-label={`${label} legend`}>
49+
{#each data as point, index}
50+
<li class="metrics-pie-chart__legend-item">
51+
<span
52+
class="metrics-pie-chart__legend-swatch"
53+
style={`--legend-color: ${colors[index % colors.length]}`}
54+
></span>
55+
<span class="metrics-pie-chart__legend-label">{point.key}</span>
56+
</li>
57+
{/each}
58+
</ul>
4659
</article>
4760
{/if}
Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,54 @@
11
.metrics-pie-chart {
22
--container-size: 160px;
33
width: var(--container-size);
4-
height: var(--container-size);
4+
display: flex;
5+
flex-direction: column;
6+
align-items: center;
7+
gap: 10px;
58
position: relative;
69
}
710

11+
.metrics-pie-chart__chart {
12+
width: var(--container-size);
13+
height: var(--container-size);
14+
}
15+
816
.metrics-pie-chart__label {
917
fill: #f4f4f4;
1018
font-weight: 600;
1119
font-size: 14px;
1220
}
21+
22+
.metrics-pie-chart__legend {
23+
list-style: none;
24+
padding: 0;
25+
margin: 0;
26+
width: 100%;
27+
display: grid;
28+
grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
29+
gap: 6px 10px;
30+
padding: 0 15px;
31+
font-size: 12px;
32+
color: #f4f4f4;
33+
}
34+
35+
.metrics-pie-chart__legend-item {
36+
display: inline-flex;
37+
align-items: center;
38+
gap: 8px;
39+
min-width: 0;
40+
}
41+
42+
.metrics-pie-chart__legend-swatch {
43+
width: 10px;
44+
height: 10px;
45+
border-radius: 999px;
46+
background: var(--legend-color);
47+
flex: 0 0 auto;
48+
}
49+
50+
.metrics-pie-chart__legend-label {
51+
overflow: hidden;
52+
text-overflow: ellipsis;
53+
white-space: nowrap;
54+
}

0 commit comments

Comments
 (0)