• We just launched and are currently in beta. Join us as we build and grow the community.

How to Create a Pie Graph Using PHP/MySQLi

337warrior

Tech Trainer
3 Rep
0
0
0
Rep
0
3 Vouches
0
0
0
Vouches
0
Posts
172
Likes
138
Bits
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.
  1. CREATE

    TABLE

    `tblsales`

    (
  2. `SalesId`

    int

    (

    11

    )

    NOT

    NULL

    ,
  3. `Product`

    varchar

    (

    90

    )

    NOT

    NULL

    ,
  4. `TotalSales`

    double

    NOT

    NULL


  5. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

  6. --
  7. -- Dumping data for table `tblsales`
  8. --

  9. INSERT

    INTO

    `tblsales`

    (

    `SalesId`

    ,

    `Product`

    ,

    `TotalSales`

    )

    VALUES


  10. (

    1

    ,

    'Surf Powder'

    ,

    1400

    )

    ,
  11. (

    2

    ,

    'Mr. Clean Powder'

    ,

    800

    )

    ,
  12. (

    3

    ,

    'Tide Powder'

    ,

    5052

    )

    ,
  13. (

    4

    ,

    'Ariel Powder'

    ,

    8030

    )

    ;

  14. --
  15. -- Indexes for dumped tables
  16. --

  17. --
  18. -- Indexes for table `tblsales`
  19. --
  20. ALTER

    TABLE

    `tblsales`
  21. ADD

    PRIMARY KEY

    (

    `SalesId`

    )

    ;

  22. --
  23. -- AUTO_INCREMENT for dumped tables
  24. --

  25. --
  26. -- AUTO_INCREMENT for table `tblsales`
  27. --
  28. ALTER

    TABLE

    `tblsales`
  29. MODIFY

    `SalesId`

    int

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,

    AUTO_INCREMENT

    =

    5

    ;
  30. 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.
  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:30%;hieght:20%;text-align:center"

    >
  10. <

    h2 class

    =

    "page-header"

    >

    Analytics Sales Report </

    h2>
  11. <

    div>

    Product </

    div>
  12. <

    canvas id=

    "chartjs_pie"

    ></

    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>
  2. <

    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.
  1. <?php
  2. $con

    =

    mysqli_connect

    (

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "salesdb"

    )

    ;
  3. if

    (

    !

    $con

    )

    {
  4. # code...
  5. echo

    "Problem in database connection! Contact administrator!"

    .

    mysqli_error

    (

    )

    ;
  6. }

    else

    {
  7. $sql

    =

    "SELECT * FROM tblsales"

    ;
  8. $result

    =

    mysqli_query

    (

    $con

    ,

    $sql

    )

    ;
  9. $chart_data

    =

    ""

    ;
  10. while

    (

    $row

    =

    mysqli_fetch_array

    (

    $result

    )

    )

    {

  11. $productname

    [

    ]

    =

    $row

    [

    'Product'

    ]

    ;
  12. $sales

    [

    ]

    =

    $row

    [

    'TotalSales'

    ]

    ;
  13. }


  14. }


  15. ?>

Step 5

Create a JavaScript script for displaying a graph.
  1. <

    script type=

    "text/javascript"

    >
  2. var

    ctx =

    document.getElementById

    (

    "chartjs_pie"

    )

    .getContext

    (

    '2d'

    )

    ;
  3. var

    myChart =

    new

    Chart(

    ctx,

    {
  4. type:

    'pie'

    ,
  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. <?php
  2. $con

    =

    mysqli_connect

    (

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "salesdb"

    )

    ;
  3. if

    (

    !

    $con

    )

    {
  4. # code...
  5. echo

    "Problem in database connection! Contact administrator!"

    .

    mysqli_error

    (

    )

    ;
  6. }

    else

    {
  7. $sql

    =

    "SELECT * FROM tblsales"

    ;
  8. $result

    =

    mysqli_query

    (

    $con

    ,

    $sql

    )

    ;
  9. $chart_data

    =

    ""

    ;
  10. while

    (

    $row

    =

    mysqli_fetch_array

    (

    $result

    )

    )

    {

  11. $productname

    [

    ]

    =

    $row

    [

    'Product'

    ]

    ;
  12. $sales

    [

    ]

    =

    $row

    [

    'TotalSales'

    ]

    ;
  13. }


  14. }


  15. ?>
  16. <!DOCTYPE html>
  17. <html lang="en">
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  20. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  21. <title>Graph</title>
  22. </head>
  23. <body>
  24. <div style="width:30%;hieght:20%;text-align:center">
  25. <h2 class="page-header" >Analytics Reports </h2>
  26. <div>Product </div>
  27. <canvas id="chartjs_pie"></canvas>
  28. </div>
  29. </body>
  30. <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  31. <script src="//cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>
  32. <script type="text/javascript">
  33. var ctx = document.getElementById("chartjs_pie").getContext('2d');
  34. var myChart = new Chart(ctx, {
  35. type: 'pie',
  36. data: {
  37. labels:<?php

    echo

    json_encode

    (

    $productname

    )

    ;

    ?>

    ,
  38. datasets: [{
  39. backgroundColor: [
  40. "#5969ff",
  41. "#ff407b",
  42. "#25d5f2",
  43. "#ffc750",
  44. "#2ec551",
  45. "#7040fa",
  46. "#ff004e"
  47. ],
  48. data:<?php

    echo

    json_encode

    (

    $sales

    )

    ;

    ?>

    ,
  49. }]
  50. },
  51. options: {
  52. legend: {
  53. display: true,
  54. position: 'bottom',

  55. labels: {
  56. fontColor: '#71748d',
  57. fontFamily: 'Circular Std Book',
  58. fontSize: 14,
  59. }
  60. },


  61. }
  62. });
  63. </script>
  64. </html>

For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.

 

452,292

323,526

323,535

Top