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

Doughnut Chart using Chart.js with PHP/MySQLi

BloodyGraphics

Human-AI Interaction Specialist
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
77
Likes
196
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Getting Started

I've used CDN for Bootstrap and jQuery in this tutorial so, you need internet connection for them to work.

Chart.js used in this tutorial is included in the downloadable file of this tutorial.

Creating our Database

First, we're going to create our database.

1. Open phpMyAdmin.
2. Click databases, create a database and name it as pie.
3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.

  1. CREATE

    TABLE

    `category`

    (
  2. `catid`

    INT

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `catname`

    VARCHAR

    (

    30

    )

    NOT

    NULL

    ,
  4. PRIMARY

    KEY

    (

    `catid`

    )
  5. )

    ENGINE=

    InnoDB DEFAULT

    CHARSET=

    latin1;

  6. CREATE

    TABLE

    `product`

    (
  7. `prodid`

    INT

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  8. `catid`

    INT

    (

    11

    )

    NOT

    NULL

    ,
  9. `prodname`

    VARCHAR

    (

    30

    )

    NOT

    NULL

    ,
  10. PRIMARY

    KEY

    (

    `prodid`

    )
  11. )

    ENGINE=

    InnoDB DEFAULT

    CHARSET=

    latin1;

database_6_39.png

Inserting Data into our Database

Next, we insert sample data to our database to be used in our chart.

1. Click pie database that we have created earlier.
2. Click SQL and paste the following codes.

  1. INSERT

    INTO

    `category`

    (

    `catid`

    ,

    `catname`

    )

    VALUES
  2. (

    1

    ,

    'Samsung'

    )

    ,
  3. (

    2

    ,

    'Apple'

    )

    ,
  4. (

    3

    ,

    'Vivo'

    )

    ,
  5. (

    4

    ,

    'Sony'

    )

    ,
  6. (

    5

    ,

    'Nokia'

    )

    ;

  7. INSERT

    INTO

    `product`

    (

    `prodid`

    ,

    `catid`

    ,

    `prodname`

    )

    VALUES
  8. (

    2

    ,

    1

    ,

    'Galaxy Note FE '

    )

    ,
  9. (

    3

    ,

    1

    ,

    'Galaxy J7 +'

    )

    ,
  10. (

    4

    ,

    1

    ,

    'Galaxy J7 Core'

    )

    ,
  11. (

    5

    ,

    1

    ,

    'Galaxy Note 8'

    )

    ,
  12. (

    6

    ,

    1

    ,

    'Galaxy J5 Prime'

    )

    ,
  13. (

    7

    ,

    2

    ,

    'iPhone 7'

    )

    ,
  14. (

    8

    ,

    2

    ,

    'iPhone 7 +'

    )

    ,
  15. (

    9

    ,

    2

    ,

    'iPhone SE'

    )

    ,
  16. (

    10

    ,

    2

    ,

    'iPhone 6S'

    )

    ,
  17. (

    11

    ,

    3

    ,

    'Vivo Y53'

    )

    ,
  18. (

    12

    ,

    3

    ,

    'Vivo V7+'

    )

    ,
  19. (

    13

    ,

    3

    ,

    'Vivo V5'

    )

    ,
  20. (

    14

    ,

    3

    ,

    'Vivo Y55'

    )

    ,
  21. (

    15

    ,

    4

    ,

    'Sony Xperia XZ Premium'

    )

    ,
  22. (

    16

    ,

    4

    ,

    'Sony Xperia XZ1'

    )

    ,
  23. (

    17

    ,

    4

    ,

    'Sony Xperia XZ'

    )

    ,
  24. (

    18

    ,

    4

    ,

    'Sony Xperia XZs'

    )

    ,
  25. (

    19

    ,

    4

    ,

    'Sony Xperia Z5 Dual'

    )

    ,
  26. (

    20

    ,

    4

    ,

    'Sony Xperia Z5 Premium Dual'

    )

    ,
  27. (

    21

    ,

    4

    ,

    'Sony Xperia C3'

    )

    ,
  28. (

    22

    ,

    4

    ,

    'Sony Xperia C5 Ultra Dual'

    )

    ,
  29. (

    23

    ,

    5

    ,

    'Nokia 5'

    )

    ,
  30. (

    24

    ,

    5

    ,

    'Nokia 6'

    )

    ,
  31. (

    25

    ,

    5

    ,

    'Nokia Lumia 530'

    )

    ,
  32. (

    26

    ,

    5

    ,

    'Nokia Lumia 950'

    )

    ,
  33. (

    27

    ,

    4

    ,

    'Sony Xperia XA1'

    )

    ;

