@@ -5,10 +5,9 @@ export default function() {
5
5
var keyFn = ( _ , ndx ) => ndx ,
6
6
textFn = ( d ) => d ,
7
7
container ,
8
- dx = 0 , dy = 0 ,
9
- x = ( d ) => d . pos . x + ( d . pos . width / 2 ) ,
10
- y = ( d ) => d . pos . y + ( d . pos . height / 2 ) ,
11
- textAnchor = 'start' ,
8
+ x = ( d ) => d . box . x + ( d . box . width / 2 ) ,
9
+ y = ( d ) => d . box . y + ( d . box . height / 2 ) ,
10
+ textAnchor = 'middle' ,
12
11
show = true ,
13
12
dragControl = drag ( )
14
13
. on ( "start" , function ( ) { this . classList . add ( 'dragging' ) ; } )
@@ -22,7 +21,6 @@ export default function() {
22
21
//
23
22
// serialize keys, bind click to add text, add text if `show` is T or fn
24
23
function annotate ( selection ) {
25
- container . classed ( 'd3-an-container' , true ) ;
26
24
selection . nodes ( ) . forEach ( ( el , ndx ) => el . __key__ = keyFn ( el . __data__ , ndx ) ) ;
27
25
selection . on ( 'click' , function ( ) { appendText ( select ( this ) ) ; } ) ;
28
26
if ( show ) { appendText ( selection , true ) ; }
@@ -32,14 +30,9 @@ export default function() {
32
30
// add new data bound <text> annotation
33
31
function appendText ( sel , filter ) {
34
32
var _sel = ( show instanceof Function && filter ) ? sel . filter ( show ) : sel ,
35
- _keyFn = ( d ) => keyFn ( d . data ) ,
36
33
_textFn = ( d ) => textFn ( d . data ) ,
37
- annotationData = _sel . nodes ( ) . map ( ( node , ndx ) => {
38
- return {
39
- data : node . __data__ ,
40
- key : node . __key__ ,
41
- pos : node . getBBox ( )
42
- } ;
34
+ annotationData = _sel . nodes ( ) . map ( ( node ) => {
35
+ return { data : node . __data__ , key : node . __key__ , box : node . getBBox ( ) } ;
43
36
} ) ;
44
37
45
38
var textSelection = container . selectAll ( 'text.with-data' )
@@ -48,7 +41,6 @@ export default function() {
48
41
. text ( _textFn )
49
42
. attr ( 'class' , 'annotation with-data' )
50
43
. attr ( 'x' , x ) . attr ( 'y' , y )
51
- . attr ( 'dx' , dx ) . attr ( 'dy' , dy )
52
44
. attr ( 'text-anchor' , textAnchor )
53
45
. call ( dragControl )
54
46
. on ( 'click' , function ( ) {
@@ -57,26 +49,6 @@ export default function() {
57
49
} ) ;
58
50
}
59
51
60
- //
61
- // properties
62
- annotate . container = function ( ) {
63
- if ( arguments . length ) {
64
- container = arguments [ 0 ] ;
65
- return annotate ;
66
- } else { return container ; }
67
- } ;
68
- annotate . text = function ( ) {
69
- if ( arguments . length ) {
70
- textFn = arguments [ 0 ] ;
71
- return annotate ;
72
- } else { return textFn ; }
73
- } ;
74
- annotate . show = function ( ) {
75
- if ( arguments . length ) {
76
- show = arguments [ 0 ] ;
77
- return annotate ;
78
- } else { return show ; }
79
- } ;
80
52
81
53
//
82
54
// text editor
@@ -107,5 +79,39 @@ export default function() {
107
79
//
108
80
// TODO: add annotations from object
109
81
82
+
83
+ //
84
+ // properties
85
+ annotate . container = function ( _ ) {
86
+ if ( ! arguments . length ) return container ;
87
+ container = _ ;
88
+ container . classed ( 'd3-an-container' , true ) ;
89
+ return annotate ;
90
+ } ;
91
+ annotate . text = function ( _ ) {
92
+ if ( ! arguments . length ) return text ;
93
+ textFn = _ ; return annotate ;
94
+ } ;
95
+ annotate . key = function ( _ ) {
96
+ if ( ! arguments . length ) return keyFn ;
97
+ keyFn = _ ; return annotate ;
98
+ } ;
99
+ annotate . show = function ( _ ) {
100
+ if ( ! arguments . length ) return show ;
101
+ show = _ ; return annotate ;
102
+ } ;
103
+ annotate . textAnchor = function ( _ ) {
104
+ if ( ! arguments . length ) return textAnchor ;
105
+ textAnchor = _ ; return annotate ;
106
+ } ;
107
+ annotate . x = function ( _ ) {
108
+ if ( ! arguments . length ) return x ;
109
+ x = _ ; return annotate ;
110
+ } ;
111
+ annotate . y = function ( _ ) {
112
+ if ( ! arguments . length ) return y ;
113
+ y = _ ; return annotate ;
114
+ } ;
115
+
110
116
return annotate ;
111
117
} ;
0 commit comments