How to create a pie chart using Canvas?

Jun 18, 2025

Leave a message

Amanda Sun
Amanda Sun
Quality Control Manager at Shandong Shengrun Textile Co.,LTD. I ensure every product meets our stringent quality standards. Passionate about precision and excellence in textile manufacturing.

Hey there! As a Canvas supplier, I'm super stoked to share with you how to create a pie chart using Canvas. Canvas is an amazing tool that gives you the power to draw all sorts of graphics right in your web browser. Whether you're a data analyst looking to visualize some stats, a teacher wanting to make your lessons more engaging, or just someone who loves playing around with cool visuals, this guide is for you.

What is Canvas?

First off, let's quickly talk about what Canvas is. Canvas is an HTML element that provides a drawing surface for graphics via JavaScript. It's like a blank canvas (pun intended) where you can create all kinds of shapes, animations, and visualizations. It's widely supported by modern browsers, so you don't have to worry about compatibility issues.

Getting Started

To start creating a pie chart, you'll need a basic HTML file with a Canvas element. Here's a simple setup:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pie Chart with Canvas</title>
</head>

<body>
    <canvas id="myChart" width="400" height="400"></canvas>
    <script>
        // Your JavaScript code will go here
    </script>
</body>

</html>

In this code, we've created a Canvas element with an id of myChart and set its width and height to 400 pixels. Now, let's move on to the JavaScript part.

Accessing the Canvas Context

To draw on the Canvas, we need to get its context. The context is an object that provides methods and properties for drawing. In our case, we'll use the 2D context. Here's how you can get it:

const canvas = document.getElementById('myChart');
const ctx = canvas.getContext('2d');

Data Preparation

Before we start drawing the pie chart, we need some data. Let's say we have the following data representing the sales of different products in a store:

const data = [
    { label: 'Product A', value: 20 },
    { label: 'Product B', value: 30 },
    { label: 'Product C', value: 15 },
    { label: 'Product D', value: 35 }
];

Calculating Angles

To draw a pie chart, we need to calculate the angles for each slice based on the data values. The total sum of all values is used to determine the proportion of each slice. Here's the code to calculate the angles:

let total = 0;
for (let i = 0; i < data.length; i++) {
    total += data[i].value;
}

const angles = [];
for (let i = 0; i < data.length; i++) {
    const angle = (data[i].value / total) * 2 * Math.PI;
    angles.push(angle);
}

Drawing the Pie Chart

Now comes the fun part - drawing the pie chart! We'll loop through the data and draw each slice using the calculated angles. Here's the code:

let startAngle = 0;
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
const radius = Math.min(canvas.width, canvas.height) / 2;

const colors = ['#FF6384', '#36A2EB', '#FFCE56', '#4BC0C0'];

for (let i = 0; i < data.length; i++) {
    const endAngle = startAngle + angles[i];

    ctx.beginPath();
    ctx.moveTo(centerX, centerY);
    ctx.arc(centerX, centerY, radius, startAngle, endAngle);
    ctx.closePath();

    ctx.fillStyle = colors[i];
    ctx.fill();

    startAngle = endAngle;
}

In this code, we first define the starting angle, the center of the canvas, and the radius of the pie chart. Then, we loop through the data and draw each slice using the arc method. We set the fill color for each slice and fill it.

Adding Labels

To make the pie chart more informative, we can add labels to each slice. Here's how you can do it:

startAngle = 0;
for (let i = 0; i < data.length; i++) {
    const endAngle = startAngle + angles[i];
    const midAngle = startAngle + (angles[i] / 2);

    const labelX = centerX + (radius * 0.6) * Math.cos(midAngle);
    const labelY = centerY + (radius * 0.6) * Math.sin(midAngle);

    ctx.font = '14px Arial';
    ctx.fillStyle = 'black';
    ctx.textAlign = 'center';
    ctx.fillText(data[i].label, labelX, labelY);

    startAngle = endAngle;
}

In this code, we calculate the midpoint of each slice and use it to position the label. We then set the font, fill color, and text alignment, and draw the label using the fillText method.

Using Our Canvas Products

At our company, we offer a wide range of high-quality Canvas products that can be used for various applications. Whether you're creating a simple pie chart like the one we just made or working on a more complex project, our Canvas products are up to the task.

Check out our Canvas Print Fabric Duck Fabric for a durable and versatile option. It's perfect for printing your visualizations and displaying them in a professional setting.

If you're in the medical field, our 100% Cotton Antichorine Medical Fabric For Uniform is a great choice. It's not only comfortable but also resistant to antichorine, making it ideal for uniforms.

Viscose Plain FabricViscose Plain Fabric

And for those looking for a fabric that can withstand chlorine bleaching, our TC resistant to chlorine bleaching hospital fabric is the way to go. It's perfect for hospital settings where cleanliness is of utmost importance.

Conclusion

Creating a pie chart using Canvas is a fun and rewarding experience. With just a little bit of JavaScript knowledge, you can visualize your data in a visually appealing way. And if you're in the market for high-quality Canvas products, we've got you covered.

If you're interested in our Canvas products or have any questions about creating visualizations using Canvas, don't hesitate to reach out. We're here to help you with all your Canvas needs. Whether you're a small business owner, a student, or a professional, we have the right products and expertise to support your projects.

References

  • HTML Canvas Tutorial - Mozilla Developer Network
  • JavaScript Basics - W3Schools
Send Inquiry
you dream it, we design it
Shandong Shengrun Textile Co.,LTD.
contact us