3. Click Go button below.

index.php

This is our index which contains our simple add form and our statistical representation of data from database. This also contains our doughnut chart script.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Doughnut Chart using Chart.js with PHP/MySQLi</title>
  5. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
  6. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  7. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

  8. <!-- ChartJS -->
  9. <script src="chart.js/Chart.js"></script>
  10. </head>
  11. <body>
  12. <div class="container">
  13. <h1 class="page-header text-center">Doughnut Chart using Chart.js with PHP/MySQLi</h1>
  14. <div class="row">
  15. <div class="col-md-3">
  16. <h3 class="page-header text-center">Add Product</h3>
  17. <form method="POST" action="addproduct.php">
  18. <div class="form-group">
  19. <label>Category:</label>
  20. <select class="form-control" name="category">
  21. <?php
  22. //connection
  23. $conn

    =

    new

    mysqli(

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "pie"

    )

    ;
  24. if

    (

    $conn

    ->

    connect_error

    )

    {
  25. die

    (

    "Connection failed: "

    .

    $conn

    ->

    connect_error

    )

    ;
  26. }

  27. $sql

    =

    "select * from category"

    ;
  28. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;

  29. while

    (

    $row

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  30. ?>
  31. <option value="<?php

    echo

    $row

    [

    'catid'

    ]

    ;

    ?>

    "><?php

    echo

    $row

    [

    'catname'

    ]

    ;

    ?>

    </option>
  32. <?php
  33. }
  34. ?>
  35. </select>
  36. </div>
  37. <div class="form-group">
  38. <label>Product Name:</label>
  39. <input type="text" class="form-control" name="pname">
  40. </div>
  41. <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-disk"></span> Save</button>
  42. </form>
  43. </div>
  44. <div class="col-md-9">
  45. <div class="box box-success">
  46. <div class="box-header with-border">
  47. <?php
  48. //set timezone
  49. //date_default_timezone_set('Asia/Manila');
  50. $year

    =

    date

    (

    'Y'

    )

    ;
  51. ?>
  52. <h3 class="box-title">Phone Brands</h3>

  53. </div>
  54. <div class="box-body">
  55. <canvas id="pieChart" style="height:250px"></canvas>
  56. </div>
  57. <!-- /.box-body -->
  58. </div>
  59. </div>
  60. </div>
  61. </div>
  62. <?php

    include

    (

    'data.php'

    )

    ;

    ?>
  63. <script>
  64. $(function () {
  65. var pieChartCanvas = $('#pieChart').get(0).getContext('2d')
  66. var pieChart = new Chart(pieChartCanvas)
  67. var PieData = [
  68. {
  69. value : <?php

    echo

    $samsung

    ;

    ?>

    ,
  70. color : '#f56954',
  71. highlight: '#f56954',
  72. label : 'Samsung'
  73. },
  74. {
  75. value : <?php

    echo

    $apple

    ;

    ?>

    ,
  76. color : '#00a65a',
  77. highlight: '#00a65a',
  78. label : 'Apple'
  79. },
  80. {
  81. value : <?php

    echo

    $vivo

    ;

    ?>

    ,
  82. color : '#f39c12',
  83. highlight: '#f39c12',
  84. label : 'Vivo'
  85. },
  86. {
  87. value : <?php

    echo

    $sony

    ;

    ?>

    ,
  88. color : '#00c0ef',
  89. highlight: '#00c0ef',
  90. label : 'Sony'
  91. },
  92. {
  93. value : <?php

    echo

    $nokia

    ;

    ?>

    ,
  94. color : '#3c8dbc',
  95. highlight: '#3c8dbc',
  96. label : 'Nokia'
  97. }
  98. ]
  99. var pieOptions = {
  100. //Boolean - Whether we should show a stroke on each segment
  101. segmentShowStroke : true,
  102. //String - The colour of each segment stroke
  103. segmentStrokeColor : '#fff',
  104. //Number - The width of each segment stroke
  105. segmentStrokeWidth : 2,
  106. //Number - The percentage of the chart that we cut out of the middle
  107. percentageInnerCutout: 50, // This is 0 for Pie charts
  108. //Number - Amount of animation steps
  109. animationSteps : 100,
  110. //String - Animation easing effect
  111. animationEasing : 'easeOutBounce',
  112. //Boolean - Whether we animate the rotation of the Doughnut
  113. animateRotate : true,
  114. //Boolean - Whether we animate scaling the Doughnut from the centre
  115. animateScale : false,
  116. //Boolean - whether to make the chart responsive to window resizing
  117. responsive : true,
  118. // Boolean - whether to maintain the starting aspect ratio or not when responsive, if set to false, will take up entire container
  119. maintainAspectRatio : true,
  120. //String - A legend template
  121. legendTemplate : '<ul class="<%=

    name.

    toLowerCase(

    )

    %>

    -legend"><%

    for

    (

    var

    i=

    0

    ;

    i<

    segments.

    length;

    i++

    )

    {

    %>

    <li><span style="background-color:<%=

    segments[

    i]

    .

    fillColor%>

    "></span><%

    if

    (

    segments[

    i]

    .

    label)

    {

    %>

    <%=

    segments[

    i]

    .

    label%>

    <%

    }

    %>

    </li><%

    }

    %>

    </ul>'
  122. }
  123. //Create pie or douhnut chart
  124. // You can switch between pie and douhnut using the method below.
  125. pieChart.Doughnut(PieData, pieOptions)

  126. })
  127. </script>
  128. </body>
  129. </html>

