Showing posts with label nvd3. Show all posts
Showing posts with label nvd3. Show all posts

Wednesday, February 10, 2016

How, when, and why to use bullet charts



Bullet charts, an invention of Stephen Few's, are a handy visualization tool. Though not often seen, the bullet chart is often a better option than other competing visualization types, such as the dashboard gauge.








Why use a bullet chart?


Why should we prefer to use the bullet chart? The dashboard gauge places all of the values to be analyzed around a circle. This then means that we must make quick angle comparisons if we want to distinguish one value from another.

Take a look at this example from Klipfolio:

How far below our target is our measurement? What is the value that is being indicated by the indicator bar? It turns out that the value is $23.6M. That is an incredibly difficult calculation to make, though -- you have to mentally calculate how many millions of dollars correspond to each portion of the arc, determine how many degrees away from 0 the indicator bar is, and determine the degree difference between the $30M target and the indicator. 

Where the dashboard gauge necessitates an angle comparison to determine the difference between two charts, a bullet chart only requires a comparsion between two common scales. This is preferable because we are making use of a stronger preattentive attribute of visual perception. In other words, we are more readily able to discern objects that make us undertake linear scale comparisons than those that make us undertake slope or angle comparisons. You can see a very useful chart that compares the relative accuracy of different attributes of visual perception here:

The visualization is from Alberto Cairo's superb The Functional Art. If you are unfamiliar with preattentive visual attributes, check out Cairo's book or Stephen Few's excellent article, Tapping the Power of Visual Perception.

We ought to prefer bullet charts because they make use of a stronger preattentive attribute. If we are looking at two bullet charts that are aligned, we even get to make a comparison along two common, aligned scales.


When to use a bullet chart?


A bullet chart is useful when you have a single, continuous variable whose value you wish to indicate in relation to several categorical regions. For example, you might wish to categorize economic growth for a country (measured in percentage points, so a continuous variable) as good, average, or bad (a categorical indicator).


How to create a bullet chart?



There are a number of ways to create bullet charts using modern visualization tools. One approach is to make them from scratch using d3. For a great overview of how to create these, you can read Mike Bostock's Bullet Chart Block.

For those wishing to use a higher-level JavaScript library which will enable you to make a bullet chart using just a few lines, check out NVD3.

Saturday, January 2, 2016

Make great-looking d3.js charts in Python without coding a line of JavaScript

d3.js is an excellent visualization library due to the ability it gives a developer to gain control over all aspects of visualization. However, its extreme power can often also be a problem -- sometimes, we just want to create a set of standard bar and line charts that look good, without needing to spend hours fine-tuning aspects of charts. This is where nvd3 can come in handy: it is a handy library that offers a set of reusable presets based on d3.js. Though you still have some level of control over the output, you can move more quickly and reuse the visualization types that the library supports.

As a Pythonista, I've particularly enjoyed using Python-nvd3 -- a great library that offers a Python wrapper for the nvd3.js interface. You can create great-looking d3.js charts without ever touching a line of JavaScript. I'll go through a quick bar chart example.


We use initialize_javascript so that we are able to render our plots within the iPython notebook, and np.random.seed so we are able to reproduce our results.

In [2]:
import numpy as np
import nvd3


nvd3.ipynb.initialize_javascript(use_remote=True)
np.random.seed(100)

Next, we initialize our discreteBarChart object with our desired chart configurations. We pass it our y and x data series, making sure to convert from the numpy.float64, since nvd3 cannot work with unconverted numpy types at this point.

In [3]:
chart_type = 'discreteBarChart'
chart = nvd3.discreteBarChart(name=chart_type, height=500, width=500)

ydata = [float(x) for x in np.random.randn(10)]
xdata = [int(x) for x in np.arange(10)]

chart.add_serie(y=ydata, x=xdata)
chart.buildhtml()
chart_html = chart.htmlcontent

We can see the output of the rendered HTML -- it's created some JS and HTML elements -

In [4]:
chart_html
Out[4]:
'<!DOCTYPE html>\n<html lang="en">\n    <head>\n        <meta charset="utf-8" />\n        <link href="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.7.0/nv.d3.min.css" rel="stylesheet" />\n        <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>\n        <script src="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.7.0/nv.d3.min.js"></script>\n    </head>\n    <body>\n        \n    <div id="discretebarchart"><svg style="width:500px;height:500px;"></svg></div>\n\n\n    <script>\n\n\n\n                data_discretebarchart=[{"key": "Serie 1", "values": [{"x": 0, "y": -1.7497654730546974}, {"x": 1, "y": 0.34268040332750216}, {"x": 2, "y": 1.153035802563644}, {"x": 3, "y": -0.25243603652138985}, {"x": 4, "y": 0.9813207869512316}, {"x": 5, "y": 0.5142188413943821}, {"x": 6, "y": 0.22117966922140045}, {"x": 7, "y": -1.0700433305682933}, {"x": 8, "y": -0.18949583082317534}, {"x": 9, "y": 0.25500144427338167}], "yAxis": "1"}];\n\n\n            nv.addGraph(function() {\n        var chart = nv.models.discreteBarChart();\n\n        chart.margin({top: 30, right: 60, bottom: 20, left: 60});\n\n        var datum = data_discretebarchart;\n\n\n\n                    chart.yAxis\n                .tickFormat(d3.format(\',.0f\'));\n\n    \n    \n\n        \n\n\n\n            d3.select(\'#discretebarchart svg\')\n            .datum(datum)\n            .transition().duration(500)\n            .attr(\'width\', 500)\n            .attr(\'height\', 500)\n            .call(chart);\n\n    \n        });\n\n\n\n    </script>\n\n    </body>\n</html>'
And if we want to view the actual chart, we can use the display and HTML functions below:

In [5]:
from IPython.display import display, HTML


display(HTML(chart_html))

For more info, check out the official nvd3.js docs here, or the Python-nvd3 docs here.

Sign up to receive data viz talk tips and updates via email. We're low traffic and take privacy very seriously.