Zoomable Sequences sunburst

by gdicerbo

HTML

<body>
    <div id="main">
      <div id="sequence"></div>
      <div id="chart"></div>
    </div>

CSS

/* Styles go here */

#main {
  float: left;
  width: 500px;
}

#sidebar {
  float: right;
  width: 500px;
}

#sequence {
  width: 500px;
  height: 70px;
}


#sequence text {
  font-family: 'Open Sans', sans-serif;
  font-size: 13px;
  font-weight: 600;
  fill: #fff;
}

#chart {
  position: relative;
}

#chart path {
  stroke: #fff;
}

#barchart {
  position: absolute;
  top: 260px;
  left: 750px;
  width: 300px;
  text-align: center;
  color: #666;
  z-index: -1;
}


#barchart rect {
  fill: steelblue;
}

#Barchart text {
  fill: white;
  font: 10px sans-serif;
  text-anchor: middle;
}

JavaScript

// Dimensions of sunburst.
var width = 900;
var widthc = 1050;
var height = 600;
var radius = Math.min(width, height) / 2;

// Breadcrumb dimensions: width, height, spacing, width of tip/tail.
var b = { w: 220, h: 50, s: 3, t: 50 };

// make `colors` an ordinal scale
var colors = d3.scale.category20c();

// Total size of all segments; we set this later, after loading the data.
var totalSize = 0; 

var vis = d3.select("#chart").append("svg:svg")
    .attr("width", width)
    .attr("height", height)
    .append("svg:g")
    .attr("id", "container")
    .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

var partition = d3.layout.partition()
    .size([2 * Math.PI, radius * radius])
    .value(function(d) { return d.size; });

var arc = d3.svg.arc()
    .startAngle(function(d) { return d.x; })
    .endAngle(function(d) { return d.x + d.dx; })
    .innerRadius(function(d) { return Math.sqrt(d.y); })
    .outerRadius(function(d) { return Math.sqrt(d.y + d.dy); });


var json = getData();
createVisualization(json);

// Main function to draw and set up the visualization, once we have the data.
function createVisualization(json) {

  // Basic setup of page elements.
  initializeBreadcrumbTrail();
  
  // Bounding circle underneath the sunburst, to make it easier to detect
  // when the mouse leaves the parent g.
  vis.append("svg:circle")
      .attr("r", radius)
      .style("opacity", 0);

  // For efficiency, filter nodes to keep only those large enough to see.
  var nodes = partition.nodes(json)
      .filter(function(d) {
      return (d.dx > 0.005); // 0.005 radians = 0.29 degrees
      });        

  var path = vis.data([json]).selectAll("path")
      .data(nodes)
      .enter().append("svg:path")
      .attr("display", function(d) { return d.depth ? null : "none"; })
      .attr("d", arc)
      .attr("fill-rule", "evenodd")
      .style("fill", function(d) { return d.color; })
      .style("opacity", 1)
      .on("mouseover", mouseover)
     ...