Getting Started With Chart.js: Pie, Doughnut, and Bubble Charts
This tutorial will explain how to create pie, ring, and bubble charts using Chart.js. Previously, we have learned four chart types of Chart.js: line chart and bar chart (tutorial 2), as well as radar chart and polar region chart (tutorial 3).
Create pie and ring charts
Pie charts and ring charts are ideal for showing the proportions of a whole that is divided into different parts. For example, a pie chart can be used to show the percentage of male lions, female lions and young lions in a safari, or the percentage of votes that different candidates receive in the election.
Pie charts are only suitable for comparing single parameters or datasets. It should be noted that the pie chart cannot draw entities with zero value because the angle of the fan in the pie chart depends on the numerical size of the data point. This means that any entity with a proportion of zero will not be displayed on the chart. Similarly, negative values cannot be drawn on pie charts.
In Chart.js, you can create a pie chart by setting type
key to pie
, and a ring chart by setting type
key to doughnut
. Here is an example of creating both charts:
var pieChart = new Chart(votesCanvas, { type: 'pie', data: votesData, options: chartOptions }); var doughnutChart = new Chart(votesCanvas, { type: 'doughnut', data: votesData, options: chartOptions });
Let's create a pie chart showing oil export data (unit: billion US dollars) for the top five oil exporters in 2015.
var data = { labels: [ "Saudi Arabia", "Russia", "Iraq", "UAE", "Canada" ], datasets: [ { data: [133.3, 86.2, 52.2, 51.2, 50.2], backgroundColor: [ "#FF6384", "#63FF84", "#84FF63", "#8463FF", "#6384FF" ] }] };
You can use different keys to control the display of the above chart, such as cutout
, which can be a number or a string. If specified as a number, the value is considered a pixel value; if specified as a string ending in % , the value is considered a percentage of the total radius. You can use rotation
key to specify the starting angle of the chart. Similarly, you can also use circumference
key to specify the angle at which the chart scans when plotting the data. Both angles are expressed in degrees rather than radians.
Two different animations can be activated when drawing a chart. You can use the animateRotate
key to specify whether the chart should have a rotation animation. Similarly, you can also use the animateScale
key to specify whether the ring graph should be scaled from the center. The value of animateRotate
is true
by default, and the value of animateScale
is false
by default.
As usual, you can use backgroundColor
, borderColor
, and borderWidth
keys to control the background color, border color and border width of all data points, respectively. Likewise, the hover values for all these properties can be controlled using hoverBackgroundColor
, hoverBorderColor
, and hoverBorderWidth
keys.
The following is the code to create a ring graph for the above data. Set the value of rotation
to -90
Set the start point to rotate 90 degrees counterclockwise.
var oilData = { labels: ["Saudi Arabia", "Russia", "Iraq", "UAE", "Canada"], datasets: [ { data: [133.3, 86.2, 52.2, 51.2, 50.2], backgroundColor: ["#FF6384", "#63FF84", "#84FF63", "#8463FF", "#6384FF"], borderColor: "black", borderWidth: 2 } ] }; var chartOptions = { rotation: -90, cutout: "45%", plugins: { title: { display: true, position: "bottom", text: "Major oil exporters in 2015", font: { size: 32 } }, legend: { position: "left", align: "start" } }, animation: { animateRotate: false, animateScale: true } }; var donutChart = new Chart(oilCanvas, { type: "doughnut", data: oilData, options: chartOptions });
Create a bubble chart
Bubble charts are used to draw or display three dimensions of data on a chart ( p1 , p2 , p3 ). The position and size of the bubble determine the values 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 ).
It should be noted that the size of the third data point is not represented by the radius of the bubbles, but by their area. The area of the circle is proportional to the square of the radius. This means you have to make sure that the bubble radius you are drawing is proportional to the square root of the size of the third data point.
In Chart.js, you can create a bubble chart by setting the value of type
key to bubble
. Here is an example of how to create a bubble chart:
var bubbleChart = new Chart(popCanvas, { type: 'bubble', data: popData, options: chartOptions });
Let's use bubble charts to draw the weight of different items in the room. The data of the chart needs to be passed in the object format. The data object needs to have the following interface to draw correctly.
{ x:<number> , y:<number> , r:<number> }</number></number></number>
An important difference between a bubble chart and all other charts is that the bubble radius does not scale with the chart.
For example, the width of a bar in a bar chart increases or decreases according to the size of the chart. This won't happen with the bubble chart. The radius of the bubble is always equal to the exact number of pixels you specify. This makes sense because in this chart type, the radius is actually used to represent the real data.
Let's create a bubble chart to plot the number of deer herds at different locations in the forest.
var popData = { datasets: [ { label: ["Deer Herd"], data: [ { x: 100, y: 0, r: 10 }, { x: 60, y: 30, r: 20 }, { x: 40, y: 60, r: 25 }, { x: 80, y: 80, r: 30 }, { x: 20, y: 30, r: 25 }, { x: 0, y: 100, r: 5 } ], backgroundColor: "#FF9966" } ] };
Since the radius here is proportional to the square root of the actual number, the number of deer at (80, 80) is 36 times the number of deer at (0, 100).
Like all other chart types, you can use backgroundColor
, borderColor
, and borderWidth
keys to control the background color, border color, and border width of the drawn points. Similarly, you can also use hoverBackgroundColor
, hoverBorderColor
, and hoverBorderWidth
keys to specify the hover background color, hover border color, and hover border width.
You can also use hoverRadius
key to specify the extra radius to add to different bubbles when hovering. Remember that this radius will be added to the original value to draw the hovering bubble. If the radius of the original bubble is 10 and hoverRadius
is set to 5, the radius of the bubble on hover will be equal to 15.
var popData = { datasets: [ { label: ["Deer Herd"], data: [ { x: 100, y: 0, r: 10 }, { x: 60, y: 30, r: 20 }, { x: 40, y: 60, r: 25 } ], backgroundColor: "#9966FF", hoverBackgroundColor: "#FFFFF", hoverBorderColor: "#9966FF", hoverBorderWidth: 5, hoverRadius: 5 }, { label: ["Giraffe Number"], data: [ { x: 80, y: 80, r: 30 }, { x: 20, y: 30, r: 25 }, { x: 0, y: 100, r: 5 } ], backgroundColor: "#FF6600", hoverBackgroundColor: "#FFFFF", hoverBorderColor: "#FF6600", hoverBorderWidth: 5, hoverRadius: 5 } ] }; var chartOptions = { plugins: { title: { display: true, position: "bottom", text: "Number of animals in different hot spots", font: { size: 20 } }, legend: { position: "bottom", align: "center" } }, layout: { padding: 20 } };
The above parameters will create the following bubble chart.
Summarize
In this tutorial, you learned three other chart types available in Chart.js. You should now be able to select the appropriate chart type to plot the data and set specific values for different keys to control its appearance. In the next tutorial, you will learn how to manipulate scales for different chart types.
The above is the detailed content of Getting Started With Chart.js: Pie, Doughnut, and Bubble Charts. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...
