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 :
---
1 2 3 4 5 | CREATE TABLE <code>test_chart_data</code> ( category</code> date NOT NULL, value1</code> int(11) NOT NULL, value2</code> int(11) NOT NULL ); |
Here is reference from official MySQL website :
1 | http://dev.mysql.com/doc/refman/5.1/en/create-table.html |
Now, we need to insert some data by running this :
1 2 3 4 5 6 7 8 9 10 11 12 | INSERT INTO <code>test_chart_data</code> (<code>category</code>, <code>value1</code>, <code>value2</code>) VALUES ('2014-08-19', 17, 27), ('2014-08-18', 41, 36), ('2014-08-17', 51, 55), ('2014-08-16', 43, 91), ('2014-08-15', 55, 30), ('2014-08-14', 49, 31), ('2014-08-13', 39, 71), ('2014-08-12', 76, 49), ('2014-08-11', 16, 36), ('2014-08-10', 31, 47), ('2014-08-09', 43, 75); |
This is a dummy random set of data.
Create Graph From MySQL Data : Example
In real life, the script will insert data for you. If we use PHP, we can access to this MySQL database with this snippet :
1 2 3 4 5 6 7 8 9 | $link = mysql_connect( 'database_host', 'database_user', 'database_password' ); if ( !$link ) { die( 'Could not connect: ' . mysql_error() ); } // define database $db = mysql_select_db( 'test', $link ); if ( !$db ) { die ( 'Error selecting database \'test\' : ' . mysql_error() ); } |
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 :
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 :
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 :
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 :
1 | mysql_close($link); |
So the final script will look like this. This will give table as plain text. If we want to get as JSON, we have to change the
1 | $query = " |
block to :
1 2 3 4 5 6 7 8 9 10 11 | $prefix = ''; echo "[\n"; while ( $row = mysql_fetch_assoc( $result ) ) { echo $prefix . " {\n"; echo ' "category": "' . $row['category'] . '",' . "\n"; echo ' "value1": ' . $row['value1'] . ',' . "\n"; echo ' "value2": ' . $row['value2'] . '' . "\n"; echo " }"; $prefix = ",\n"; } echo "\n]"; |
So our script is becoming like this one.
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 :
1 | https://github.com/elliottb/phpgraphlib |