1 <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
3 #slice_interaction_chart_placeholder {
5 margin: -40px 20px 20px 0px;
12 font: 10px sans-serif;
27 transition: opacity 0.3s;
29 #circle:hover path.fade {
33 text-decoration: none;
34 border-bottom: 1px dotted #666;
47 text-decoration: none;
56 font-family:'Arial', sans-serif;
62 background-color: lightyellow;
64 #sliceEngagementTitle {
71 <h3 id="sliceEngagementTitle"> Involvement between Slices by User Engagement</h3>
72 <div id="slice_interaction_chart_placeholder"></div>
76 /* d3.json(datasetURL, function(error, matrix) {
78 if (error) {alert("Error reading file: ", error.statusText); return; }
81 actualData = [[2, 2, 2, 2, 2, 2, 2, 1, 2, 1],
82 [2, 7, 3, 7, 7, 3, 2, 2, 7, 2],
83 [2, 3, 4, 3, 4, 2, 2, 2, 3, 2],
84 [2, 7, 3, 7, 7, 3, 2, 2, 7, 2],
85 [2, 7, 4, 7, 15, 3, 2, 6, 7, 6],
86 [2, 3, 2, 3, 3, 3, 2, 1, 3, 1],
87 [2, 2, 2, 2, 2, 2, 2, 1, 2, 1],
88 [1, 2, 2, 2, 6, 1, 1, 6, 2, 6],
89 [2, 7, 3, 7, 7, 3, 2, 2, 7, 2],
90 [1, 2, 2, 2, 6, 1, 1, 6, 2, 6]];
92 // Chord Diagram for showing Collaboration between users found in an anchor query
98 outerRadius = Math.min(width, height) / 2 - 100,
99 innerRadius = outerRadius - 18;
101 var dataset = "#allinfo";
102 //string url for the initial data set
103 //would usually be a file path url, here it is the id
104 //selector for the <pre> element storing the data
106 //create number formatting functions
107 var formatPercent = d3.format("%");
108 var numberWithCommas = d3.format("0,f");
110 //create the arc path data generator for the groups
111 var arc = d3.svg.arc()
112 .innerRadius(innerRadius)
113 .outerRadius(outerRadius);
115 //create the chord path data generator for the chords
116 var path = d3.svg.chord()
117 .radius(innerRadius);
119 //define the default chord layout parameters
120 //within a function that returns a new layout object;
121 //that way, you can create multiple chord layouts
122 //that are the same except for the data.
123 function getDefaultLayout() {
124 return d3.layout.chord()
126 .sortSubgroups(d3.descending)
127 .sortChords(d3.ascending);
129 var last_layout; //store layout between updates
130 var users = [{"color": "#005586", "name": "Owl"}, {"color": "#6ebe49", "name": "DnsDemux"}, {"color": "orange", "name": "Infrastructure"}, {"color": "#707170", "name": "HyperCache"}, {"color": "#00c4b3", "name": "Syndicate"}, {"color": "#077767", "name": "Hadoop"}, {"color": "dodgerblue", "name": "Stork"}, {"color": "#a79b94", "name": "test2"}, {"color": "#c4e76a", "name": "DnsRedir"}, {"color": "red", "name": "test"}];
132 /*** Initialize the visualization ***/
133 var g = d3.select("#slice_interaction_chart_placeholder").append("svg")
134 .attr("width", width)
135 .attr("height", height)
137 .attr("id", "circle")
139 "translate(" + width / 2 + "," + height / 2 + ")");
140 //the entire graphic will be drawn within this <g> element,
141 //so all coordinates will be relative to the center of the circle
144 .attr("r", outerRadius);
145 //this circle is set in CSS to be transparent but to respond to mouse events
146 //It will ensure that the <g> responds to all mouse events within
147 //the area, even after chords are faded out.
149 /*** Read in the neighbourhoods data and update with initial data matrix ***/
150 //normally this would be done with file-reading functions
151 //d3.csv and d3.json and callbacks,
152 //instead we're using the string-parsing functions
153 //d3.csv.parse and JSON.parse, both of which return the data,
154 //no callbacks required.
157 updateChords(dataset);
158 //call the update method with the default dataset
160 //} ); //end of d3.csv function
163 /* Create OR update a chord layout from a data matrix */
164 function updateChords( datasetURL ) {
166 /* d3.json(datasetURL, function(error, matrix) {
168 if (error) {alert("Error reading file: ", error.statusText); return; }
171 //var matrix = JSON.parse( d3.select(datasetURL).text() );
172 var matrix = actualData;
173 // instead of d3.json
175 /* Compute chord layout. */
176 layout = getDefaultLayout(); //create a new layout object
177 layout.matrix(matrix);
179 /* Create/update "group" elements */
180 var groupG = g.selectAll("g.group")
181 .data(layout.groups(), function (d) {
183 //use a key function in case the
184 //groups are sorted differently between updates
191 .remove(); //remove after transitions are complete
193 var newGroups = groupG.enter().append("g")
194 .attr("class", "group");
195 //the enter selection is stored in a variable so we can
196 //enter the <path>, <text>, and <title> elements as well
199 //Create the title tooltip for the new groups
200 newGroups.append("title");
202 //Update the (tooltip) title text based on the data
203 groupG.select("title")
204 .text(function(d, i) {
205 return "Slice (" + users[i].name +
210 //create the arc paths and set the constant attributes
211 //(those based on the group index, not on the value)
212 newGroups.append("path")
213 .attr("id", function (d) {
214 return "group" + d.index;
215 //using d.index and not i to maintain consistency
216 //even if groups are sorted
218 .style("fill", function (d) {
219 return users[d.index].color;
222 //update the paths to match the layout
223 groupG.select("path")
226 .attr("opacity", 0.5) //optional, just to observe the transition
227 .attrTween("d", arcTween( last_layout ))
228 // .transition().duration(100).attr("opacity", 1) //reset opacity
231 //create the group labels
232 newGroups.append("svg:text")
233 .attr("xlink:href", function (d) {
234 return "#group" + d.index;
237 .attr("color", "#fff")
239 return users[d.index].name;
242 //position group labels to match layout
243 groupG.select("text")
246 .attr("transform", function(d) {
247 d.angle = (d.startAngle + d.endAngle) / 2;
248 //store the midpoint angle in the data object
250 return "rotate(" + (d.angle * 180 / Math.PI - 90) + ")" +
251 " translate(" + (innerRadius + 26) + ")" +
252 (d.angle > Math.PI ? " rotate(180)" : " rotate(0)");
253 //include the rotate zero so that transforms can be interpolated
255 .attr("text-anchor", function (d) {
256 return d.angle > Math.PI ? "end" : "begin";
260 /* Create/update the chord paths */
261 var chordPaths = g.selectAll("path.chord")
262 .data(layout.chords(), chordKey );
263 //specify a key function to match chords
267 //create the new chord paths
268 var newChords = chordPaths.enter()
270 .attr("class", "chord");
272 // Add title tooltip for each new chord.
273 newChords.append("title");
275 // Update all chord title texts
276 chordPaths.select("title")
278 if (users[d.target.index].name !== users[d.source.index].name) {
279 return [numberWithCommas(d.source.value),
280 " users in common between \n",
281 users[d.source.index].name,
283 users[d.target.index].name,
286 //joining an array of many strings is faster than
287 //repeated calls to the '+' operator,
288 //and makes for neater code!
290 else { //source and target are the same
291 return numberWithCommas(d.source.value)
292 + " users are only in Slice ("
293 + users[d.source.index].name + ")";
297 //handle exiting paths:
298 chordPaths.exit().transition()
303 //update the path shape
304 chordPaths.transition()
306 //.attr("opacity", 0.5) //optional, just to observe the transition
307 .style("fill", function (d) {
308 return users[d.source.index].color;
310 .attrTween("d", chordTween(last_layout))
311 //.transition().duration(100).attr("opacity", 1) //reset opacity
314 //add the mouseover/fade out behaviour to the groups
315 //this is reset on every update, so it will use the latest
316 //chordPaths selection
317 groupG.on("mouseover", function(d) {
318 chordPaths.classed("fade", function (p) {
319 //returns true if *neither* the source or target of the chord
320 //matches the group that has been moused-over
321 return ((p.source.index != d.index) && (p.target.index != d.index));
324 //the "unfade" is handled with CSS :hover class on g#circle
325 //you could also do it using a mouseout event:
327 g.on("mouseout", function() {
328 if (this == g.node() )
329 //only respond to mouseout of the entire circle
330 //not mouseout events for sub-components
331 chordPaths.classed("fade", false);
335 last_layout = layout; //save for next update
337 // }); //end of d3.json
340 function arcTween(oldLayout) {
341 //this function will be called once per update cycle
343 //Create a key:value version of the old layout's groups array
344 //so we can easily find the matching group
345 //even if the group index values don't match the array index
346 //(because of sorting)
349 oldLayout.groups().forEach( function(groupData) {
350 oldGroups[ groupData.index ] = groupData;
354 return function (d, i) {
356 var old = oldGroups[d.index];
357 if (old) { //there's a matching old group
358 tween = d3.interpolate(old, d);
361 //create a zero-width arc object
362 var emptyArc = {startAngle:d.startAngle,
363 endAngle:d.startAngle};
364 tween = d3.interpolate(emptyArc, d);
367 return function (t) {
368 return arc( tween(t) );
373 function chordKey(data) {
374 return (data.source.index < data.target.index) ?
375 data.source.index + "-" + data.target.index:
376 data.target.index + "-" + data.source.index;
378 //create a key that will represent the relationship
379 //between these two groups *regardless*
380 //of which group is called 'source' and which 'target'
382 function chordTween(oldLayout) {
383 //this function will be called once per update cycle
385 //Create a key:value version of the old layout's chords array
386 //so we can easily find the matching chord
387 //(which may not have a matching index)
392 oldLayout.chords().forEach( function(chordData) {
393 oldChords[ chordKey(chordData) ] = chordData;
397 return function (d, i) {
398 //this function will be called for each active chord
401 var old = oldChords[ chordKey(d) ];
403 //old is not undefined, i.e.
404 //there is a matching old chord value
406 //check whether source and target have been switched:
407 if (d.source.index != old.source.index ){
408 //swap source and target to match the new data
415 tween = d3.interpolate(old, d);
418 //create a zero-width chord object
420 var oldGroups = oldLayout.groups().filter(function(group) {
421 return ( (group.index == d.source.index) ||
422 (group.index == d.target.index) )
424 old = {source:oldGroups[0],
425 target:oldGroups[1] || oldGroups[0] };
426 //the OR in target is in case source and target are equal
427 //in the data, in which case only one group will pass the
430 if (d.source.index != old.source.index ){
431 //swap source and target to match the new data
441 source: { startAngle: old.source.startAngle,
442 endAngle: old.source.startAngle},
443 target: { startAngle: old.target.startAngle,
444 endAngle: old.target.startAngle}
446 tween = d3.interpolate( emptyChord, d );
449 return function (t) {
450 //this function calculates the intermediary shapes
451 return path(tween(t));
457 /* Activate the buttons and link to data sets */
458 d3.select("#ReadersButton").on("click", function () {
459 updateChords( "#readinfo" );
460 //replace this with a file url as appropriate
462 //enable other buttons, disable this one
466 d3.select("#ContributorsButton").on("click", function() {
467 updateChords( "#contributorinfo" );
471 d3.select("#AllUsersButton").on("click", function() {
472 updateChords( "#allinfo" );
475 function disableButton(buttonNode) {
476 d3.selectAll("button")
477 .attr("disabled", function(d) {
478 return this === buttonNode? "true": null;