@@ -85,25 +85,97 @@ const svgen = (d3,jsondata={})=>{
8585 }
8686 else if ( jsondata ?. target === 'histogram' ) {
8787
88+ let size = ( jsondata . hasOwnProperty ( 'size' ) ) ? parseFloat ( jsondata . size ) : 1 ;
89+
8890 const dmax = d3 . max ( jsondata ?. dataset ) ;
8991
92+ let animate = ( jsondata . hasOwnProperty ( 'animate' ) ) ? jsondata . animate : false ;
93+
94+ if ( animate ) {
95+
96+ function enterAnim ( enter ) {
97+
98+ enter . append ( 'rect' )
99+ . attr ( 'x' , ( d , i ) => i * ( jsondata ?. intergap + jsondata ?. barwidth ) * size )
100+ . attr ( 'width' , jsondata ?. barwidth * size )
101+ . attr ( "y" , d => ( dmax - d ) * size )
102+ . attr ( "height" , d => d * size )
103+ . attr ( "class" , 'bar' )
104+
105+
106+ . call ( rect =>
107+ rect . append ( 'animate' )
108+ . attr ( 'attributeName' , "height" )
109+ . attr ( 'values' , ( d ) => `0;1;${ d * size } ` )
110+ . attr ( 'keyTimes' , ( d , i ) => `0;${ Math . trunc ( 1000 * ( 1 - ( 1 / ( 3 * i + 3 ) ) ) ) / 1000 } ;1` ) // ="0; 0.25; 0.5; 0.75; 1"
111+ . attr ( 'dur' , ( d , i ) => `${ i * 2 + 2 } s` )
112+ . attr ( 'repeatcount' , 1 )
113+
114+ )
115+
116+ . call ( rect =>
117+ rect . append ( 'animate' )
118+ . attr ( 'attributeName' , "y" )
119+ . attr ( 'values' , ( d ) => `${ dmax * size } ;${ dmax * size } ;${ ( dmax - d ) * size } ` )
120+ . attr ( 'keyTimes' , ( d , i ) => `0;${ Math . trunc ( 1000 * ( 1 - ( 1 / ( 3 * i + 3 ) ) ) ) / 1000 } ;1` )
121+ . attr ( 'dur' , ( d , i ) => `${ i * 2 + 2 } s` )
122+ . attr ( 'repeatcount' , 1 )
123+ )
124+ }
125+
126+ svg . append ( 'g' )
127+ . attr ( 'fill' , jsondata ?. color )
128+ . attr ( 'transform' , `translate(${ 200 / size } ,${ 200 / size } )` )
129+ . selectAll ( 'rect' )
130+ . data ( jsondata ?. dataset )
131+ . join (
132+ enter => enterAnim ( enter )
133+ )
134+ /* .selectAll('animate')
135+ .data([d => d], (h,i)=>{ return { "begin":i, "height":h} } ) //
136+ .join(
137+ enter=>enter.append('animate')
138+ .attr('attributeName', "height")
139+ .attr('values', (d)=> `0;${ d.height }` ) // `0;50` function(d){ return `0;${ d }` }
140+ .attr('begin', d => `2s` ) // `${d.begin}s` () =>{ indx++; `${ indx }s`; }
141+ .attr('dur', "1s")
142+ .attr('repeatcount', 1)
143+ ) */
144+ }
145+ else {
146+
147+ svg . append ( 'g' )
148+ . attr ( 'fill' , jsondata ?. color )
149+ . attr ( 'transform' , `translate(${ 200 / size } ,${ 200 / size } )` )
150+ . selectAll ( 'rect' )
151+ . data ( jsondata ?. dataset )
152+ . join ( 'rect' )
153+ . attr ( 'x' , ( d , i ) => i * ( jsondata ?. intergap + jsondata ?. barwidth ) * size )
154+ . attr ( 'width' , jsondata ?. barwidth * size )
155+ . attr ( "y" , d => ( dmax - d ) * size )
156+ . attr ( "height" , d => d * size )
157+ }
158+
90159 svg . append ( 'g' )
91- . attr ( 'fill' , jsondata ?. color )
92- . attr ( 'transform' , `translate(200,200)` )
93- . selectAll ( 'rect' )
94- . data ( jsondata ?. dataset )
95- . join ( 'rect' )
96- . attr ( 'x' , ( d , i ) => i * ( jsondata ?. intergap + jsondata ?. barwidth ) )
97- . attr ( 'width' , jsondata ?. barwidth )
98- . attr ( "y" , d => ( dmax - d ) )
99- . attr ( "height" , d => d )
160+ . attr ( 'fill' , jsondata ?. color )
161+ . attr ( 'transform' , `translate(${ 200 / size } ,${ 200 / size } )` )
162+ . selectAll ( 'text' )
163+ . data ( jsondata ?. dataset )
164+ . join ( 'text' )
165+ . attr ( 'x' , ( d , i ) => i * ( jsondata ?. intergap + jsondata ?. barwidth ) * size )
166+ . attr ( "y" , d => ( dmax - d ) * size - 20 )
167+ . style ( "font-size" , 10 + size * 2 )
168+ . style ( 'font-family' , 'sans-serif' )
169+ . text ( d => d )
100170
101171
102172 }
173+
103174
104175 console . log ( svg . node ( ) . outerHTML )
105176
106177 return svg . node ( ) . outerHTML ;
107178}
108179
109- module . exports = { svgen }
180+
181+ module . exports = svgen ;
0 commit comments