All examples By author By category About

seemantk

Bar Chart II: Causes of Death in Zambia

Visualizing causes of death in Zambia: II

(From the 2015 Living Conditions Survey)

Introduction

Building on the bar chart from the previous example, we'll add some enhancements to make the chart easier to read and give us a deeper level of insight into the dataset.

The smallest improvement will be simply to adjust the angle of rotation of the x-axis tick text. Instead of rotating it clockwise 90 degrees, we'll rotate it anticlockwise by 60 degrees, to make the text easier to read.

Speaking of axes, let's add a y-axis. This simple level of labelling on the y-axis enables us to callibrate the bars in our mind. We can see that FEVER/MALARIA killed a little over 200 people. The bars toward the left, are closer to the axis, and so easier to sort-of-measure :).

We can improve this by adding a tooltip for each bar. Thus, when the user hovers their mouse over a bar, the tooltip will helpfully indicate the exact number of people represented by the bar.

To relax our eyes even more, let's redo the outlines of the rectangles in white. Now the bars appear to be separated rather than touching, making the visualization less crowdy.

Fundamentals

  1. We now rotate the axis tick labels by 60 degreees to make them easier to read.
  2. We add an SVG <g> element to contain all the axes in the chart. By putting them all in one group, we add some order and arrangement to the SVG's structure.
  1. We use the d3.tip() library to add tooltips:
  1. We use the <style> section of the HTML document to set the fill and stroke for the the bars. While we're at it, we make the stroke white now.

Look for the comments in the code to guide you to the new additions.

Analysis

The y axis adds much needed context to the heights of the bar chart bars. Now we can see that FEVER/MALARIA kills more than twice the number of the next cause of death, OTHER (SPECIFY).

The x-axis tick text is easier to read, but still requires effort. The tooltips provide relief by displaying the cause of death in an easy to read manner. We can also provide, using the tooltip, the exact number of people who died from that cause.

This would make a fine summary chart for an overview of Causes of Death in Zambia in 2015.

Improvements

Since d3 is changing and now has a v4 in release, there are a number of improvements and optimizations that are naturally to our advantage. Also, new documentation and examples are more and more likely to to use the new version. Let's take this early opportunity, while the code is relatively simple, to adapt,or port, it to use d3v4.

Next, let's update/port our code to use d3v4