pie chart in php

Create Dynamic pie chart in php using Google API and Chart.js, PHP and MySQL with demo

by Code4berry. Last modified on April 1st, 2022.

In this tutorial, you will learn how to create a Pie Chart of the Mysql data using the Google API and chart.js, PHP and MySQL.

A pie chart is a roundabout factual realistic which is separated into cuts to envision mathematical information. The bend length of a pie graph is relative to the amount it addresses. Pie diagrams are generally utilized in the business portrayal and the broad communications.

Google API gives a simple answer for make outlines on the site. In this article, we give bit by bit cycle to make a powerful pie outline.

How to create pie chart with Mysql dynamic data

A diagram which is made to show records in the extent inside a round graph in type of areas is known as Pie Chart. Each sector is proportion of total and total value of pie is always 100 percent.

Dynamic Pie diagram implies that getting information from data set to show in extent in areas. This instructional exercise will assist you with making Pie diagram for your web application.

Utilizing the Google Chart devices we will make pie outline in PHP with MySQL. Google Pie diagram apparatus is open source and simple to use in web application. Google have wide range of chart types and you can use as per need.


									header('Content-Type: application/json');

									$conn = mysqli_connect("localhost","root","","phpgraphs");

									$sqlQuery = "SELECT student_id,student_name,marks FROM tbl_marks ORDER BY student_id";

									$result = mysqli_query($conn,$sqlQuery);

									$data = array();
									foreach ($result as $row) {
									$data[] = $row;


								echo json_encode($data);


								<!DOCTYPE html>
								<title>Creating dynamic data chart using PHP and Chart.js</title>
								<style type="text/css">
								BODY {
								width: 550PX;

							#chart-container {
							width: 100%;
							height: auto;
						.card {
						position: relative;
						display: -webkit-box;
						display: -ms-flexbox;
						display: flex;
						-webkit-box-orient: vertical;
						-webkit-box-direction: normal;
						-ms-flex-direction: column;
						flex-direction: column;
						min-width: 0;
						word-wrap: break-word;
						background-color: #fff;
						background-clip: border-box;
						border: 1px solid rgba(0, 0, 0, 0.125);
						border-radius: 0.25rem;

					.card-body {
					-webkit-box-flex: 1;
					-ms-flex: 1 1 auto;
					flex: 1 1 auto;
					padding: 1.25rem;
				<script type="text/javascript" src="js/jquery.min.js"></script>
				<script type="text/javascript" src="js/Chart.min.js"></script>
				<div class="card-body">
				<p>How to create pie chart with Mysql dynamic data </p>
				<div class="card"  id="chart-container">
				<canvas id="graphCanvas"></canvas>
				<script type="text/javascript">
				url: "data.php",
				method: "GET",
				success: function(data){
				var name = [];
				var marks = [];

				for (var i in data){

			var chartdata = {
			labels: name,
			datasets: [{
			label: 'Student Marks',
			backgroundColor : ['#f56954', '#00a65a', '#f39c12', '#00c0ef', '#3c8dbc', '#d2d6de'],
			hoverBackgroundColor: 'rgba(230, 236, 235, 0.75)',
			hoverBorderColor: 'rgba(230, 236, 235, 0.75)',
			data: marks

	var graphTarget = $("#graphCanvas");
	var barGraph = new Chart(graphTarget, {
	type: 'pie',
	data: chartdata,

error: function(data) {




Amanya Owen

You have clean, latest and well organized code.

Arnold Tumu

This site helped me to learn coding very fast ,thank you

Christine Bestie

This code helped me to analyze my data

Leave a Comment:

You Might Also Like

hostel booking

Hostel booking management system
Learn More

car rental

Car rental management system in Php
Learn More

student details

Student details management system
Learn More


Tourism management system in Php and Mysql
Learn More

Latest Tutorial

sweet alerts

How to delete table row using sweet alert2
Learn More


How to create piechart with Mysql data
Learn More

register and login

How to register and login in php
Learn More

edit data

How to edit Mysql data in modal using php
Learn More