@@ -2,7 +2,7 @@ import {ascending} from "d3-array";
2
2
import { create } from "d3-selection" ;
3
3
import { filter , nonempty } from "../defined.js" ;
4
4
import { Mark , indexOf , identity , string , title , maybeColor , first , second } from "../mark.js" ;
5
- import { Style , applyDirectStyles , applyIndirectStyles , applyAttr , applyTransform } from "../style.js" ;
5
+ import { Style , applyDirectStyles , applyIndirectStyles , applyAttr , applyStyle , applyTransform } from "../style.js" ;
6
6
7
7
export class Text extends Mark {
8
8
constructor (
@@ -16,6 +16,11 @@ export class Text extends Mark {
16
16
fill,
17
17
transform,
18
18
textAnchor,
19
+ fontFamily,
20
+ fontSize,
21
+ fontStyle,
22
+ fontVariant,
23
+ fontWeight,
19
24
dx,
20
25
dy = "0.32em" ,
21
26
...style
@@ -36,6 +41,11 @@ export class Text extends Mark {
36
41
) ;
37
42
Style ( this , { fill : cfill , ...style } ) ;
38
43
this . textAnchor = string ( textAnchor ) ;
44
+ this . fontFamily = string ( fontFamily ) ;
45
+ this . fontSize = string ( fontSize ) ;
46
+ this . fontStyle = string ( fontStyle ) ;
47
+ this . fontVariant = string ( fontVariant ) ;
48
+ this . fontWeight = string ( fontWeight ) ;
39
49
this . dx = string ( dx ) ;
40
50
this . dy = string ( dy ) ;
41
51
}
@@ -48,13 +58,12 @@ export class Text extends Mark {
48
58
const index = filter ( I , X , Y , F ) . filter ( i => nonempty ( T [ i ] ) ) ;
49
59
if ( Z ) index . sort ( ( i , j ) => ascending ( Z [ i ] , Z [ j ] ) ) ;
50
60
return create ( "svg:g" )
51
- . call ( applyIndirectStyles , this )
61
+ . call ( applyIndirectTextStyles , this )
52
62
. call ( applyTransform , x , y , 0.5 , 0.5 )
53
63
. call ( g => g . selectAll ( )
54
64
. data ( index )
55
65
. join ( "text" )
56
- . call ( applyDirectStyles , this )
57
- . call ( applyTextStyles , this )
66
+ . call ( applyDirectTextStyles , this )
58
67
. attr ( "x" , X ? i => x ( X [ i ] ) : ( marginLeft + width - marginRight ) / 2 )
59
68
. attr ( "y" , Y ? i => y ( Y [ i ] ) : ( marginTop + height - marginBottom ) / 2 )
60
69
. attr ( "fill" , F && ( i => color ( F [ i ] ) ) )
@@ -76,8 +85,18 @@ export function textY(data, {y = identity, ...options} = {}) {
76
85
return new Text ( data , { ...options , y, x : null } ) ;
77
86
}
78
87
79
- function applyTextStyles ( selection , style ) {
80
- applyAttr ( selection , "text-anchor" , style . textAnchor ) ;
81
- applyAttr ( selection , "dx" , style . dx ) ;
82
- applyAttr ( selection , "dy" , style . dy ) ;
88
+ function applyIndirectTextStyles ( selection , mark ) {
89
+ applyIndirectStyles ( selection , mark ) ;
90
+ applyAttr ( selection , "text-anchor" , mark . textAnchor ) ;
91
+ applyStyle ( selection , "font-family" , mark . fontFamily ) ;
92
+ applyStyle ( selection , "font-size" , mark . fontSize ) ;
93
+ applyStyle ( selection , "font-style" , mark . fontStyle ) ;
94
+ applyStyle ( selection , "font-variant" , mark . fontVariant ) ;
95
+ applyStyle ( selection , "font-weight" , mark . fontWeight ) ;
96
+ }
97
+
98
+ function applyDirectTextStyles ( selection , mark ) {
99
+ applyDirectStyles ( selection , mark ) ;
100
+ applyAttr ( selection , "dx" , mark . dx ) ;
101
+ applyAttr ( selection , "dy" , mark . dy ) ;
83
102
}
0 commit comments