Bubble Chart with Chart.js
Bubble charts are similar to scatter plots, but it adds one more variable (also known as dimension): the size of each point (marker) also represents a value.
The bubble chart in Figure 12.6 shows how median household income (x-axis) and test performance (y-axis) in 6 school districts in Connecticut are related. The size of data point corresponds to the number of students enrolled in the school district: bigger circles represent larger school districts.
To create your own bubble chart with Chart.js, with data loaded from a CSV file, you can:
Go to our GitHub repo for this template, log into your GitHub account, and click Use this template to create a copy that you can edit.
Prepare your data in CSV format and upload into a
data.csvfile. The first two columns should contain x- and y-values respectively. The third column should contain bubble names that will appear on mouse hover. The final, fourth column, represents the size of your bubble.
| income | grades | district | enrollment | | 29430 | -1.7 | Hartford | 21965 | | 82322 | 1.5 | West Hartford | 10078 | | 50400 | -1.4 | East Hartford | 7053 |
script.js, customize the values of variables shown in the code snippet below:
var TITLE = 'Income, Test Scores, and Enrollment in Select \ Connecticut School Districts, 2009-13'; var POINT_X = 'income'; // column name for x values in data.csv var POINT_X_PREFIX = '$'; // prefix for x values, eg '$' var POINT_X_POSTFIX = ''; // postfix for x values, eg '%' var POINT_Y = 'grades'; // column name for y values in data.csv var POINT_Y_PREFIX = ''; // prefix for x values, eg 'USD ' var POINT_Y_POSTFIX = ''; // postfix for x values, eg ' kg' var POINT_R = 'enrollment'; // column name for radius in data.csv var POINT_R_DESCRIPTION = 'Enrollment'; // description of radius value var POINT_R_PREFIX = ''; // prefix for radius values, eg 'USD ' var POINT_R_POSTFIX = ' students'; // postfix for radius values, eg ' kg' var R_DENOMINATOR = 800; // use this to scale the dot sizes, or set to 1 // if your dataset contains precise radius values var POINT_NAME = 'district'; // point names that appear in tooltip var POINT_COLOR = 'rgba(0,0,255,0.7)'; // eg `black` or `rgba(10,100,44,0.8)` var X_AXIS = 'Median Household Income, USD'; // x-axis label, label in tooltip var Y_AXIS = 'Grade, Relative to Average'; // y-axis label, label in tooltip var SHOW_GRID = true; // `true` to show the grid, `false` to hide
Tip: To display smaller data points that may be hidden behind larger neighbors, use semi-transparent circles with RGBA color codes. The first three characters represent red, green, and blue, while the
a stands for
alpha and represents the level of transparency on a scale from 0.0 (full transparent) to 1.0 (fully opaque). For example,
rgba(160, 0, 0, 0.5) creates a red color that is semi-transparent. Learn more by playing with RGBA color values at W3Schools.
If you have more than three variables that you would like to show in your bubble chart, you can use color and glyphs (instead of simple dots) to represent two extra dimentions. For example, you may want to use the blue color to only show school districts in Fairfield County (generally a richer part of CT) and gray color to represent all other districts. You may want to use circles, squares, and triangles to represent results for males, females, and non-binary students. We won’t be showing you how to achieve this, but we can assure you that it can be done in 5-10 extra lines of code.
Chart.js is pretty limitless when it comes to customization, but remember not to overwhelm the viewer and communicate only the data that are necessary to prove or illustrate your idea.
In this chapter, we introduced Chart.js and Highcharts templates that can be used to construct rich and interactive charts that you can host in your own GitHub account, and embed them anywhere on the web. You can use these templates as a base to kickstart your interactive visualizations. You can refer to Chart.js Samples and Chart.js documentation for more information on Chart.js customization and troubleshooting. Highcharts Demos gallery shows plenty of charts along with the code that you can copy, and Highcharts API Reference lists all features available to refine your visualizations. Just remember that you need to obtain a license to use Highcharts in commercial projects.