337warrior
Tech Trainer
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
This tutorial is about how to create a Pie Graph using PHP/MySQLi. In here, you will be able to learn the simple way of creating a pie graph by fetching the data inside the database. This pie graph is usually use in Analytics sales report to determine what kind of products have the most number of purchase. Whoever has the biggest slice in the pie graph it is the product that has the most number purchased by the customers. Follow the instructions below to see how it works.
Creating Database
Create a database named “salesdb”.
Execute the following query for adding table and inserting data in the table.
Creating HTML and PHP Script
Step 1
Create and landing page and name it “index.php”
Step 2
Do the following codes for the index page.
Step 3
Add the following extension to access Chart.js
Libraries.
Step 4
Create a php script for fetching the data in the database.
Step 5
Create a JavaScript script for displaying a graph.
Full Source Code
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.
Creating Database
Create a database named “salesdb”.
Execute the following query for adding table and inserting data in the table.
- CREATE
TABLE
`tblsales`
(
- `SalesId`
int
(
11
)
NOT
NULL
,
- `Product`
varchar
(
90
)
NOT
NULL
,
- `TotalSales`
double
NOT
NULL
- )
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
latin1;
- --
- -- Dumping data for table `tblsales`
- --
- INSERT
INTO
`tblsales`
(
`SalesId`
,
`Product`
,
`TotalSales`
)
VALUES
- (
1
,
'Surf Powder'
,
1400
)
,
- (
2
,
'Mr. Clean Powder'
,
800
)
,
- (
3
,
'Tide Powder'
,
5052
)
,
- (
4
,
'Ariel Powder'
,
8030
)
;
- --
- -- Indexes for dumped tables
- --
- --
- -- Indexes for table `tblsales`
- --
- ALTER
TABLE
`tblsales`
- ADD
PRIMARY KEY
(
`SalesId`
)
;
- --
- -- AUTO_INCREMENT for dumped tables
- --
- --
- -- AUTO_INCREMENT for table `tblsales`
- --
- ALTER
TABLE
`tblsales`
- MODIFY
`SalesId`
int
(
11
)
NOT
NULL
AUTO_INCREMENT
,
AUTO_INCREMENT
=
5
;
- COMMIT
;
Creating HTML and PHP Script
Step 1
Create and landing page and name it “index.php”
Step 2
Do the following codes for the index page.
- <!
DOCTYPE html>
- <
html lang=
"en"
>
- <
head>
- <
meta http-
equiv=
"Content-Type"
content=
"text/html; charset=utf-8"
/>
- <
meta name=
"viewport"
content=
"width=device-width, initial-scale=1.0"
>
- <
title>
Graph</
title>
- </
head>
- <
body>
- <
div style=
"width:30%;hieght:20%;text-align:center"
>
- <
h2 class
=
"page-header"
>
Analytics Sales Report </
h2>
- <
div>
Product </
div>
- <
canvas id=
"chartjs_pie"
></
canvas>
- </
div>
- </
body>
- </
html>
Step 3
Add the following extension to access Chart.js
Libraries.
- <
script src=
"//code.jquery.com/jquery-1.9.1.js"
>
</script>
- <
script src=
"//cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"
>
</script>
Step 4
Create a php script for fetching the data in the database.
- <?php
- $con
=
mysqli_connect
(
"localhost"
,
"root"
,
""
,
"salesdb"
)
;
- if
(
!
$con
)
{
- # code...
- echo
"Problem in database connection! Contact administrator!"
.
mysqli_error
(
)
;
- }
else
{
- $sql
=
"SELECT * FROM tblsales"
;
- $result
=
mysqli_query
(
$con
,
$sql
)
;
- $chart_data
=
""
;
- while
(
$row
=
mysqli_fetch_array
(
$result
)
)
{
- $productname
[
]
=
$row
[
'Product'
]
;
- $sales
[
]
=
$row
[
'TotalSales'
]
;
- }
- }
- ?>
Step 5
Create a JavaScript script for displaying a graph.
- <
script type=
"text/javascript"
>
- var
ctx =
document.getElementById
(
"chartjs_pie"
)
.getContext
(
'2d'
)
;
- var
myChart =
new
Chart(
ctx,
{
- type:
'pie'
,
- data:
{
- labels:<?
php echo json_encode(
$productname)
;
?>,
- datasets:
[
{
- backgroundColor:
[
- "#5969ff"
,
- "#ff407b"
,
- "#25d5f2"
,
- "#ffc750"
,
- "#2ec551"
,
- "#7040fa"
,
- "#ff004e"
- ]
,
- data:<?
php echo json_encode(
$sales)
;
?>,
- }
]
- }
,
- options:
{
- legend:
{
- display:
true
,
- position:
'bottom'
,
- labels:
{
- fontColor:
'#71748d'
,
- fontFamily:
'Circular Std Book'
,
- fontSize:
14
,
- }
- }
,
- }
- }
)
;
- </
script>
Full Source Code
- <?php
- $con
=
mysqli_connect
(
"localhost"
,
"root"
,
""
,
"salesdb"
)
;
- if
(
!
$con
)
{
- # code...
- echo
"Problem in database connection! Contact administrator!"
.
mysqli_error
(
)
;
- }
else
{
- $sql
=
"SELECT * FROM tblsales"
;
- $result
=
mysqli_query
(
$con
,
$sql
)
;
- $chart_data
=
""
;
- while
(
$row
=
mysqli_fetch_array
(
$result
)
)
{
- $productname
[
]
=
$row
[
'Product'
]
;
- $sales
[
]
=
$row
[
'TotalSales'
]
;
- }
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Graph</title>
- </head>
- <body>
- <div style="width:30%;hieght:20%;text-align:center">
- <h2 class="page-header" >Analytics Reports </h2>
- <div>Product </div>
- <canvas id="chartjs_pie"></canvas>
- </div>
- </body>
- <script src="//code.jquery.com/jquery-1.9.1.js"></script>
- <script src="//cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>
- <script type="text/javascript">
- var ctx = document.getElementById("chartjs_pie").getContext('2d');
- var myChart = new Chart(ctx, {
- type: 'pie',
- data: {
- labels:<?php
echo
json_encode
(
$productname
)
;
?>
,
- datasets: [{
- backgroundColor: [
- "#5969ff",
- "#ff407b",
- "#25d5f2",
- "#ffc750",
- "#2ec551",
- "#7040fa",
- "#ff004e"
- ],
- data:<?php
echo
json_encode
(
$sales
)
;
?>
,
- }]
- },
- options: {
- legend: {
- display: true,
- position: 'bottom',
- labels: {
- fontColor: '#71748d',
- fontFamily: 'Circular Std Book',
- fontSize: 14,
- }
- },
- }
- });
- </script>
- </html>
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.