Creating Google Charts from MySQL Database

21:08:00 0 Comments


Create Google Charts
If you need to display some sort of statistics on your website, it would be beautiful if you represent it with charts. It may be difficult to create those charts on your own. But, Google provides visualization api that makes it easy to create google charts from MySQL database using php.

Basic Setup to Generate Charts

To create google charts, you need to include Google visualization api in your page and make sure you have connected with your MySQL database like in the simple and basic setup below.
For this tutorial, we are going to show visitors statistics on our page with four kinds of charts such as Column chart, Pie chart, Geo chart and Bar chart.
I assume that you already have the data in your database. But if you don’t know how to get your visitors data, I will post it as a separate tutorial on how to save your visitors geo location data to your mysql database.
For now, let us consider this sample MySQL table entries from which we are going to create google charts.
create-google-charts1

Column Chart

Column chart is helpful if you want to show your stats for a particular time period. So we can use this chart for period wise stats.
Excluding id, we have four columns such as ip, browser, country and vdate. So we can generate date wise chart by grouping mysql select query by vdate.
The above code is the basic syntax for creating a column chart. Look at the values after “var data = google.visualization.arrayToDataTable([” This one has static values. But in order to show our stats, we need to dynamically load these values from our database with php.
The final code will be something like this.

Pie Chart

Pie chart is the most beautiful charts that can be created with google visualization api. You can create Pie charts in situations like representing the values in percentage. So we are going to create a chart that shows browser wise visits from our database. Like we did for column chart above, you have to dynamically load the values to replace the static content from google pie chart’s syntax.

Geo Chart

Geo chart’s main purpose is to show country wise reports with a world map. We have a column called country. So we can create a geo chart to show country wise visits with our data.

Bar Chart

Basically Bar charts and Column charts are same but it can be used for different purposes according to the users. I use bar charts in situations like comparing two or more values for each row.
Since we are generating visitors stats, we can actually determine new visitors and returning visitors by their ip address. So we are going to create a bar chart that shows new and returned visitors for each country.

For more details about style options, configurations and more documentations, visit here https://developers.google.com/chart/interactive/docs/gallery?hl=en
So, creating charts is simple and easy with google visualization api. All you need to do is to load your db values in right place.
Try creating google charts and tell your feedback below. Also share this post, if you find it useful.

0 comments :