/
LegendPosition.tsx
179 lines (174 loc) · 6.14 KB
/
LegendPosition.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import React from 'react'
import omit from 'lodash/omit'
import { useTheme } from 'styled-components'
import { BoxLegendSvg } from '@nivo/legends'
const anchors = [
'top-left',
'top',
'top-right',
'right',
'bottom-right',
'bottom',
'bottom-left',
'left',
'center',
] as const
const translateExamples = [
{
anchor: 'top',
translateX: -160,
translateY: -30,
},
{
anchor: 'left',
translateX: -30,
translateY: 50,
},
{
anchor: 'right',
translateX: -30,
translateY: -60,
},
{
anchor: 'bottom',
translateX: 160,
translateY: 30,
},
]
const margin = 30
const legendProps = {
containerWidth: 800 - margin * 2,
containerHeight: 300 - margin * 2,
itemWidth: 120,
itemHeight: 16,
itemsSpacing: 4,
direction: 'column',
}
export const LegendPosition = () => {
const theme = useTheme()
return (
<div>
<h2>Legend position</h2>
<p>
The legend can be positioned in your chart area using the <code>anchor</code>{' '}
property.
<br />
You have 9 available directives:
</p>
<svg
width={legendProps.containerWidth + margin * 2}
height={legendProps.containerHeight + margin * 2}
style={{
background: theme.colors.cardBackground,
}}
>
<text
x={legendProps.containerWidth / 2 + margin}
y={margin / 2}
textAnchor="middle"
style={{
alignmentBaseline: 'middle',
fill: theme.colors.textLight,
fontSize: '14px',
}}
>
margin
</text>
<g transform={`translate(${margin},${margin})`}>
<rect
fill={theme.colors.background}
width={legendProps.containerWidth}
height={legendProps.containerHeight}
/>
{anchors.map(anchor => (
<BoxLegendSvg
{...legendProps}
itemTextColor={theme.colors.text}
key={anchor}
anchor={anchor}
data={[
{ id: 'a', label: anchor, color: theme.colors.accent },
{ id: 'b', label: '...', color: theme.colors.accent },
{ id: 'c', label: '...', color: theme.colors.accent },
]}
/>
))}
</g>
</svg>
<p>
The legend's anchor is relative to the inner chart area (with margin applied), but
depending on the chart type, you'll probably want to move it outside of this area.
<br />
That's where <code>translateX</code> & <code>translateY</code> come into play,
allowing to adjust the legend position from its original anchor.
</p>
<svg
width={legendProps.containerWidth + margin * 2}
height={legendProps.containerHeight + margin * 2}
style={{
background: theme.colors.cardBackground,
}}
>
<text
x={legendProps.containerWidth / 2 + margin}
y={margin / 2}
textAnchor="middle"
style={{
alignmentBaseline: 'middle',
fill: theme.colors.textLight,
fontSize: '14px',
}}
>
margin
</text>
<g transform={`translate(${margin},${margin})`}>
<rect
fill={theme.colors.background}
width={legendProps.containerWidth}
height={legendProps.containerHeight}
/>
{translateExamples.map((example, i) => (
<BoxLegendSvg
key={i}
{...legendProps}
{...omit(example, ['translateX', 'translateY'])}
itemTextColor={theme.colors.text}
data={[
{
id: example.anchor,
label: `${example.anchor} (regular)`,
color: theme.colors.text,
},
]}
/>
))}
{translateExamples.map((example, i) => (
<BoxLegendSvg
key={i}
{...legendProps}
{...example}
itemTextColor={theme.colors.text}
data={[
{
id: 'a',
label: `${example.anchor} (translated)`,
color: theme.colors.accent,
},
{
id: 'b',
label: `translateX: ${example.translateX}`,
color: theme.colors.accent,
},
{
id: 'c',
label: `translateY: ${example.translateY}`,
color: theme.colors.accent,
},
]}
/>
))}
</g>
</svg>
</div>
)
}