Processing Ring Chart

I was always a big fan of Ring Charts, also known as Multi-level Pie Charts or Radial Treemap, specially the Baobap disk allocation program for Linux, developed by Igalia. Now I want to start to use them for other kind of visualizations as an alternative to treemaps. I didn’t find any implementation in Javascript so I started to develop one on my own using Processing and it’s Processing.js exporter. The design of the classes was inspired by this Java Ring Chart implementation.

Some characteristics of this class: It’s size and the thickness of the rings adapt dynamically to the container. The rings can start and end at any angle. The segments of each ring are divided proportionally based on the segments value and drawn to the proper arc length based on the start and end angle.

You can download the sources at OpenProcessing, what follows is a description of the classes ArcSegment, Ring and RingChart.

The ArcSegment Class

ArcSegment is the basic shape which consists of 2 arcs and 2 lines connecting them. As there is no easy fill method I just paint an ellipse over the inner element. To draw the lines for connecting the arcs I used a PVector and Shiffmans method to rotate the PVector object. To detect a mouseOver I used the mag() and angleBetween() functions of PVector.

<br />
class ArcSegment {<br />
  float centerx, centery, radius, arcwidth, arcstart, extent, left, top;</p>
<p>  ArcSegment(float centerx, float centery, float radius, float arcwidth, float arcstart, float extent) {<br />
    this.centerx = centerx;<br />
    this.centery = centery;<br />
    this.radius = radius;<br />
    this.arcwidth = arcwidth;<br />
    this.arcstart = arcstart;<br />
    this.extent = extent;<br />
    this.left = centerx - radius;<br />
    this.top = centery - radius;<br />
  }</p>
<p>  //source: http://www.shiffman.net/2011/02/03/rotate-a-vector-processing-js/<br />
  void rotate2D(PVector v, float theta) {<br />
    float xTemp = v.x;<br />
    v.x = v.x*cos(theta) - v.y*sin(theta);<br />
    v.y = xTemp*sin(theta) + v.y*cos(theta);<br />
  }</p>
<p>  boolean mouseOver() {<br />
    PVector v = new PVector(mouseX-centerx,mouseY-centery);<br />
    PVector v0 = new PVector(radius/2,0);<br />
    float mouseAngle = degrees(PVector.angleBetween(v, v0));<br />
    float mouseLength = v.mag();<br />
    if (mouseY &amp;lt; centery) mouseAngle = 360-mouseAngle;<br />
    return (mouseLength &amp;gt; radius-arcwidth &amp;amp;&amp;amp; mouseLength &amp;lt; radius &amp;amp;&amp;amp; mouseAngle &amp;gt; arcstart &amp;amp;&amp;amp; mouseAngle &amp;lt; arcstart+extent);<br />
  }</p>
<p>  void draw() {<br />
    //outer arc<br />
    arc(left, top, 2 * radius, 2 * radius, radians(arcstart), radians(arcstart+extent));</p>
<p>    //inner arc<br />
    fill(255);<br />
    ellipse(left + arcwidth, top + arcwidth, 2 * radius - 2 * arcwidth, 2 * radius - 2 * arcwidth);<br />
    noFill();<br />
    arc(left + arcwidth, top + arcwidth, 2 * radius - 2 * arcwidth, 2 * radius - 2 * arcwidth, radians(arcstart), radians(arcstart+extent));</p>
<p>    //lines to connect arcs<br />
    PVector v = new PVector(radius/2,0);<br />
    rotate2D(v,radians(arcstart));<br />
    line(centerx+v.x,centery+v.y,centerx+v.x*2,centery+v.y*2);<br />
    rotate2D(v,radians(extent));<br />
    line(centerx+v.x,centery+v.y,centerx+v.x*2,centery+v.y*2);<br />
  }<br />
}<br />

The Ring Class

This class will create a ring based on the start and end points. It will take a value, a label and a color for each segment of the ring and divide them accordingly based on the values. The Value, Label and Color ArrayLists are set in the Ring class and the ring is created by creating the segments. Each Ring contains the start angle, end angle and the values.

