"...git@git.app.uib.no:Rein.Landmark/lecture-code-1-30-24.git" did not exist on "56ffddc649919d6610f899c44eedbef1e4a50983"
Newer
Older
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* eslint-disable no-unused-vars */
import React, { useState } from 'react'
import Pie, { ProvidedProps, PieArcDatum } from '@visx/shape/lib/shapes/Pie'
import { scaleOrdinal } from '@visx/scale'
import { Group } from '@visx/group'
import { animated, useTransition, interpolate } from 'react-spring'
type Slice = {
id: string
value: number
}
/* const test: Slice[] = [
{
id: 'Universitetet i Bergen',
value: 67,
},
{
id: 'Havforskningsinstituttet',
value: 59,
},
{
id: 'Alfred Wegner Institute - Helmholz Centre for Polar Marine Research / Helmholz Association',
value: 57,
},
{
id: 'Norges Arktiske Universitet, UiT',
value: 51,
},
{
id: 'Bjerknessenteret for klimaforskning',
value: 48,
},
{
id: 'Norsk Polarinstitutt',
value: 40,
},
{
id: 'Russian Academy of Sciences',
value: 37,
},
{
id: 'Universitetet I Oslo',
value: 36,
},
{
id: 'UNIS - The University Centre in Svalbard',
value: 26,
},
{
id: 'University of Alaska',
value: 26,
},
{
id: 'Woods Hole Oceanographic Institution',
value: 26,
},
{
id: 'Centre National de la Recherche Scientifique, CNRS',
value: 21,
},
] */
const defaultMargin = { top: 20, right: 20, bottom: 20, left: 20 }
export type PieProps = {
width: number
height: number
margin?: typeof defaultMargin
animate?: boolean
data: Slice[]
}
export default function Donut({
width,
height,
margin = defaultMargin,
animate = true,
data,
}: PieProps) {
const [selectedSlice, setSelectedSlice] = useState<string | null>(null)
const test: Slice[] = data
// accessor functions
const n = (d: Slice) => d.value
// color scales
const getSliceColor = scaleOrdinal({
domain: [...test.flatMap((s) => s.id)],
range: [
'rgba(255,255,255,0.7)',
'rgba(255,255,255,0.6)',
'rgba(255,250,255,0.5)',
'rgba(255,255,255,0.4)',
'rgba(255,255,255,0.3)',
'rgba(255,255,255,0.2)',
'rgba(255,255,255,0.1)',
],
})
if (width < 10) return null
const innerWidth = width - margin.left - margin.right
const innerHeight = height - margin.top - margin.bottom
const radius = Math.min(innerWidth, innerHeight) / 2
const centerY = innerHeight / 2
const centerX = innerWidth / 2
const donutThickness = 200
/* console.log(JSON.stringify(test, null, 2), getSliceColor('Universitetet i Bergen')) */
return (
<svg width={width} height={height}>
<rect rx={14} width={width} height={height} />
<Group top={centerY + margin.top} left={centerX + margin.left}>
<Pie
data={selectedSlice ? test.filter(({ id }) => id === selectedSlice) : test}
pieValue={n}
outerRadius={radius}
innerRadius={radius - donutThickness}
cornerRadius={3}
padAngle={0.005}
>
{(pie) => (
<AnimatedPie<Slice>
{...pie}
animate={animate}
getKey={(arc) => arc.data.id.slice(0, 40) + '...'}
onClickDatum={({ data: { id } }) =>
animate && setSelectedSlice(selectedSlice && selectedSlice === id ? null : id)
}
getColor={(arc) => getSliceColor(arc.data.id)}
/>
)}
</Pie>
</Group>
{animate && (
<text
textAnchor="end"
x={width - 16}
y={height - 16}
fill="white"
fontSize={11}
fontWeight={300}
pointerEvents="none"
>
Click segments to update
</text>
)}
</svg>
)
}
// react-spring transition definitions
type AnimatedStyles = { startAngle: number; endAngle: number; opacity: number }
const fromLeaveTransition = ({ endAngle }: PieArcDatum<any>) => ({
// enter from 360° if end angle is > 180°
startAngle: endAngle > Math.PI ? 2 * Math.PI : 0,
endAngle: endAngle > Math.PI ? 2 * Math.PI : 0,
opacity: 0,
})
const enterUpdateTransition = ({ startAngle, endAngle }: PieArcDatum<any>) => ({
startAngle,
endAngle,
opacity: 1,
})
type AnimatedPieProps<Datum> = ProvidedProps<Datum> & {
animate?: boolean
getKey: (d: PieArcDatum<Datum>) => string
getColor: (d: PieArcDatum<Datum>) => string
onClickDatum: (d: PieArcDatum<Datum>) => void
delay?: number
}
function AnimatedPie<Datum>({
animate,
arcs,
path,
getKey,
getColor,
onClickDatum,
}: AnimatedPieProps<Datum>) {
const transitions = useTransition<PieArcDatum<Datum>, AnimatedStyles>(arcs, {
from: animate ? fromLeaveTransition : enterUpdateTransition,
enter: enterUpdateTransition,
update: enterUpdateTransition,
leave: animate ? fromLeaveTransition : enterUpdateTransition,
keys: getKey,
})
return transitions((props, arc, { key }) => {
const [centroidX, centroidY] = path.centroid(arc)
const hasSpaceForLabel = arc.endAngle - arc.startAngle >= 0.1
return (
<g key={key}>
<animated.path
// compute interpolated path d attribute from intermediate angle values
d={interpolate([props.startAngle, props.endAngle], (startAngle, endAngle) =>
path({
...arc,
startAngle,
endAngle,
}),
)}
fill={getColor(arc)}
onClick={() => onClickDatum(arc)}
onTouchStart={() => onClickDatum(arc)}
/>
{hasSpaceForLabel && (
<animated.g style={{ opacity: props.opacity }}>
<text
fill="white"
x={centroidX}
y={centroidY}
dy=".33em"
fontSize={9}
textAnchor="middle"
pointerEvents="none"
>
{getKey(arc)}
</text>
</animated.g>
)}
</g>
)
})
}