A graphical toolkit for visualization
Protovis
Overview
Examples
Documentation
Download
Index
« Previous / Next »

Grouped Charts

View full screen.

In this multi-series bar chart, we group bars together rather than stack them. A grouped chart allows accurate comparison of individual values thanks to an aligned baseline: a position, rather than length, judgment is used.

An ordinal scale positions the groups vertically; the bars are then replicated inside a panel, a technique that is also used for small multiples.

Next: Anderson’s Flowers

Source

<html>
  <head>
    <title>Grouped Bar Chart</title>
    <link type="text/css" rel="stylesheet" href="ex.css?3.2"/>
    <script type="text/javascript" src="../protovis-r3.2.js"></script>
    <script type="text/javascript" src="group.js"></script>
    <style type="text/css">

#fig {
  width: 430px;
  height: 275px;
}

    </style>
  </head>
  <body><div id="center"><div id="fig">
    <script type="text/javascript+protovis">

/* Sizing and scales. */
var w = 400,
    h = 250,
    x = pv.Scale.linear(0, 1.1).range(0, w),
    y = pv.Scale.ordinal(pv.range(n)).splitBanded(0, h, 4/5);

/* The root panel. */
var vis = new pv.Panel()
    .width(w)
    .height(h)
    .bottom(20)
    .left(20)
    .right(10)
    .top(5);

/* The bars. */
var bar = vis.add(pv.Panel)
    .data(data)
    .top(function() y(this.index))
    .height(y.range().band)
  .add(pv.Bar)
    .data(function(d) d)
    .top(function() this.index * y.range().band / m)
    .height(y.range().band / m)
    .left(0)
    .width(x)
    .fillStyle(pv.Colors.category20().by(pv.index));

/* The value label. */
bar.anchor("right").add(pv.Label)
    .textStyle("white")
    .text(function(d) d.toFixed(1));

/* The variable label. */
bar.parent.anchor("left").add(pv.Label)
    .textAlign("right")
    .textMargin(5)
    .text(function() "ABCDEFGHIJK".charAt(this.parent.index));

/* X-axis ticks. */
vis.add(pv.Rule)
    .data(x.ticks(5))
    .left(x)
    .strokeStyle(function(d) d ? "rgba(255,255,255,.3)" : "#000")
  .add(pv.Rule)
    .bottom(0)
    .height(5)
    .strokeStyle("#000")
  .anchor("bottom").add(pv.Label)
    .text(x.tickFormat);

vis.render();

    </script>
  </div></div></body>
</html>

Data

var n = 3, m = 4, data = pv.range(n).map(function() {
    return pv.range(m).map(function() {
        return Math.random() + .1;
      });
  });
Copyright 2010 Stanford Visualization Group