Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 20th, 2012  |  syntax: None  |  size: 2.51 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // from http://mbostock.github.com/d3/ex/chord.html
  2.  
  3. var chord = d3.layout.chord()
  4.   .padding(0.038)
  5.   .sortSubgroups(d3.descending)
  6.   .matrix(livecoding.json.data);
  7.  
  8. var w = $('svg').width(),
  9.     h = $('svg').height(),
  10.     r0 = Math.min(w, h) * 0.41,
  11.     r1 = r0 * 1.1;
  12.  
  13. var fill = d3.scale.ordinal()
  14.     .domain(d3.range(4))
  15.     .range(["#CF6A4C", "#CDA869", "#8F9D6A", "#7587A6"]);
  16.  
  17. var svg = d3.select('svg')
  18.   .append("g")
  19.     .attr("transform", "translate(" + w / 2 + "," + h / 2 + ")");
  20.  
  21. svg.append("g")
  22.   .selectAll("path")
  23.     .data(chord.groups)
  24.   .enter().append("path")
  25.     .style("fill", function(d) { return fill(d.index); })
  26.     .style("stroke", function(d) { return fill(d.index); })
  27.     .attr("d", d3.svg.arc().innerRadius(r0).outerRadius(r1))
  28.     .on("mouseover", fade(0.1))
  29.     .on("mouseout", fade(1));
  30.    
  31. var ticks = svg.append("g")
  32.   .selectAll("g")
  33.     .data(chord.groups)
  34.   .enter().append("g")
  35.   .selectAll("g")
  36.     .data(groupTicks)
  37.   .enter().append("g")
  38.     .attr("transform", function(d) {
  39.       return "rotate(" + (d.angle * 180 / Math.PI - 90) + ")" +
  40.           "translate(" + r1 + ",0)";
  41.     });
  42.  
  43. ticks.append("line")
  44.     .attr("x1", 1)
  45.     .attr("y1", 0)
  46.     .attr("x2", 5)
  47.     .attr("y2", 0)
  48.     .style("stroke", "#000");
  49.  
  50. ticks.append("text")
  51.     .attr("x", 8)
  52.     .attr("dy", ".35em")
  53.     .attr("text-anchor", function(d) {
  54.       return d.angle > Math.PI ? "end" : null;
  55.     })
  56.     .attr("transform", function(d) {
  57.       return d.angle > Math.PI ? "rotate(180)translate(-16)" : null;
  58.     })
  59.     .text(function(d) { return d.label; });
  60.  
  61. svg.append("g")
  62.     .attr("class", "chord")
  63.   .selectAll("path")
  64.     .data(chord.chords)
  65.   .enter().append("path")
  66.     .style("fill", function(d) { return fill(d.target.index); })
  67.     .attr("d", d3.svg.chord().radius(r0))
  68.     .style("opacity", 1);
  69.    
  70. $('.chord path').css('fill-opacity', 0.67);
  71. $('.chord path').css('stroke', '#000');
  72. $('.chord path').css('stroke-width', 0.5);
  73.  
  74. /** Returns an array of tick angles and labels, given a group. */
  75. function groupTicks(d) {
  76.   var k = (d.endAngle - d.startAngle) / d.value;
  77.   return d3.range(0, d.value, 1000).map(function(v, i) {
  78.     return {
  79.       angle: v * k + d.startAngle,
  80.       label: i % 5 ? null : v / 1000 + "k"
  81.     };
  82.   });
  83. }
  84.  
  85. /** Returns an event handler for fading a given chord group. */
  86. function fade(opacity) {
  87.   return function(g, i) {
  88.     svg.selectAll("g.chord path")
  89.         .filter(function(d) {
  90.           return d.source.index != i && d.target.index != i;
  91.         })
  92.       .transition()
  93.         .style("opacity", opacity);
  94.   };
  95. }