-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathindex.js
More file actions
329 lines (280 loc) · 9.36 KB
/
index.js
File metadata and controls
329 lines (280 loc) · 9.36 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import { line as d3Line, curveMonotoneX as d3CurveMonotoneX } from 'd3-shape'
import { select as d3Select } from 'd3-selection'
import { transition } from 'd3-transition'
import isEmpty from 'lodash-es/isEmpty'
import isArray from 'lodash-es/isArray'
import {
centerTranslation,
getRadius,
calculateNeedleHeight,
formatCurrentValueText,
sumArrayTill,
} from '../util'
import { getNeedleTransition } from '../util/get-needle-transition'
import {
configureArc,
configureTicks,
configureTickData,
configureScale,
} from '../config/configure'
// ref: https://stackoverflow.com/a/64596738/1410291
function constructTransition({ duration, ease }) {
return transition().duration(duration).ease(ease)
}
export const update = ({ d3_refs, newValue, config }) => {
const scale = configureScale(config)
const ratio = scale(newValue)
const range = config.maxAngle - config.minAngle
const newAngle = config.minAngle + ratio * range
// update the pointer
d3_refs.pointer
.transition(
constructTransition({
duration: config.needleTransitionDuration,
ease: getNeedleTransition(config.needleTransition),
})
)
.attr('transform', `rotate(${newAngle})`)
d3_refs.current_value_text.text(formatCurrentValueText(newValue, config))
}
export const render = ({ container, config }) => {
const r = getRadius(config)
const centerTx = centerTranslation(
r,
config.paddingHorizontal,
config.paddingVertical
)
const svg = _renderSVG({ container, config })
_renderArcs({ config, svg, centerTx })
_renderLabels({ config, svg, centerTx, r })
return {
current_value_text: _renderCurrentValueText({ config, svg }),
pointer: _renderNeedle({ config, svg, r, centerTx }),
}
}
// helper function to render individual parts of gauge
function _renderSVG({ container, config }) {
// calculate width and height
const width = config.width + 2 * config.paddingHorizontal
const height = config.height + 2 * config.paddingVertical
return (
d3Select(container)
.append('svg:svg')
.attr('class', 'speedometer')
.attr('width', `${width}${config.dimensionUnit}`)
.attr('height', `${height}${config.dimensionUnit}`)
.attr('role', 'img')
.attr('focusable', 'false')
.attr('aria-label', config.svgAriaLabel)
// use inline styles so that width/height is not overridden
.style('width', `${width}${config.dimensionUnit}`)
.style('height', `${height}${config.dimensionUnit}`)
)
}
function _renderArcs({ config, svg, centerTx }) {
const tickData = configureTickData(config)
const arc = configureArc(config)
let arcs = svg.append('g').attr('class', 'arc').attr('transform', centerTx)
arcs
.selectAll('path')
.data(tickData)
.enter()
.append('path')
.attr('class', 'speedo-segment')
.attr('fill', (d, i) => {
// if custom segment colors is present just use it
if (!isEmpty(config.segmentColors) && config.segmentColors[i]) {
return config.segmentColors[i]
}
return config.arcColorFn(d * i)
})
.attr('d', arc)
.attr('data-label', (d, i) => {
return config.customSegmentLabels[i]?.text || 'Default Label';
})
.attr('data-sstop', (d, i) => {
const startStop = config.customSegmentStops[i];
const endStop = config.customSegmentStops[i + 1];
// Check if startStop or endStop are undefined, null, or not numbers
if (typeof startStop !== 'number' || startStop === null || isNaN(startStop) ||
typeof endStop !== 'number' || endStop === null || isNaN(endStop)) {
// Provide a default value or skip setting the attribute
return 'unknown';
} else {
// Both startStop and endStop are valid numbers
return `${startStop}-${endStop}`;
}
})
.on('click', function (event, d) {
const label = event.target.getAttribute('data-label');
const value = event.target.getAttribute('data-sstop');
if (config.onSegmentClick) {
config.onSegmentClick(label,value);
}
});
}
export function _renderLabels({ config, svg, centerTx, r }) {
const ticks = configureTicks(config)
const tickData = configureTickData(config)
const scale = configureScale(config)
const range = config.maxAngle - config.minAngle
// assuming we have the custom segment labels here
const { customSegmentLabels } = config
const isCustomLabelsPresent =
isArray(customSegmentLabels) && !isEmpty(customSegmentLabels)
const isCustomLabelsValid =
isCustomLabelsPresent && customSegmentLabels.length === tickData.length
// if custom labels present and not valid
if (isCustomLabelsPresent && !isCustomLabelsValid) {
throw new Error(
`Custom Segment Labels should be an array with length of ${tickData.length}`
)
}
// we have valid custom labels
if (isCustomLabelsPresent && isCustomLabelsValid) {
_renderCustomSegmentLabels({
config,
svg,
centerTx,
r,
ticks,
tickData,
scale,
range,
})
// do not proceed
return
}
// normal label rendering
let lg = svg.append('g').attr('class', 'label').attr('transform', centerTx)
lg.selectAll('text')
.data(ticks)
.enter()
.append('text')
.attr('transform', (d, i) => {
const ratio =
config.customSegmentStops.length === 0
? scale(d)
: sumArrayTill(tickData, i)
const newAngle = config.minAngle + ratio * range
return `rotate(${newAngle}) translate(0, ${config.labelInset - r})`
})
// first labelFormat is applied via d3Format
// then we will apply custom 'segmentValueFormatter' function
// .text(config.labelFormat)
.text(value => {
return config.segmentValueFormatter(config.labelFormat(value))
})
// add class for text label
.attr('class', 'segment-value')
// styling stuffs
.style('text-anchor', 'middle')
.style('font-size', config.labelFontSize)
.style('font-weight', 'bold')
// .style("fill", "#666");
.style('fill', config.textColor)
}
// helper function to render 'custom segment labels'
function _renderCustomSegmentLabels({
config,
svg,
centerTx,
r,
ticks,
tickData,
scale,
range,
}) {
const { customSegmentStops, customSegmentLabels } = config
// helper function to calculate angle
function _calculateAngle(d, i) {
const ratio =
customSegmentStops.length === 0 ? scale(d) : sumArrayTill(tickData, i)
const newAngle = config.minAngle + ratio * range
return newAngle
}
// calculate the angles ([avg of range angles])
const newAngles = customSegmentLabels.map((label, i) => {
const curr_index = i
const next_index = i + 1
const d1 = ticks[curr_index]
const angle1 = _calculateAngle(d1, curr_index)
const d2 = ticks[next_index]
const angle2 = _calculateAngle(d2, next_index)
return (angle2 + angle1) / 2
})
const innerRadius = r - config.ringWidth - config.ringInset
const outerRadius = r - config.ringInset
const position = outerRadius - (outerRadius - innerRadius) / 2
let lg = svg.append('g').attr('class', 'label').attr('transform', centerTx)
lg.selectAll('text')
.data(customSegmentLabels)
.enter()
.append('text')
.attr('transform', (d, i) => {
const newAngle = newAngles[i]
const outerText = `rotate(${newAngle}) translate(0, ${
config.labelInset - r
})`
const innerText = `rotate(${newAngle}) translate(0, ${
config.labelInset / 2 - position
})`
// by default we will show "INSIDE"
return d.position === 'OUTSIDE' ? outerText : innerText
})
.text(d => d.text || '')
// add class for text label
.attr('class', 'segment-value')
// styling stuffs
.style('text-anchor', 'middle')
.style('font-size', d => d.fontSize || config.labelFontSize)
.style('font-weight', 'bold')
.style('fill', d => d.color || config.textColor)
// depending on INSIDE/OUTSIDE config calculate the position/rotate/translate
// utilise the color/fontSize configs
}
function _renderCurrentValueText({ config, svg }) {
const translateX = (config.width + 2 * config.paddingHorizontal) / 2
// move the current value text down depending on padding vertical
const translateY = (config.width + 4 * config.paddingVertical) / 2
return (
svg
.append('g')
.attr('transform', `translate(${translateX}, ${translateY})`)
.append('text')
// add class for the text
.attr('class', 'current-value')
.attr('text-anchor', 'middle')
// position the text 23pt below
.attr('y', 23)
// add text
.text(config.currentValue)
.style('font-size', config.valueTextFontSize)
.style('font-weight', config.valueTextFontWeight)
.style('fill', config.textColor)
)
}
function _renderNeedle({ config, svg, r, centerTx }) {
const needleLength = calculateNeedleHeight({
heightRatio: config.needleHeightRatio,
radius: r,
})
const lineData = [
[config.pointerWidth / 2, 0],
[0, -needleLength],
[-(config.pointerWidth / 2), 0],
[0, config.pointerTailLength],
[config.pointerWidth / 2, 0],
]
const pointerLine = d3Line().curve(d3CurveMonotoneX)
let pg = svg
.append('g')
.data([lineData])
.attr('class', 'pointer')
.attr('transform', centerTx)
.style('fill', config.needleColor)
return pg
.append('path')
.attr('d', pointerLine)
.attr('transform', `rotate(${config.minAngle})`)
}