@@ -5,10 +5,9 @@ export default function() {
55 var keyFn = ( _ , ndx ) => ndx ,
66 textFn = ( d ) => d ,
77 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' ,
1211 show = true ,
1312 dragControl = drag ( )
1413 . on ( "start" , function ( ) { this . classList . add ( 'dragging' ) ; } )
@@ -22,7 +21,6 @@ export default function() {
2221 //
2322 // serialize keys, bind click to add text, add text if `show` is T or fn
2423 function annotate ( selection ) {
25- container . classed ( 'd3-an-container' , true ) ;
2624 selection . nodes ( ) . forEach ( ( el , ndx ) => el . __key__ = keyFn ( el . __data__ , ndx ) ) ;
2725 selection . on ( 'click' , function ( ) { appendText ( select ( this ) ) ; } ) ;
2826 if ( show ) { appendText ( selection , true ) ; }
@@ -32,14 +30,9 @@ export default function() {
3230 // add new data bound <text> annotation
3331 function appendText ( sel , filter ) {
3432 var _sel = ( show instanceof Function && filter ) ? sel . filter ( show ) : sel ,
35- _keyFn = ( d ) => keyFn ( d . data ) ,
3633 _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 ( ) } ;
4336 } ) ;
4437
4538 var textSelection = container . selectAll ( 'text.with-data' )
@@ -48,7 +41,6 @@ export default function() {
4841 . text ( _textFn )
4942 . attr ( 'class' , 'annotation with-data' )
5043 . attr ( 'x' , x ) . attr ( 'y' , y )
51- . attr ( 'dx' , dx ) . attr ( 'dy' , dy )
5244 . attr ( 'text-anchor' , textAnchor )
5345 . call ( dragControl )
5446 . on ( 'click' , function ( ) {
@@ -57,26 +49,6 @@ export default function() {
5749 } ) ;
5850 }
5951
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- } ;
8052
8153 //
8254 // text editor
@@ -107,5 +79,39 @@ export default function() {
10779 //
10880 // TODO: add annotations from object
10981
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+
110116 return annotate ;
111117} ;
0 commit comments