|
20 | 20 | //Filter and take only dates in 2013 |
21 | 21 | rawData = rawData.filter(function(d){ |
22 | 22 | var time = inputFormat.parse(d.time); |
23 | | - var starDate = inputFormat.parse('2013-01-01T00:00:00'); |
24 | | - var endDate = inputFormat.parse('2014-01-01T00:00:00'); |
| 23 | + var starDate = inputFormat.parse('2014-01-01T00:00:00'); |
| 24 | + var endDate = inputFormat.parse('2014-07-01T00:00:00'); |
25 | 25 | return time >= starDate && time < endDate; |
26 | 26 | }); |
27 | 27 | var data = {}; |
|
56 | 56 | }).sort(function(a, b){//sort the terms by frequency |
57 | 57 | return b.frequency-a.frequency; |
58 | 58 | }).filter(function(d){return d.text; })//filter out empty words |
59 | | - .slice(0, 20); |
| 59 | + .slice(0, 30); |
60 | 60 | }); |
61 | 61 | return { |
62 | 62 | date: date, |
|
67 | 67 | }); |
68 | 68 | draw(data); |
69 | 69 | }); |
70 | | - // var data = getDummyData(); |
71 | | - // draw(data); |
72 | 70 | function draw(data){ |
73 | 71 | //Layout data |
74 | | - var width = 1200, height = 500; |
| 72 | + var width = 720, height = 300; |
75 | 73 | var interpolation = "cardinal"; |
76 | 74 | var axisPadding = 10; |
77 | 75 | var margins = {left: 20, top: 20, right: 10, bottom: 30}; |
|
114 | 112 | //Main group |
115 | 113 | var mainGroup = svg.append('g').attr('transform', 'translate(' + margins.left + ',' + margins.top + ')'); |
116 | 114 | var wordStreamG = mainGroup.append('g'); |
117 | | - |
| 115 | + |
| 116 | + var topics = boxes.topics; |
118 | 117 | mainGroup.selectAll('path') |
119 | 118 | .data(boxes.layers) |
120 | 119 | .enter() |
|
126 | 125 | .attr({ |
127 | 126 | 'fill-opacity': 0.1, |
128 | 127 | stroke: 'black', |
129 | | - 'stroke-width': 0.3 |
| 128 | + 'stroke-width': 0.3, |
| 129 | + topic: function(d, i){return topics[i];} |
130 | 130 | }); |
131 | 131 | var allWords = []; |
132 | 132 | d3.map(boxes.data, function(row){ |
133 | 133 | boxes.topics.forEach(topic=>{ |
134 | 134 | allWords = allWords.concat(row.words[topic]); |
135 | 135 | }); |
136 | 136 | }); |
137 | | - allWords = allWords.filter(d=> d.placed==true);//Filter out not placable words |
138 | | - |
139 | | - //Display text |
140 | | - var topics = boxes.topics; |
141 | 137 | var c20 = d3.scale.category20b(); |
142 | 138 | //Color based on the topic |
143 | 139 | var topicColorMap = d3.scale.ordinal().domain(topics).range(c20.range()); |
|
164 | 160 | fill: function(d, i){return termColorMap(d.text);}, |
165 | 161 | 'text-anchor': 'middle', |
166 | 162 | 'alignment-baseline': 'middle', |
167 | | - topic: function(d){return d.topic;} |
| 163 | + topic: function(d){return d.topic;}, |
| 164 | + visibility: function(d, i){ return d.placed ? "visible": "hidden";} |
168 | 165 | }); |
169 | 166 | //Try |
170 | 167 | var prevColor; |
|
175 | 172 | var text = thisText.text(); |
176 | 173 | var topic = thisText.attr('topic'); |
177 | 174 | var allTexts = mainGroup.selectAll('text').filter(t =>{ |
178 | | - return t.text === text && t.topic === topic; |
| 175 | + return t && t.text === text && t.topic === topic; |
179 | 176 | }); |
180 | 177 | allTexts.attr({ |
181 | 178 | stroke: 'red', |
|
187 | 184 | var text = thisText.text(); |
188 | 185 | var topic = thisText.attr('topic'); |
189 | 186 | var allTexts = mainGroup.selectAll('text').filter(t =>{ |
190 | | - return t.text === text && t.topic === topic; |
| 187 | + return t && !t.cloned && t.text === text && t.topic === topic; |
191 | 188 | }); |
192 | 189 | allTexts.attr({ |
193 | 190 | stroke: 'none', |
194 | 191 | 'stroke-width': '0' |
195 | 192 | }); |
196 | | - wordStreamG.selectAll('path').remove(); |
197 | | - //Set the other text visibles |
198 | | - //Hide all other texts |
199 | | - var allOtherTexts = mainGroup.selectAll('text').filter(t =>{ |
200 | | - return t.text != text && t.topic === topic; |
201 | | - }); |
202 | | - allOtherTexts.attr('visibility', 'visible'); |
203 | 193 | }); |
204 | 194 | //Click |
205 | 195 | mainGroup.selectAll('text').on('click', function(){ |
206 | 196 | var thisText = d3.select(this); |
207 | 197 | var text = thisText.text(); |
208 | 198 | var topic = thisText.attr('topic'); |
209 | 199 | var allTexts = mainGroup.selectAll('text').filter(t =>{ |
210 | | - return t.text === text && t.topic === topic; |
| 200 | + return t && t.text === text && t.topic === topic; |
211 | 201 | }); |
| 202 | + //Select the data for the stream layers |
| 203 | + var streamLayer = d3.select("path[topic='"+ topic+"']" )[0][0].__data__; |
212 | 204 | //Push all points |
213 | | - var points = []; |
| 205 | + var points = Array(); |
| 206 | + //Initialize all points |
| 207 | + streamLayer.forEach(elm => { |
| 208 | + points.push({ |
| 209 | + x: elm.x, |
| 210 | + y0: elm.y0+elm.y, |
| 211 | + y: 0//zero as default |
| 212 | + }); |
| 213 | + }); |
214 | 214 | allTexts[0].forEach(t => { |
215 | 215 | var data = t.__data__; |
216 | | - var x = data.x; |
217 | | - var y = data.y; |
218 | 216 | var fontSize = data.fontSize; |
219 | | - points.push({ |
220 | | - x: x, |
221 | | - y0: y-(fontSize>>1), |
222 | | - y: fontSize |
223 | | - }); |
| 217 | + //The point |
| 218 | + var thePoint = points[data.timeStep+1];;//+1 since we added 1 to the first point and 1 to the last point. |
| 219 | + thePoint.y = -data.streamHeight; |
| 220 | + //Set it to visible. |
| 221 | + //Clone the nodes. |
| 222 | + var clonedNode = t.cloneNode(true); |
| 223 | + d3.select(clonedNode).attr({ |
| 224 | + visibility: "visible", |
| 225 | + stroke: 'none', |
| 226 | + 'stroke-size': 0 |
| 227 | + }); |
| 228 | + var clonedParentNode = t.parentNode.cloneNode(false); |
| 229 | + clonedParentNode.appendChild(clonedNode); |
| 230 | + |
| 231 | + t.parentNode.parentNode.appendChild(clonedParentNode); |
| 232 | + d3.select(clonedParentNode).attr({ |
| 233 | + cloned: true, |
| 234 | + topic: topic |
| 235 | + }).transition().duration(300).attr({ |
| 236 | + transform: function(d, i){return 'translate('+thePoint.x+','+(thePoint.y0+thePoint.y-fontSize/2)+')';}, |
| 237 | + }); |
224 | 238 | }); |
225 | 239 | //Append stream |
226 | 240 | wordStreamG.append('path') |
|
230 | 244 | .attr({ |
231 | 245 | 'fill-opacity': 1, |
232 | 246 | stroke: 'black', |
233 | | - 'stroke-width': 0.3}); |
| 247 | + 'stroke-width': 0.3, |
| 248 | + topic: topic, |
| 249 | + wordStream: true |
| 250 | + }); |
234 | 251 | //Hide all other texts |
235 | 252 | var allOtherTexts = mainGroup.selectAll('text').filter(t =>{ |
236 | | - return t.text != text && t.topic === topic; |
| 253 | + return t && !t.cloned && t.topic === topic; |
237 | 254 | }); |
238 | 255 | allOtherTexts.attr('visibility', 'hidden'); |
239 | 256 | }); |
| 257 | + topics.forEach(topic=>{ |
| 258 | + d3.select("path[topic='"+ topic+"']" ).on('click', function(){ |
| 259 | + mainGroup.selectAll('text').filter(t=>{ |
| 260 | + return t && !t.cloned && t.placed && t.topic === topic; |
| 261 | + }).attr({ |
| 262 | + visibility: 'visible' |
| 263 | + }); |
| 264 | + //Remove the cloned element |
| 265 | + document.querySelectorAll("g[cloned='true'][topic='"+topic+"']").forEach(node=>{ |
| 266 | + node.parentNode.removeChild(node); |
| 267 | + }); |
| 268 | + //Remove the added path for it |
| 269 | + document.querySelectorAll("path[wordStream='true'][topic='"+topic+"']").forEach(node=>{ |
| 270 | + node.parentNode.removeChild(node); |
| 271 | + }); |
| 272 | + }); |
| 273 | + |
| 274 | + }); |
240 | 275 |
|
241 | 276 | //Build the legends |
242 | 277 | var legendGroup = svg.append('g').attr('transform', 'translate(' + margins.left + ',' + (height+margins.top) + ')'); |
|
262 | 297 | axisNodes.selectAll('.tick line').attr({ |
263 | 298 | fill: 'none', |
264 | 299 | }); |
| 300 | + axisNodes.selectAll('.tick text').attr({ |
| 301 | + 'font-family': 'serif', |
| 302 | + 'font-size': 10 |
| 303 | + }); |
265 | 304 | } |
266 | 305 | function styleGridlineNodes(gridlineNodes){ |
267 | 306 | gridlineNodes.selectAll('.domain').attr({ |
|
0 commit comments