In August, I presented a workshop called d3.fundamentals()
at BongoHive in Lusaka, Zambia. The Zambian Government’s Central Statistical Office ran a survey in 2015, to determine the Living Conditions of people throughout the country. They graciously provided anonymized datasets for us to use during the workshop.
Chinyanta Mwenya, a Zambian hacker, collaborated with me to prepare the datasets for the workshop. Chinyanta cleaned the dataset and ran statistical analysis using both Excel and R. We took some time to find a smallish dataset that was easy to understand. We settled on the unfortunately morbid Causes of Death dataset.
For the workshop I assumed a minimal knowledge of HTML and CSS, and no knowledge of Javascript (let alone any knowledge of d3). So, the workshop turned out to be, essentially From Zero to Bar Chart with d3.
Rather than a bunch of pre-done, canned examples, I opted to live-code my way through the agenda. The journey went as follows:
<style>
section<svg>
elements<rect>
angles and <circle>
s<script>
tags in HTML documentsconsole.log()
Array.length()
.
notation[]
notationd3.select()
to manipulate the <SVG>
s we drew earlier.attr
ibutes and style
s of the SVG objects (width, height, fill, stroke)attr
ibutesattr
ibutes and style
s of HTML objects (<div>
s)<div>
s<div>
s, as they tend to align pretty automaticallybackground-color
and border
SVG <rect>
s instead.fill
and stroke
d3.csv()
This bl.ock is the result of that demo. It is presented here with minimal cleanup (spaces/indentations and moving variable declarations near the top).
The goal with this first visualization was to introduce d3 concepts while live-coding. So we built a bar chart showing the number of people who died from each cause listed.
<style>
section contains styling for the axis:{CAUSE:"FEVER/MALARIA", AGE:"38", OTHER:"", SEX:"1"}
CAUSE
{ key: "FEVER/MALARIA", values: [] }
values
is an array of objects with the CAUSE specified by key
values
array in each object.values
array is the # of people that died of CAUSE, or key
.<rect>
for each bar in the bar chart.scale.range([height,0])
). This allows us to present the data in a more intuitive way (with origin at bottom left).<rect>
for each bar, so we need to specify:height
in SVG (y
)coordinates). So, the height is the difference between the y-coordinate of the top-left corner and y-coordinate of the baseThis is a great start to visualizing this dataset. We can easily tell the relative seriousness of each cause of death. We can see that FEVER/MALARIA kills about 2 times more people as the next cause of death (OTHER, or miscellaneous, causes).
It’s a basic chart that gives us a basic insight into the dataset. By making some small changes, we can vastly improve our insight.