Getting Started With Chart.js: Pie, Doughnut, and Bubble Charts

You have learned about four different chart types in Chart.js up to this point. The second tutorial of the series covered line and bar charts. The third tutorial discussed radar and polar area charts. In this tutorial, you will learn how to use Chart.js to create pie, doughnut, and bubble charts.

Creating Pie and Doughnut Charts

Pie and doughnut charts are useful when you want to show the proportion in which something is divided among different entities. For example, you can use pie charts to show the percentage of males, females, and young ones of lions in a wildlife park, or the percentage of votes that different candidates got in an election.

Pie charts are only helpful when you want to compare one specific parameter or set of data. An important thing to keep in mind is that you cannot use pie charts to plot entities whose values are zero because the angle of the sectors in a pie chart depends on the magnitude of the data points.

This means that any entity whose share is zero won't be shown on the chart at all. Similarly, you cannot plot negative values on a pie chart. You can create pie charts in Chart.js by setting the type key to pie. Similarly, you can create doughnut charts by setting the type key to doughnut. Here is an example of creating these two charts:

Let's create a pie chart which shows the oil exports data of the top five countries in 2015. The data is in US billion dollars.

You can control the appearance of the above chart using different keys like cutoutPercentage, which defines the percentage of the chart that is cut out of the middle. You can also specify the start angle of the chart using the rotation key. Similarly, you can also specify the angle that the chart sweeps while plotting the data using the circumference key.

There are two different animations that can be activated while drawing a chart. You can specify if the chart should have a rotation animation using the animateRotate key. Similarly, you can also specify if the doughnut chart should be scaled from the center using the animateScale key. The value of animateRotate is set to true by default, and the value for animateScale is set to false by default.

As usual, you can control the background color, border color, and border width of all the data points using the backgroundColorborderColor, and borderWidth keys respectively. Similarly, the hover values of all these properties can be controlled using the hoverBackgroundColorhoverBorderColor, and hoverBorderWidth keys.

Here is the code to create a doughnut chart for the above data. Setting the value for rotation equal to -Math.PI takes that starting point 180 degrees anti-clockwise. Then, setting the value of circumference to Math.PI makes the chart only span a semicircle.

Creating Bubble Charts

Bubble charts are used to plot or display three dimensions (p1, p2, p3) of data on a chart. The position and size of the bubbles determines the value of these three data points. The horizontal axis represents the first data point (p1), the vertical axis represents the second data point (p2), and the area of the bubble is used to represent the value of the third data point (p3).

One thing that you should keep in mind is that the magnitude of the third data point is not represented by the radius of the bubbles but their area. Now, the area of a circle is proportional to the square of the radius. This means that you have to make sure that the radius of the bubble that you plot is proportional to the square root of the magnitude of the third data point.

You can create bubble charts in Chart.js by setting the value of the type key to bubble. Here is an example of creating a bubble chart.

Let's plot the weight of different items kept in a room using a bubble chart. The data for the chart needs to be passed in the form of an object. The data object needs to have the following interface for it to be plotted properly.

One important difference between bubble charts and all other charts is that the bubble radius is not scaled with the chart. 

For example, the width of bars in a bar chart can increase or decrease based on the chart size. The same thing does not happen with bubble charts. The radius of the bubbles is always equal to the exact number of pixels that you specified. It makes sense because in this chart type, the radius is actually being used to represent real data.

Let's create a bubble chart to plot the population of deer at different spots in a forest. 

Since the radius here is proportional to the square root of the actual magnitude, the number of deer at (80, 80) is 100 times more than the number of deer at (0, 100).

Just like all other chart types, you can control the background color, border color, and border width of plotted points using the backgroundColor, borderColor, and borderWidth keys. Similarly, you can also specify the hover background color, hover border color, and hover border width using the hoverBackgroundColor, hoverBorderColor, and hoverBorderWidth keys. 

You can also specify the additional radius that you want to add to different bubbles on hover using the hoverRadius key. Remember that this radius is added to the original value to draw the hovered bubble. If the original bubble had a radius of 10 and hoverRadius is set to 5, the radius of the bubble on hover will be equal to 15. 

The above parameters will create the following bubble chart.

Final Thoughts

In this tutorial, you learned about three more chart types available in Chart.js. You should now be able to choose the appropriate chart type to plot your data and set specific values for different keys to control their appearance. In the next tutorial, you will learn how to manipulate the scales for different chart types.

I hope you liked this tutorial. If you have any questions, please let me know in the comments.

Tags:

Comments

Related Articles