@@ -31,7 +31,7 @@ const Main = (function() {
3131 lm = new LabelManager ( svg ) ;
3232
3333 // load and render initial dataset by default
34- changeDataset ( 3 ) ;
34+ changeDataset ( 2 ) ;
3535
3636 // svg event listeners
3737 svg . on ( 'row-resize' , function ( e ) {
@@ -57,8 +57,11 @@ const Main = (function() {
5757 changeDataset ( this . selectedIndex ) ;
5858 }
5959 }
60- document . getElementById ( 'options-toggle' ) . onclick = function ( ) {
61- document . getElementById ( 'options' ) . classList . toggle ( 'open' ) ;
60+ document . getElementById ( 'taxonomy-toggle' ) . onclick = function ( ) {
61+ document . getElementById ( 'taxonomy' ) . classList . add ( 'open' ) ;
62+ }
63+ document . getElementById ( 'taxonomy' ) . onclick = function ( e ) {
64+ e . target . classList . remove ( 'open' ) ;
6265 }
6366 }
6467
@@ -71,8 +74,102 @@ const Main = (function() {
7174 clear ( ) ;
7275 ymlToJson . convert ( 'taxonomy.yml.txt' , function ( taxonomy ) {
7376 [ words , links , clusters ] = buildWordsAndLinks ( ) ;
74- populateOptions ( ) ;
77+
78+ // turn taxonomy into a proper tree
79+ let tree = ( function ( ) {
80+
81+ let flat = [ ] ;
82+
83+ function createLinks ( val , i , n , parent ) {
84+ let index = { i, n } ;
85+ let obj = {
86+ val,
87+ parent,
88+ index : parent ? parent . index . concat ( index ) : [ index ] ,
89+ depth : parent ? parent . depth + 1 : 0 ,
90+ ancestor : parent ? parent . ancestor : null ,
91+ children : null
92+ } ;
93+ if ( ! obj . ancestor ) {
94+ obj . ancestor = obj ;
95+ obj . descendantCount = 0 ;
96+ }
97+ ++ obj . ancestor . descendantCount ;
98+
99+ flat . push ( obj ) ;
100+
101+ if ( ! ( typeof val === 'string' || val instanceof String ) ) {
102+ let key = Object . keys ( val ) [ 0 ] ;
103+ obj . val = key ;
104+ obj . children = val [ key ] . map ( ( v , i ) => createLinks ( v , i , val [ key ] . length , obj ) ) ;
105+ }
106+ return obj ;
107+ }
108+
109+ let hierarchy = taxonomy . map ( ( val , i ) => createLinks ( val , i , taxonomy . length , null ) ) ;
110+
111+ return {
112+ hierarchy,
113+ flat
114+ }
115+ } ) ( ) ;
116+
117+ let tagTypes = { } ;
118+ words . forEach ( word => {
119+ if ( word . tag ) {
120+ if ( tagTypes [ word . tag ] ) {
121+ tagTypes [ word . tag ] . push ( word ) ;
122+ }
123+ else {
124+ tagTypes [ word . tag ] = [ word ] ;
125+ }
126+ }
127+ if ( word . clusters . length > 0 ) {
128+ word . clusters . forEach ( cluster => {
129+ if ( tagTypes [ cluster . val ] ) {
130+ tagTypes [ cluster . val ] . push ( cluster ) ;
131+ }
132+ else {
133+ tagTypes [ cluster . val ] = [ cluster ] ;
134+ }
135+ } ) ;
136+ }
137+ } ) ;
138+ colors = [
139+ '#6185b5' ,
140+ '#ff7f0e' ,
141+ '#52a717' ,
142+ '#d62728' ,
143+ '#9467bd' ,
144+ '#8c564b' ,
145+ '#e377c2' ,
146+ '#347847' ,
147+ '#909108' ,
148+ '#17becf'
149+ ] ;
150+
151+ let div = document . querySelector ( '#taxonomy > div' ) ;
152+
153+ tree . flat . forEach ( el => {
154+ let u = document . createElement ( 'div' ) ;
155+ u . style . color = colors [ el . depth ] ;
156+ u . style . marginLeft = el . depth * 15 + 5 + 'px' ;
157+ u . appendChild ( document . createTextNode ( el . val ) ) ;
158+ div . appendChild ( u ) ;
159+ } ) ;
160+
75161 draw ( ) ;
162+ console . log ( Object . keys ( tagTypes ) , tree . flat . filter ( x => tagTypes [ x . val ] ) ) ;
163+ Object . keys ( tagTypes ) . forEach ( ( tag , i ) => {
164+ tagTypes [ tag ] . forEach ( word => {
165+ if ( word instanceof Word ) {
166+ word . tag . svgText . node . style . fill = colors [ i ] ;
167+ }
168+ else {
169+ word . svgText . node . style . fill = colors [ i ] ;
170+ }
171+ } ) ;
172+ } ) ;
76173 } ) ;
77174 } ) ;
78175 } ;
0 commit comments