Create Graph From MySQL Data

Advertisement

It is quite practical thought to create graph from MySQL Data – the need can be for a standalone Cloud App or server stat on WordPress dashboard. We have talked about really lot of different libraries to create various types of charts and graphs – c3 Chart Library, Peity, Epoch. Everything basically depends on situation and need. We are using MySQL Database to store the data. So, it is better to have a separate Database if the thing is going to run inside another App / Web App / Cloud App – example can be WordPress Dashboard.

Create Graph From MySQL Data : What Are Needed?

Most existing WordPress Plugins grab the data via API based access to a Third Party Service, like Google Analytics. In the same way, it is possible to generate graph on WordPress dashboard using self hosted Piwik. Few things are needed to know and can be listed in this way :

A powerful, well documented programming language to write the scripts – it is somewhat dependent on the need, it can be Perl, Python, Ruby or PHP for the output. Obviously you’ll not manually run
INSERT INTO<code>chart_data</code>(<code>category</code>,<code>value1</code>,<code>value2</code>)VALUES manually by logging into mysql database.

Data collection and processing – depends on your need. Quite obviously, in most cases; unlike the provided examples by the Chart and Graph Libraries, we need a dynamic set of data.

A script which will grab the data, insert data in MySQL database.

A script which will retrieve the data from MySQL database. Usually we prefer to get data as JSON format.

Last one is a script which will call the required plugins to show things in a visual way.

Create a new database with the name test on your MySQL Server, grant all privileges to the user, add password based access. Otherwise people might get fun to make your MySQL database working. So, in real life while working with a database, you will need a thing like
wp-config.php and call the second php file. If you are working within WordPress, you will omit this step, instead make the thing running under WordPress Administration privilege. That is basically a WordPress Plugin. If you manually creating, then go ahead and run this :

Add PHP opening at the beginning. If we add PHP opening and closing here, WordPress will throw error! So we have connected. You can name this file as login.php and call from the second file or continue as one script. We can fetch data by :

Vim

1

2

3

4

5

$query= "

SELECT*

FROM test_chart_data

ORDER BY category ASC";

$result=mysql_query($query);

You can add a system to throw error message :

Vim

1

2

3

4

5

if(!$result){

$message='Invalid query: '.mysql_error()."\n";

$message.='Whole query: '.$query;

die($message);

}

This will print the data set if we run the script on browser :

Vim

1

2

3

while($row=mysql_fetch_assoc($result)){

echo$row['category'].' | '.$row['value1'].' | '.$row['value2']."\n";

}

Finally, at the end of the script, before the PHP closure, we have to close MySQL connection :

Now you can pass the data to any supporting chart creating library, usually Javascript will render the data to visible real graph. There is PHP based library, so that you need not to work from scratch :