1
1
import { degreesToRadians } from '@nivo/core'
2
- import { Theme } from '@nivo/theming'
2
+ import { Theme , PartialTheme , extendAxisTheme } from '@nivo/theming'
3
3
import { setCanvasFont , drawCanvasText } from '@nivo/text'
4
4
import { ScaleValue , AnyScale , TicksSpec } from '@nivo/scales'
5
5
import { computeCartesianTicks , getFormatter , computeGridLines } from './compute'
@@ -24,6 +24,7 @@ export const renderAxisToCanvas = <Value extends ScaleValue>(
24
24
legendPosition = 'end' ,
25
25
legendOffset = 0 ,
26
26
theme,
27
+ style,
27
28
} : {
28
29
axis : 'x' | 'y'
29
30
scale : AnyScale
@@ -40,6 +41,7 @@ export const renderAxisToCanvas = <Value extends ScaleValue>(
40
41
legendPosition ?: AxisLegendPosition
41
42
legendOffset ?: number
42
43
theme : Theme
44
+ style ?: PartialTheme [ 'axis' ]
43
45
}
44
46
) => {
45
47
const { ticks, textAlign, textBaseline } = computeCartesianTicks ( {
@@ -56,18 +58,20 @@ export const renderAxisToCanvas = <Value extends ScaleValue>(
56
58
ctx . save ( )
57
59
ctx . translate ( x , y )
58
60
61
+ const axisTheme = extendAxisTheme ( theme . axis , style )
62
+
59
63
ctx . textAlign = textAlign
60
64
ctx . textBaseline = textBaseline
61
65
62
- setCanvasFont ( ctx , theme . axis . ticks . text )
66
+ setCanvasFont ( ctx , axisTheme . ticks . text )
63
67
64
- const domainLineWidth = theme . axis . domain . line . strokeWidth ?? 0
68
+ const domainLineWidth = axisTheme . domain . line . strokeWidth ?? 0
65
69
if ( typeof domainLineWidth !== 'string' && domainLineWidth > 0 ) {
66
70
ctx . lineWidth = domainLineWidth
67
71
ctx . lineCap = 'square'
68
72
69
- if ( theme . axis . domain . line . stroke ) {
70
- ctx . strokeStyle = theme . axis . domain . line . stroke
73
+ if ( axisTheme . domain . line . stroke ) {
74
+ ctx . strokeStyle = axisTheme . domain . line . stroke
71
75
}
72
76
73
77
ctx . beginPath ( )
@@ -78,15 +82,15 @@ export const renderAxisToCanvas = <Value extends ScaleValue>(
78
82
79
83
const format = typeof _format === 'function' ? _format : ( value : unknown ) => `${ value } `
80
84
81
- const tickLineWidth = theme . axis . ticks . line . strokeWidth ?? 0
85
+ const tickLineWidth = axisTheme . ticks . line . strokeWidth ?? 0
82
86
const shouldRenderTickLine = typeof tickLineWidth !== 'string' && tickLineWidth > 0
83
87
ticks . forEach ( tick => {
84
88
if ( shouldRenderTickLine ) {
85
89
ctx . lineWidth = tickLineWidth
86
90
ctx . lineCap = 'square'
87
91
88
- if ( theme . axis . ticks . line . stroke ) {
89
- ctx . strokeStyle = theme . axis . ticks . line . stroke
92
+ if ( axisTheme . ticks . line . stroke ) {
93
+ ctx . strokeStyle = axisTheme . ticks . line . stroke
90
94
}
91
95
92
96
ctx . beginPath ( )
@@ -101,7 +105,7 @@ export const renderAxisToCanvas = <Value extends ScaleValue>(
101
105
ctx . translate ( tick . x + tick . textX , tick . y + tick . textY )
102
106
ctx . rotate ( degreesToRadians ( tickRotation ) )
103
107
104
- drawCanvasText ( ctx , theme . axis . ticks . text , `${ value } ` )
108
+ drawCanvasText ( ctx , axisTheme . ticks . text , `${ value } ` )
105
109
106
110
ctx . fillText ( `${ value } ` , 0 , 0 )
107
111
ctx . restore ( )
@@ -140,17 +144,15 @@ export const renderAxisToCanvas = <Value extends ScaleValue>(
140
144
141
145
ctx . translate ( legendX , legendY )
142
146
ctx . rotate ( degreesToRadians ( legendRotation ) )
143
- ctx . font = `${
144
- theme . axis . legend . text . fontWeight ? `${ theme . axis . legend . text . fontWeight } ` : ''
145
- } ${ theme . axis . legend . text . fontSize } px ${ theme . axis . legend . text . fontFamily } `
147
+ setCanvasFont ( ctx , axisTheme . legend . text )
146
148
147
- if ( theme . axis . legend . text . fill ) {
148
- ctx . fillStyle = theme . axis . legend . text . fill
149
+ if ( axisTheme . legend . text . fill ) {
150
+ ctx . fillStyle = axisTheme . legend . text . fill
149
151
}
150
152
151
153
ctx . textAlign = textAlign
152
154
ctx . textBaseline = 'middle'
153
- ctx . fillText ( legend , 0 , 0 )
155
+ drawCanvasText ( ctx , axisTheme . legend . text , legend )
154
156
}
155
157
156
158
ctx . restore ( )
@@ -163,12 +165,10 @@ export const renderAxesToCanvas = <X extends ScaleValue, Y extends ScaleValue>(
163
165
yScale,
164
166
width,
165
167
height,
166
-
167
168
top,
168
169
right,
169
170
bottom,
170
171
left,
171
-
172
172
theme,
173
173
} : {
174
174
xScale : AnyScale
0 commit comments