Facebook



How to Create a Doughnut Graph Using PHP/MySQLi

If you want to be creative in creating statistics sales report you can try this simple program that I’m going to share to you. This time, I will teach you how to create your own custom doughnut graph using Chart.js and PHP/MySQLi. This kind of graph demonstrates the products sale in different slices depending on the sales of the products. It also automatically set a colour to easily identify each products. Let’s begin.

Creating Database

Create a database named “salesdb”.
Execute the following query for adding table and inserting data in the table.
  1. CREATE TABLE `tblsales` (
  2. `SalesId` int(11) NOT NULL,
  3. `Product` varchar(90) NOT NULL,
  4. `TotalSales` double NOT NULL
  5. --
  6. -- Dumping data for table `tblsales`
  7. --
  8. INSERT INTO `tblsales` (`SalesId`, `Product`, `TotalSales`) VALUES
  9. (1,'Cellphone', 1400),
  10. (2,'Laptop', 800),
  11. (3,'Desktop', 5052),
  12. (4,'Ipod', 8030),
  13. (5,'Tablet', 10000);

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.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Graph</title>
  7. </head>
  8. <body>
  9. <div style="width:40%;hieght:20%;text-align:center">
  10. <h2 class="page-header" >Analytics Sales Report </h2>
  11. <div>Gadgets</div>
  12. <canvas id="chartjs_doughnut"></canvas>
  13. </div>
  14. </body>
  15. </html>

Step 3

Add the following extension to access Chart.js Libraries.
  1. <script src="//code.jquery.com/jquery-1.9.1.js">

  • <script src="//cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js">

  • Step 4

    Create a PHP script for fetching the data in the database.
    1. $con = mysqli_connect("localhost","root","","salesdb");
    2. if (!$con) {
    3. # code...
    4. echo "Problem in database connection! Contact administrator!" . mysqli_error();
    5. }else{
    6. $sql ="SELECT * FROM tblsales";
    7. $result = mysqli_query($con,$sql);
    8. $chart_data="";
    9. while ($row = mysqli_fetch_array($result)) {
    10. $productname[]=$row['Product'];
    11. $sales[] = $row['TotalSales'];
    12. }
    13. }
    14. ?>

    Step 5

    Create a JavaScript script for displaying a graph.
    1. <script type="text/javascript">
    2. var ctx = document.getElementById("chartjs_doughnut").getContext('2d');
    3. var myChart = new Chart(ctx, {
    4. type: 'doughnut',
    5. data: {
    6. labels:php echo json_encode($productname); ?>,
    7. datasets: [{
    8. backgroundColor: [
    9. "#5969ff",
    10. "#ff407b",
    11. "#25d5f2",
    12. "#ffc750",
    13. "#2ec551",
    14. "#7040fa",
    15. "#ff004e"
    16. ],
    17. data:php echo json_encode($sales); ?>,
    18. }]
    19. },
    20. options: {
    21. legend: {
    22. display: true,
    23. position: 'bottom',
    24. labels: {
    25. fontColor: '#71748d',
    26. fontFamily: 'Circular Std Book',
    27. fontSize: 14,
    28. }
    29. },
    30. }
    31. });
    32. </script>

    Full Source Code

    1. $con = mysqli_connect("localhost","root","","salesdb");
    2. if (!$con) {
    3. # code...
    4. echo "Problem in database connection! Contact administrator!" . mysqli_error();
    5. }else{
    6. $sql ="SELECT * FROM tblsales";
    7. $result = mysqli_query($con,$sql);
    8. $chart_data="";
    9. while ($row = mysqli_fetch_array($result)) {
    10. $productname[]=$row['Product'];
    11. $sales[] = $row['TotalSales'];
    12. }
    13. }
    14. ?>
    15. Graph

  • labels: echo json_encode($productname); ?>,
  • data: echo json_encode($sales); ?>,
  • For any questions about this article. You can contact me
    Previous Post Next Post