-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathrenderAxes.js
More file actions
86 lines (80 loc) · 2.09 KB
/
renderAxes.js
File metadata and controls
86 lines (80 loc) · 2.09 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
import { axisBottom, axisLeft } from 'd3';
export const renderAxes = (
svg,
{
xScale,
yScale,
dimensions: { width, height },
margin: { left, right, top, bottom },
showAxis = true,
axisColor = '#e0e0e0',
xAxisLabel = '',
yAxisLabel = '',
fontSize = '14px',
fontFamily = 'sans-serif',
},
) => {
if (showAxis) {
// Create X-axis
const xAxis = axisBottom(xScale)
.tickFormat((d) => d)
.tickSizeOuter(0);
// Create Y-axis
const yAxis = axisLeft(yScale)
.tickFormat((d) => d)
.tickSizeOuter(0);
// Render X-axis
svg
.selectAll('.x-axis')
.data([null])
.join('g')
.attr('class', 'x-axis')
.attr('transform', `translate(0, ${height - bottom})`)
.attr('fill', axisColor)
.attr('color', axisColor)
.call(xAxis);
// Render X-axis label
svg
.selectAll('.x-axis-label')
.data([null])
.join('text')
.attr('class', 'x-axis-label')
.attr('x', width / 2)
.attr('y', height - bottom / 3)
.attr('text-anchor', 'middle')
.attr('font-size', fontSize)
.attr('font-family', fontFamily)
.attr('fill', axisColor)
.text(xAxisLabel);
// Render Y-axis
svg
.selectAll('.y-axis')
.data([null])
.join('g')
.attr('class', 'y-axis')
.attr('transform', `translate(${left}, 0)`)
.attr('fill', axisColor)
.attr('color', axisColor)
.call(yAxis);
// Render Y-axis label
svg
.selectAll('.y-axis-label')
.data([null])
.join('text')
.attr('class', 'y-axis-label')
.attr('x', -height / 2)
.attr('y', left / 3)
.attr('text-anchor', 'middle')
.attr('transform', 'rotate(-90)')
.attr('font-size', fontSize)
.attr('font-family', fontFamily)
.attr('fill', axisColor)
.text(yAxisLabel);
} else {
// Remove axes and labels when showAxis is false
svg.selectAll('.x-axis').remove();
svg.selectAll('.y-axis').remove();
svg.selectAll('.x-axis-label').remove();
svg.selectAll('.y-axis-label').remove();
}
};