@@ -11,12 +11,12 @@ d3.layout.wordStream = function(){
1111 frequencyScale = d3 . scale . linear ( ) ,
1212 spiral = achemedeanSpiral ,
1313 canvas = cloudCanvas ,
14- interpolation = "cardinal " ;
14+ interpolation = "linear " ;
1515 let wordStream = { } ;
16-
16+
1717 let cloudRadians = Math . PI / 180 ,
18- cw = 1 << 11 ,
19- ch = 1 << 11 ;
18+ cw = 1 << 11 ,
19+ ch = 1 << 11 ;
2020
2121 wordStream . boxes = function ( ) {
2222 let boxWidth = size [ 0 ] / data . length ;
@@ -47,10 +47,10 @@ d3.layout.wordStream = function(){
4747 }
4848 return boxes ;
4949 } ;
50-
50+
5151 //#region helper functions
5252 function buildFontScale ( data ) {
53-
53+
5454 let topics = d3 . keys ( data [ 0 ] . words ) ;
5555 //#region scale for the font size.
5656 let maxFrequency = 0 ;
@@ -80,9 +80,7 @@ d3.layout.wordStream = function(){
8080 } ) ;
8181 if ( total > max ) max = total ;
8282 } ) ;
83- frequencyScale = d3 . scale . linear ( )
84- . domain ( [ 0 , max ] )
85- . range ( [ 0 , size [ 1 ] ] ) ;
83+ frequencyScale . domain ( [ 0 , max ] ) . range ( [ 0 , size [ 1 ] ] ) ;
8684 }
8785 //Convert from data to box
8886 function buildBoxes ( data ) {
@@ -98,23 +96,17 @@ d3.layout.wordStream = function(){
9896 topics . forEach ( topic => {
9997 let dataPerTopic = [ ] ;
10098 //Push the first point
101- dataPerTopic . push ( { x : 0 , y :totalFrequencies [ 0 ] [ topic ] } ) ;
99+ dataPerTopic . push ( { x : 0 , y :frequencyScale ( totalFrequencies [ 0 ] [ topic ] ) } ) ;
102100 totalFrequencies . forEach ( ( frq , i ) => {
103- dataPerTopic . push ( { x : ( i * boxWidth ) + ( boxWidth >> 1 ) , y : frq [ topic ] } ) ;
101+ dataPerTopic . push ( { x : ( i * boxWidth ) + ( boxWidth >> 1 ) , y : frequencyScale ( frq [ topic ] ) } ) ;
104102 } ) ;
105103 //Push the last point
106- dataPerTopic . push ( { x : size [ 0 ] , y :totalFrequencies [ totalFrequencies . length - 1 ] [ topic ] } ) ;
104+ dataPerTopic . push ( { x : size [ 0 ] , y :frequencyScale ( totalFrequencies [ totalFrequencies . length - 1 ] [ topic ] ) } ) ; //TODO:
107105 allPoints . push ( dataPerTopic ) ;
108106 } ) ;
109- let layers = d3 . layout . stack ( ) . offset ( 'wiggle' ) ( allPoints ) ;
110- //Process the scale of each box here.
111- layers . forEach ( layer => {
112- layer . forEach ( point => {
113- point . y0 = frequencyScale ( point . y0 ) ;
114- point . y = frequencyScale ( point . y ) ;
115- } ) ;
116- } ) ;
117- let innerBoxes = { } ;
107+ var layers = d3 . layout . stack ( ) . offset ( 'silhouette' ) ( allPoints ) ;
108+
109+ var innerBoxes = { } ;
118110 topics . forEach ( ( topic , i ) => {
119111 innerBoxes [ topic ] = [ ] ;
120112 for ( let j = 1 ; j < layers [ i ] . length - 1 ; j ++ ) {
@@ -140,7 +132,7 @@ d3.layout.wordStream = function(){
140132 maxDelta = ~ ~ Math . sqrt ( ( board . boxWidth * board . boxWidth ) + ( board . boxHeight * board . boxHeight ) ) ,
141133 startX = ~ ~ ( board . boxX + ( board . boxWidth * ( Math . random ( ) + .5 ) >> 1 ) ) ,
142134 startY = ~ ~ ( board . boxY + ( board . boxHeight * ( Math . random ( ) + .5 ) >> 1 ) ) ,
143- s = spiral ( [ board . boxWidth , board . boxHeight ] ) ,
135+ s = spiral ( [ board . boxWidth , board . boxHeight ] ) ,
144136 dt = Math . random ( ) < .5 ? 1 : - 1 ,
145137 t = - dt ,
146138 dxdy , dx , dy ;
@@ -180,21 +172,21 @@ d3.layout.wordStream = function(){
180172
181173 let bsi = ( j + word . y + word . y0 ) * bw + i + ( word . x + word . x0 ) ; //board sprite index
182174 let boardPixel = board . sprite [ bsi ] ;
183-
175+
184176 if ( boardPixel != 0 && wordPixel != 0 ) {
185177 return true ;
186178 }
187179 }
188- }
180+ }
189181 return false ;
190182 }
191183 function placeWordToBoard ( word , board ) {
192184 //Add the sprite
193185 let y0 = word . y + word . y0 ,
194- x0 = word . x + word . x0 ,
195- bw = board . width ,
196- ww = word . width ,
197- wh = word . height ;
186+ x0 = word . x + word . x0 ,
187+ bw = board . width ,
188+ ww = word . width ,
189+ wh = word . height ;
198190 for ( let j = 0 ; j < wh ; j ++ ) {
199191 for ( let i = 0 ; i < ww ; i ++ ) {
200192 let wsi = j * ww + i ;
@@ -206,7 +198,7 @@ d3.layout.wordStream = function(){
206198
207199 function buildSvg ( boxes , topic ) {
208200 streamPath1 = Array ( ) ,
209- streamPath2 = Array ( ) ;
201+ streamPath2 = Array ( ) ;
210202 let width = size [ 0 ] ,
211203 height = size [ 1 ] ;
212204 let svg = d3 . select ( document . createElement ( 'svg' ) ) . attr ( {
@@ -215,38 +207,38 @@ d3.layout.wordStream = function(){
215207 } ) ;
216208 let graphGroup = svg . append ( 'g' ) ;
217209 let n = boxes . length ;
218-
210+
219211 let catIndex = boxes . topics . indexOf ( topic ) ;
220-
212+
221213 let area1 = d3 . svg . area ( )
222- . interpolate ( interpolation )
223- . x ( function ( d ) { return d . x ; } )
224- . y0 ( 0 )
225- . y1 ( function ( d ) { return d . y0 ; } ) ;
226-
214+ . interpolate ( interpolation )
215+ . x ( function ( d ) { return d . x ; } )
216+ . y0 ( 0 )
217+ . y1 ( function ( d ) { return d . y0 ; } ) ;
218+
227219
228220 let area2 = d3 . svg . area ( )
229- . interpolate ( 'cardinal' )
230- . x ( function ( d ) { return d . x ; } )
231- . y0 ( function ( d ) { return ( d . y + d . y0 ) ; } )
232- . y1 ( height ) ;
221+ . interpolate ( interpolation )
222+ . x ( function ( d ) { return d . x ; } )
223+ . y0 ( function ( d ) { return ( d . y + d . y0 ) ; } )
224+ . y1 ( height ) ;
233225
234226 graphGroup . append ( 'path' ) . datum ( boxes . layers [ catIndex ] )
235- . attr ( {
236- d : area1 ,
237- stroke : 'red' ,
238- 'stroke-width' : 2 ,
239- fill :'red' ,
240- id : 'path1'
241- } ) ;
227+ . attr ( {
228+ d : area1 ,
229+ stroke : 'red' ,
230+ 'stroke-width' : 2 ,
231+ fill :'red' ,
232+ id : 'path1'
233+ } ) ;
242234 graphGroup . append ( 'path' ) . datum ( boxes . layers [ catIndex ] )
243- . attr ( {
244- d : area2 ,
245- stroke : 'red' ,
246- 'stroke-width' : 2 ,
247- fill :'red' ,
248- id : 'path2'
249- } ) ;
235+ . attr ( {
236+ d : area2 ,
237+ stroke : 'red' ,
238+ 'stroke-width' : 2 ,
239+ fill :'red' ,
240+ id : 'path2'
241+ } ) ;
250242 return svg ;
251243 }
252244 function buildCanvas ( boxes , topic ) {
@@ -313,32 +305,32 @@ d3.layout.wordStream = function(){
313305 d . fontSize = ~ ~ fontScale ( d . frequency ) ;
314306 d . rotate = ( ~ ~ ( Math . random ( ) * 6 ) - 3 ) * rotateCorner ;
315307 c . font = ~ ~ ( d . fontSize + 1 ) + "px " + font ;
316-
308+
317309 let w = ~ ~ ( c . measureText ( d . text ) . width ) ,
318310 h = d . fontSize ;
319311 if ( d . rotate ) {
320- let sr = Math . sin ( d . rotate * cloudRadians ) ,
321- cr = Math . cos ( d . rotate * cloudRadians ) ,
322- wcr = w * cr ,
323- wsr = w * sr ,
324- hcr = h * cr ,
325- hsr = h * sr ;
326- w = ~ ~ Math . max ( Math . abs ( wcr + hsr ) , Math . abs ( wcr - hsr ) ) ;
327- h = ~ ~ Math . max ( Math . abs ( wsr + hcr ) , Math . abs ( wsr - hcr ) ) ;
328- }
312+ let sr = Math . sin ( d . rotate * cloudRadians ) ,
313+ cr = Math . cos ( d . rotate * cloudRadians ) ,
314+ wcr = w * cr ,
315+ wsr = w * sr ,
316+ hcr = h * cr ,
317+ hsr = h * sr ;
318+ w = ~ ~ Math . max ( Math . abs ( wcr + hsr ) , Math . abs ( wcr - hsr ) ) ;
319+ h = ~ ~ Math . max ( Math . abs ( wsr + hcr ) , Math . abs ( wsr - hcr ) ) ;
320+ }
329321 if ( h > maxh ) maxh = h ;
330322 if ( x + w >= cw ) {
331- x = 0 ;
332- y += maxh ;
333- maxh = 0 ;
323+ x = 0 ;
324+ y += maxh ;
325+ maxh = 0 ;
334326 }
335327 if ( y + h >= ch ) break ;
336328 c . translate ( ( x + ( w >> 1 ) ) , ( y + ( h >> 1 ) ) ) ;
337329 if ( d . rotate ) c . rotate ( d . rotate * cloudRadians ) ;
338330 c . fillText ( d . text , 0 , 0 ) ;
339331 if ( d . padding ) c . lineWidth = 2 * d . padding , c . strokeText ( d . text , 0 , 0 ) ;
340332 c . restore ( ) ;
341-
333+
342334 d . width = w ;
343335 d . height = h ;
344336 d . x = x ;
@@ -350,7 +342,7 @@ d3.layout.wordStream = function(){
350342 d . timeStep = i ;
351343 d . streamHeight = frequencyScale ( d . frequency ) ;
352344 x += w ;
353- }
345+ }
354346 } ) ;
355347 }
356348 for ( let bc = 0 ; bc < data . length ; bc ++ ) {
@@ -365,13 +357,13 @@ d3.layout.wordStream = function(){
365357 h = d . height ,
366358 x = d . x ,
367359 y = d . y ;
368-
369- let pixels = c . getImageData ( d . x , d . y , d . width , d . height ) . data ;
370- d . sprite = Array ( ) ;
371- for ( let i = 0 ; i << 2 < pixels . length ; i ++ ) {
372- d . sprite . push ( pixels [ i << 2 ] ) ;
373- }
374- }
360+
361+ let pixels = c . getImageData ( d . x , d . y , d . width , d . height ) . data ;
362+ d . sprite = Array ( ) ;
363+ for ( let i = 0 ; i << 2 < pixels . length ; i ++ ) {
364+ d . sprite . push ( pixels [ i << 2 ] ) ;
365+ }
366+ }
375367 } ) ;
376368 }
377369 //Only return this to test if needed
@@ -403,9 +395,9 @@ d3.layout.wordStream = function(){
403395 } ;
404396 function rectangularSpiral ( size ) {
405397 let dy = 4 ,
406- dx = dy * size [ 0 ] / size [ 1 ] ,
407- x = 0 ,
408- y = 0 ;
398+ dx = dy * size [ 0 ] / size [ 1 ] ,
399+ x = 0 ,
400+ y = 0 ;
409401 return function ( t ) {
410402 let sign = t < 0 ? - 1 : 1 ;
411403 switch ( ( Math . sqrt ( 1 + 4 * sign * t ) - sign ) & 3 ) {
0 commit comments