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

Line Chart using Chart.js with PHP/MySQLi

mr5p3rf3ct

Privilege Access Management Tester
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
137
Likes
144
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 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 chartjs.
3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.

  1. CREATE

    TABLE

    `sales`

    (
  2. `salesid`

    INT

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `amount`

    DOUBLE

    NOT

    NULL

    ,
  4. `sales_date`

    DATE

    NOT

    NULL

    ,
  5. PRIMARY

    KEY

    (

    `salesid`

    )
  6. )

    ENGINE=

    InnoDB DEFAULT

    CHARSET=

    latin1;

database_6_37.png

Inserting Data into our Database

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

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

  1. INSERT

    INTO

    `sales`

    (

    `salesid`

    ,

    `amount`

    ,

    `sales_date`

    )

    VALUES
  2. (

    8

    ,

    100

    ,

    '2017-01-01'

    )

    ,
  3. (

    9

    ,

    55

    ,

    '2016-01-01'

    )

    ,
  4. (

    10

    ,

    200

    ,

    '2017-02-02'

    )

    ,
  5. (

    11

    ,

    55

    ,

    '2016-02-02'

    )

    ,
  6. (

    12

    ,

    175

    ,

    '2017-03-03'

    )

    ,
  7. (

    13

    ,

    150

    ,

    '2016-03-03'

    )

    ,
  8. (

    14

    ,

    150

    ,

    '2017-04-04'

    )

    ,
  9. (

    15

    ,

    85

    ,

    '2016-04-04'

    )

    ,
  10. (

    16

    ,

    99

    ,

    '2017-04-04'

    )

    ,
  11. (

    17

    ,

    20

    ,

    '2016-04-04'

    )

    ,
  12. (

    18

    ,

    180

    ,

    '2017-05-05'

    )

    ,
  13. (

    19

    ,

    70

    ,

    '2016-05-05'

    )

    ,
  14. (

    20

    ,

    225

    ,

    '2016-06-06'

    )

    ,
  15. (

    21

    ,

    150

    ,

    '2017-06-06'

    )

    ,
  16. (

    22

    ,

    120

    ,

    '2017-07-07'

    )

    ,
  17. (

    23

    ,

    55

    ,

    '2016-07-07'

    )

    ,
  18. (

    24

    ,

    199

    ,

    '2017-08-08'

    )

    ,
  19. (

    25

    ,

    45

    ,

    '2016-08-08'

    )

    ,
  20. (

    26

    ,

    130

    ,

    '2017-09-09'

    )

    ,
  21. (

    27

    ,

    75

    ,

    '2016-09-09'

    )

    ,
  22. (

    28

    ,

    300

    ,

    '2017-10-10'

    )

    ,
  23. (

    29

    ,

    35

    ,

    '2016-10-10'

    )

    ,
  24. (

    30

    ,

    250

    ,

    '2017-11-11'

    )

    ,
  25. (

    31

    ,

    20

    ,

    '2016-11-11'

    )

    ,
  26. (

    32

    ,

    220

    ,

    '2017-12-12'

    )

    ,
  27. (

    33

    ,

    200

    ,

    '2016-12-12'

    )

    ,
  28. (

    34

    ,

    45

    ,

    '2016-01-05'

    )

    ;

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 line chart script.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Line 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">Line 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 Sales</h3>
  17. <form method="POST" action="addsales.php">
  18. <div class="form-group">
  19. <label>Amount:</label>
  20. <input type="text" class="form-control" name="amount" required>
  21. </div>
  22. <div class="form-group">
  23. <label>Date:</label>
  24. <input type="date" class="form-control" name="sales_date" required>
  25. </div>
  26. <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-disk"></span> Save</button>
  27. </form>
  28. </div>
  29. <div class="col-md-9">
  30. <div class="box box-success">
  31. <div class="box-header with-border">
  32. <?php
  33. //set timezone
  34. //date_default_timezone_set('Asia/Manila');
  35. $year

    =

    date

    (

    'Y'

    )

    ;
  36. ?>
  37. <h3 class="box-title">Sales Report (<?php

    echo

    $year

    -

    1

    ;

    ?>

    vs <?php

    echo

    $year

    ;

    ?>

    )</h3>

  38. </div>
  39. <div class="box-body">
  40. <div class="chart">
  41. <canvas id="lineChart" style="height:250px"></canvas>
  42. </div>
  43. </div>
  44. <!-- /.box-body -->
  45. </div>
  46. </div>
  47. </div>
  48. </div>
  49. <?php

    include

    (

    'data.php'

    )

    ;

    ?>
  50. <script>
  51. $(function () {
  52. var lineChartData = {
  53. labels : ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
  54. datasets: [
  55. {
  56. label : 'Previous Year',
  57. fillColor : 'rgba(210, 214, 222, 1)',
  58. strokeColor : 'rgba(210, 214, 222, 1)',
  59. pointColor : 'rgba(210, 214, 222, 1)',
  60. pointStrokeColor : '#c1c7d1',
  61. pointHighlightFill : '#fff',
  62. pointHighlightStroke: 'rgba(220,220,220,1)',
  63. data : [ "<?php

    echo

    $pjan

    ;

    ?>

    ",
  64. "<?php

    echo

    $pfeb

    ;

    ?>

    ",
  65. "<?php

    echo

    $pmar

    ;

    ?>

    ",
  66. "<?php

    echo

    $papr

    ;

    ?>

    ",
  67. "<?php

    echo

    $pmay

    ;

    ?>

    ",
  68. "<?php

    echo

    $pjun

    ;

    ?>

    ",
  69. "<?php

    echo

    $pjul

    ;

    ?>

    ",
  70. "<?php

    echo

    $paug

    ;

    ?>

    ",
  71. "<?php

    echo

    $psep

    ;

    ?>

    ",
  72. "<?php

    echo

    $poct

    ;

    ?>

    ",
  73. "<?php

    echo

    $pnov

    ;

    ?>

    ",
  74. "<?php

    echo

    $pdec

    ;

    ?>

    "
  75. ]
  76. },
  77. {
  78. label : 'This Year',
  79. fillColor : 'rgba(60,141,188,0.9)',
  80. strokeColor : 'rgba(60,141,188,0.8)',
  81. pointColor : '#3b8bba',
  82. pointStrokeColor : 'rgba(60,141,188,1)',
  83. pointHighlightFill : '#fff',
  84. pointHighlightStroke: 'rgba(60,141,188,1)',
  85. data : [ "<?php

    echo

    $tjan

    ;

    ?>

    ",
  86. "<?php

    echo

    $tfeb

    ;

    ?>

    ",
  87. "<?php

    echo

    $tmar

    ;

    ?>

    ",
  88. "<?php

    echo

    $tapr

    ;

    ?>

    ",
  89. "<?php

    echo

    $tmay

    ;

    ?>

    ",
  90. "<?php

    echo

    $tjun

    ;

    ?>

    ",
  91. "<?php

    echo

    $tjul

    ;

    ?>

    ",
  92. "<?php

    echo

    $taug

    ;

    ?>

    ",
  93. "<?php

    echo

    $tsep

    ;

    ?>

    ",
  94. "<?php

    echo

    $toct

    ;

    ?>

    ",
  95. "<?php

    echo

    $tnov

    ;

    ?>

    ",
  96. "<?php

    echo

    $tdec

    ;

    ?>

    "
  97. ]
  98. }
  99. ]
  100. }

  101. var lineChartCanvas = $('#lineChart').get(0).getContext('2d')
  102. var lineChart = new Chart(lineChartCanvas)
  103. var lineChartOptions = {
  104. //Boolean - If we should show the scale at all
  105. showScale : true,
  106. //Boolean - Whether grid lines are shown across the chart
  107. scaleShowGridLines : false,
  108. //String - Colour of the grid lines
  109. scaleGridLineColor : 'rgba(0,0,0,.05)',
  110. //Number - Width of the grid lines
  111. scaleGridLineWidth : 1,
  112. //Boolean - Whether to show horizontal lines (except X axis)
  113. scaleShowHorizontalLines: true,
  114. //Boolean - Whether to show vertical lines (except Y axis)
  115. scaleShowVerticalLines : true,
  116. //Boolean - Whether the line is curved between points
  117. bezierCurve : true,
  118. //Number - Tension of the bezier curve between points
  119. bezierCurveTension : 0.3,
  120. //Boolean - Whether to show a dot for each point
  121. pointDot : false,
  122. //Number - Radius of each point dot in pixels
  123. pointDotRadius : 4,
  124. //Number - Pixel width of point dot stroke
  125. pointDotStrokeWidth : 1,
  126. //Number - amount extra to add to the radius to cater for hit detection outside the drawn point
  127. pointHitDetectionRadius : 20,
  128. //Boolean - Whether to show a stroke for datasets
  129. datasetStroke : true,
  130. //Number - Pixel width of dataset stroke
  131. datasetStrokeWidth : 2,
  132. //Boolean - Whether to fill the dataset with a color
  133. datasetFill : true,
  134. //String - A legend template
  135. legendTemplate : '<ul class="<%=

    name.

    toLowerCase(

    )

    %>

    -legend"><%

    for

    (

    var

    i=

    0

    ;

    i<

    datasets.

    length;

    i++

    )

    {

    %>

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

    datasets[

    i]

    .

    lineColor%>

    "></span><%

    if

    (

    datasets[

    i]

    .

    label)

    {

    %>

    <%=

    datasets[

    i]

    .

    label%>

    <%

    }

    %>

    </li><%

    }

    %>

    </ul>',
  136. //Boolean - whether to maintain the starting aspect ratio or not when responsive, if set to false, will take up entire container
  137. maintainAspectRatio : true,
  138. //Boolean - whether to make the chart responsive to window resizing
  139. responsive : true
  140. }

  141. lineChartOptions.datasetFill = false
  142. lineChart.Line(lineChartData, lineChartOptions)

  143. })
  144. </script>
  145. </body>
  146. </html>