addproduct.php

This is our PHP code in adding data into our database.

  1. <?php
  2. $conn

    =

    new

    mysqli(

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "pie"

    )

    ;

  3. if

    (

    $conn

    ->

    connect_error

    )

    {
  4. die

    (

    "Connection failed: "

    .

    $conn

    ->

    connect_error

    )

    ;
  5. }

  6. $category

    =

    $_POST

    [

    'category'

    ]

    ;
  7. $pname

    =

    $_POST

    [

    'pname'

    ]

    ;

  8. $sql

    =

    "insert into product (catid, prodname) values ('$category

    ', '$pname

    ')"

    ;
  9. $conn

    ->

    query

    (

    $sql

    )

    ;

  10. header

    (

    'location:index.php'

    )

    ;
  11. ?>

data.php

Lastly, this is our PHP code that contains our data that we're gonna be using in our chart.js to make statistical data in the form of doughnut chart.

  1. <?php
  2. $conn

    =

    new

    mysqli(

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "pie"

    )

    ;

  3. if

    (

    $conn

    ->

    connect_error

    )

    {
  4. die

    (

    "Connection failed: "

    .

    $conn

    ->

    connect_error

    )

    ;
  5. }

  6. //$cat = array();

  7. //for Samsung
  8. $sql

    =

    "select * from product left join category on category.catid=product.catid where product.catid='1'"

    ;
  9. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  10. $samsung

    =

    $query

    ->

    num_rows

    ;

  11. //for Apple
  12. $sql

    =

    "select * from product left join category on category.catid=product.catid where product.catid='2'"

    ;
  13. $aquery

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  14. $apple

    =

    $aquery

    ->

    num_rows

    ;

  15. //for Vivo
  16. $sql

    =

    "select * from product left join category on category.catid=product.catid where product.catid='3'"

    ;
  17. $vquery

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  18. $vivo

    =

    $vquery

    ->

    num_rows

    ;

  19. //for Sony
  20. $sql

    =

    "select * from product left join category on category.catid=product.catid where product.catid='4'"

    ;
  21. $squery

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  22. $sony

    =

    $squery

    ->

    num_rows

    ;

  23. //for Nokia
  24. $sql

    =

    "select * from product left join category on category.catid=product.catid where product.catid='5'"

    ;
  25. $nquery

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  26. $nokia

    =

    $nquery

    ->

    num_rows

    ;

  27. ?>
  28. <php>

  29. That ends this tutorial. Happy Coding :)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top