LIVE DEMODOWNLOAD

A chart is a graphical representation of data in which “the data is represented by symbols, such as bars in a bar chart, lines in a line chart, or slices in a pie chart.” Putting up a chart or graph in your report or presentation will make sense of what you are saying. It helps user to understand data quickly.

In this tutorial we will see how to use google chart with PHP and MySQLi. Google chart library works only with data in JSON format. You can convert your database data into JSON format using PHP and then display data result into any type of chart, you can use Google Chart library that provides very powerful chart making tool that you can use for making chart very simple.

Google chart library is free and very easy to use. It allows you to customize your charts according to your need like increasing size of graphs and change of color and make your application more interactive.

LET’S DO IT!

Create table in MySQLi Database

Insert Data into table

Database Connection

PHP Code to retrieve data from Database

The above code will retrieve date in the following format.

Now let’s change this result to be shown as a chart. For that we just need to add jQuery and Google Chart libraries in our code as below:

After adding the above code in your <head> add the below code also to make a chart:

The End!

Wait a minute, I just forget to tell you there is one line of HTML code also in our script :-D. Just add below line of code in your <body> tag and that’s it.

Now your chart will look like:

Column Chart
Column Chart

There are other types of charts also available in Google Chart. You just need to change the following line of code:

You can change ColumnChart() to PieChart() or BarChart(). For more Charts style you can visit Google Chart Page.

Pie Chart

Pie Chart

Bar Chart

Bar Chart
Bar Chart

LIVE DEMODOWNLOAD

Leave a Reply