addsales.php

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

  1. <?php
  2. $conn

    =

    new

    mysqli(

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "chartjs"

    )

    ;

  3. if

    (

    $conn

    ->

    connect_error

    )

    {
  4. die

    (

    "Connection failed: "

    .

    $conn

    ->

    connect_error

    )

    ;
  5. }

  6. $amount

    =

    $_POST

    [

    'amount'

    ]

    ;
  7. $sales_date

    =

    $_POST

    [

    'sales_date'

    ]

    ;

  8. $sql

    =

    "insert into sales (amount, sales_date) values ('$amount

    ', '$sales_date

    ')"

    ;
  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 line chart.

  1. <?php
  2. $conn

    =

    new

    mysqli(

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "chartjs"

    )

    ;

  3. if

    (

    $conn

    ->

    connect_error

    )

    {
  4. die

    (

    "Connection failed: "

    .

    $conn

    ->

    connect_error

    )

    ;
  5. }

  6. //set timezone
  7. //date_default_timezone_set('Asia/Manila');
  8. $year

    =

    date

    (

    'Y'

    )

    ;
  9. $total

    =

    array

    (

    )

    ;
  10. for

    (

    $month

    =

    1

    ;

    $month

    <=

    12

    ;

    $month

    ++

    )

    {
  11. $sql

    =

    "select *, sum(amount) as total from sales where month(sales_date)='$month

    ' and year(sales_date)='$year

    '"

    ;
  12. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  13. $row

    =

    $query

    ->

    fetch_array

    (

    )

    ;

  14. $total

    [

    ]

    =

    $row

    [

    'total'

    ]

    ;
  15. }

  16. $tjan

    =

    $total

    [

    0

    ]

    ;
  17. $tfeb

    =

    $total

    [

    1

    ]

    ;
  18. $tmar

    =

    $total

    [

    2

    ]

    ;
  19. $tapr

    =

    $total

    [

    3

    ]

    ;
  20. $tmay

    =

    $total

    [

    4

    ]

    ;
  21. $tjun

    =

    $total

    [

    5

    ]

    ;
  22. $tjul

    =

    $total

    [

    6

    ]

    ;
  23. $taug

    =

    $total

    [

    7

    ]

    ;
  24. $tsep

    =

    $total

    [

    8

    ]

    ;
  25. $toct

    =

    $total

    [

    9

    ]

    ;
  26. $tnov

    =

    $total

    [

    10

    ]

    ;
  27. $tdec

    =

    $total

    [

    11

    ]

    ;

  28. $pyear

    =

    $year

    -

    1

    ;
  29. $pnum

    =

    array

    (

    )

    ;

  30. for

    (

    $pmonth

    =

    1

    ;

    $pmonth

    <=

    12

    ;

    $pmonth

    ++

    )

    {
  31. $sql

    =

    "select *, sum(amount) as ptotal from sales where month(sales_date)='$pmonth

    ' and year(sales_date)='$pyear

    '"

    ;
  32. $pquery

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  33. $prow

    =

    $pquery

    ->

    fetch_array

    (

    )

    ;

  34. $ptotal

    [

    ]

    =

    $prow

    [

    'ptotal'

    ]

    ;
  35. }

  36. $pjan

    =

    $ptotal

    [

    0

    ]

    ;
  37. $pfeb

    =

    $ptotal

    [

    1

    ]

    ;
  38. $pmar

    =

    $ptotal

    [

    2

    ]

    ;
  39. $papr

    =

    $ptotal

    [

    3

    ]

    ;
  40. $pmay

    =

    $ptotal

    [

    4

    ]

    ;
  41. $pjun

    =

    $ptotal

    [

    5

    ]

    ;
  42. $pjul

    =

    $ptotal

    [

    6

    ]

    ;
  43. $paug

    =

    $ptotal

    [

    7

    ]

    ;
  44. $psep

    =

    $ptotal

    [

    8

    ]

    ;
  45. $poct

    =

    $ptotal

    [

    9

    ]

    ;
  46. $pnov

    =

    $ptotal

    [

    10

    ]

    ;
  47. $pdec

    =

    $ptotal

    [

    11

    ]

    ;
  48. ?>

That ends this tutorial. Happy Coding :)


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

452,292

323,526

323,535

Top