How to Create a Line Graph in PHP/MySQLi
Submitted by janobe on Monday, December 24, 2018 - 16:17.
Line graph is one of the most commonly used chart types and is very useful especially in the fields of statistics. It is composed of vertical y-axis and a horizontal x-axis which displays series information of data points connected by lines. Each of this axes is named with a data type, this will help you in monitoring your sales and determine the result accurately. Take a look at the procedure below on how to make Line graph program.
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.- --
- -- Dumping data for table `tblsales`
- --
- (1, '2018-01-30', 'Surf Powder', 1400),
- (2, '2018-02-28', 'Surf Powder', 800),
- (3, '2018-03-31', 'Surf Powder', 5052),
- (4, '2019-04-30', 'Surf Powder', 8030),
- (5, '2019-05-31', 'Surf Powder', 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.- <!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:50%;hieght:20%;text-align:center">
- <h2 class="page-header" >Analytics Sales Report </h2>
- <div><?php echo $productname; ?> </div>
- <canvas id="chartjs_line"></canvas>
- </div>
- </body>
- </html>
Step 3
Add the following extension to accessChart.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
- if (!$con) {
- # code...
- }else{
- $sql ="SELECT * FROM tblsales";
- $chart_data="";
- $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_line").getContext('2d');
- var myChart = new Chart(ctx, {
- type: 'line',
- data: {
- labels:<?php echo json_encode($month); ?>,
- 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
- if (!$con) {
- # code...
- }else{
- $sql ="SELECT * FROM tblsales";
- $chart_data="";
- $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:50%;hieght:20%;text-align:center">
- <h2 class="page-header" >Analytics Sales Report </h2>
- <div><?php echo $productname; ?> </div>
- <canvas id="chartjs_line"></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_line").getContext('2d');
- var myChart = new Chart(ctx, {
- type: 'line',
- data: {
- datasets: [{
- backgroundColor: [
- "#5969ff",
- "#ff407b",
- "#25d5f2",
- "#ffc750",
- "#2ec551",
- "#7040fa",
- "#ff004e"
- ],
- }]
- },
- options: {
- legend: {
- display: true,
- position: 'bottom',
- labels: {
- fontColor: '#71748d',
- fontFamily: 'Circular Std Book',
- fontSize: 14,
- }
- },
- }
- });
- </script>
- </html>
Comments
comment
very good as anotice if you want to not apper any color in dody charts use this code
labels:,
datasets: [{
backgroundColor: [
" #FFFFFF00",
// "blue",
// "yellow",
// "pink",
// "green",
// "silver"
],
borderColor: [
" red",
// "blue",
// "yellow",
// "pink",
// "green",
// "silver"
],
Add new comment
- Add new comment
- 5880 views