Home » how to create bar chart in php and mysql?

how to create bar chart in php and mysql?

Today, We want to share with you how to create bar chart in php and mysql.In this post we will show you how to create pie chart in php using database values, hear for chart.js – getting data from database using mysql and php we will give you demo and example for implement.In this post, we will learn about PHP Laravel Charts and Graphs Data from Database with an example.

Create Dynamic Bar Chart with JavaScript, PHP & MySQL

here in this Example You learn All About Example of bar chart in php mysql source code, how to create dashboard in php mysql with demo and full source code.

Read Also:  Laravel 5.8 Get Client IP Address Example

Step 1: Create Database Table

CREATE TABLE `citizens` (
  `id` int(10) UNSIGNED NOT NULL,
  `continent` varchar(255) NOT NULL,
  `pop_percent` varchar(255) NOT NULL,
  `pop_total` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `citizens` (`id`, `continent`, `pop_percent`, `pop_total`) VALUES
(1, 'Rajkot', '59.69', '4,436,224,110'),
(2, 'Surat', '16.36', '1,216,130,110'),
(3, 'Ahemdabad', '9.94', '738,849,110'),
(4, 'Bhavnagar', '7.79', '579,024,110'),
(5, 'Jammnagar', '7.79', '422,535,110'),
(6, 'Junaghdh', '0.54', '39,901,110'),
(7, 'Morabi', '0.01', '1,106');

Step 2: Include JavaScript Files

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="graphite.js"></script>
<script src="bar_chart.js"></script>

Step 3: Create Bar Chart Container

<div class="container">
	<div class="graph" id="barChart"></div>	
</div>

Step 4: Create Dynamic Bar Chart or Graph

$(document).ready(function() {
	$.ajax({				
		type : 'POST',
		url  : 'chart_data.php',	
		dataType:'json',
		success : function(response){
			var chartData = JSON.parse(response);
			var chartOptions = {
			  'height': 350,
			  'title': 'List of continents by citizens (%)',
			  'width': 1000,
			  'fixPadding': 18,
			  'barFont': [0, 12, "bold"],
			  'labelFont': [0, 13, 0]
			};
			graphite(chartData, chartOptions, barChart);			
		}
	});
});

Step 5: Get Data from MySQL Database Table

<?php
include_once("db_connect.php");
$sqlQuery = "SELECT continent, pop_percent FROM citizens";
$resultset = mysqli_query($conn, $sqlQuery) or die("database error:". mysqli_error($conn));
$citizens = "{";
while( $records = mysqli_fetch_array($resultset) ) {
	$citizens.='"'.$records['continent'].'":'.$records['pop_percent'].',';  		
}
$citizens=rtrim($citizens,",");
$citizens.="}";
$data[] = $citizens;
echo json_encode($data);
exit;
?>

I hope you get an idea about how to create line chart in php and mysql.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  WooCommerce Get Product Tax, Shipping & Stock

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  encapsulation in php
  2. Read Also:  Laravel 6 Delete File from public folder Example
  3. Read Also:  PHP Compare Two Arrays Values
  4. Read Also:  jQuery Ajax POST example with Laravel 7/6
  5. Read Also:  Laravel 5.6 PDF Generation using Dompdf Example
  6. Read Also:  Radio button validation in jquery Example
  7. Read Also:  laravel 6 Retrieve data using jQuery Ajax
  8. Read Also:  delete image from folder in php and MySQL Database
  9. Read Also:  delete image from folder in php and MySQL Database
  10. Read Also:  jQuery Ajax POST example with Laravel 7/6

Leave a Comment

Your email address will not be published. Required fields are marked *