<br />
class Ring {<br />
  ArrayList&amp;lt;String&amp;gt; Labels;<br />
  ArrayList&amp;lt;Float&amp;gt; Values;<br />
  ArrayList&amp;lt;Integer&amp;gt; Colors;<br />
  float x, y, radius, ringwidth, ringstart, ringend;<br />
  ArcSegment[] segments;</p>
<p>  Ring() {<br />
    Labels = new ArrayList();<br />
    Values = new ArrayList();<br />
    Colors = new ArrayList();<br />
    this.ringstart = 0;<br />
    this.ringend = 360;<br />
    this.segments = null;<br />
  }</p>
<p>  void setStart(float ringstart) {<br />
    this.ringstart = ringstart;<br />
  }</p>
<p>  void setEnd(float ringend) {<br />
    this.ringend = ringend;<br />
  }</p>
<p>  int count() {<br />
    return Values.size();<br />
  }</p>
<p>  String getLabel(int index) {<br />
    return Labels.get(index);<br />
  }</p>
<p>  color getColor(int index) {<br />
    return Colors.get(index);<br />
  }</p>
<p>  void setCenter(float x, float y) {<br />
    this.x = x;<br />
    this.y = y;<br />
  }</p>
<p>  void setRadius(float radius) {<br />
    this.radius = radius;<br />
  }</p>
<p>  void setRingWidth(float ringwidth) {<br />
    this.ringwidth = ringwidth;<br />
  }</p>
<p>  void addItem(String label, float val, color col) {<br />
    Labels.add(label);<br />
    Values.add(val);<br />
    Colors.add(col);<br />
  }</p>
<p>  ArcSegment getSegment(int index) {<br />
    return segments[index];<br />
  }</p>
<p>  void createSegments() {<br />
    segments = new ArcSegment[Values.size()];<br />
    float sum = 0;<br />
    float span = this.ringend - this.ringstart;<br />
    for (int i=0; i&amp;lt;Values.size(); i++) {<br />
      sum += Values.get(i);<br />
    }</p>
<p>    float strt = this.ringstart;<br />
    for (int i=0; i&amp;lt;Values.size(); i++) {<br />
      float extent = (Values.get(i) / sum) *span;<br />
      ArcSegment segment = new ArcSegment(this.x, this.y, this.radius, this.ringwidth, strt, extent);<br />
      this.segments[i] = segment;<br />
      strt += extent;<br />
    }<br />
  }</p>
<p>  void draw() {<br />
    for (int i=0; i&amp;lt;segments.length; i++) {<br />
      if (segments[i].mouseOver()) {<br />
        fill(0);<br />
        text(Labels.get(i),20,20);<br />
      }<br />
      else fill(Colors.get(i));<br />
      segments[i].draw();<br />
    }<br />
  }<br />
}<br />

The RingChart Class

The RingChart Class puts it all together, it’s the class that will be called from the application. It takes a ArrayList of type Ring and does all the rendering looping through the draw() method of each ring and and each of their segments.

<br />
class RingChart {<br />
  ArrayList&amp;lt;Ring&amp;gt; rings;</p>
<p>  RingChart() {<br />
    rings = new ArrayList();<br />
  }</p>
<p>  void setRings(ArrayList&amp;lt;Ring&amp;gt; rings){<br />
    this.rings = rings;<br />
  }</p>
<p>  void draw() {<br />
    //draw from bigger to smaller rings<br />
    for (int r = rings.size()-1; r &amp;gt;= 0; r--) {<br />
      Ring ring = rings.get(r);<br />
      float rw = (min(width,height) / (rings.size()))/2 - 20;<br />
      ring.setRingWidth(rw);<br />
      ring.setRadius(2*rw + rw*r);<br />
      ring.setCenter(width/2, height/2);<br />
      ring.createSegments();      </p>
<p>      for (int index = 0; index &amp;lt; ring.count(); index++) {<br />
        ring.draw();<br />
      }<br />
    }<br />
  }<br />
}<br />

You can download the full sources, including the example shown above, at OpenProcessing.

Finally here is the Ring Chart using Processing.js. Because of performance issues it’s just a static version without redrawing and therefor without mouseover detection:

Tags: , , , , , , ,

Leave a Reply