• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here:Home » Create Graph From MySQL Data

By Abhishek Ghosh August 19, 2014 8:37 am Updated on October 17, 2014

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 :

 

  1. 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.
  2. 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.
  3. A script which will grab the data, insert data in MySQL database.
  4. A script which will retrieve the data from MySQL database. Usually we prefer to get data as JSON format.
  5. 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 :

Advertisement

---

Vim
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 :

Vim
1
http://dev.mysql.com/doc/refman/5.1/en/create-table.html

Now, we need to insert some data by running this :

Vim
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

 

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 :

Vim
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 :

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 :

Vim
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

Vim
1
$query = "

block to :

Vim
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 :

Vim
1
https://github.com/elliottb/phpgraphlib

Tagged With show mysql data as graph using python , php graph from mysql table , mysql data graph , how to fetch data fron database in php and generate a graph , how to create graph using data from mysql table in angularjs , how to create graph from mysql data , how to create a graphfrom mysql data for a webpage made in html , create graphs mysql data , create graphics of data from mysql , create bar chart from sql in c3
Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to Create Graph From MySQL Data

  • Nginx WordPress Installation Guide (All Steps)

    This is a Full Nginx WordPress Installation Guide With All the Steps, Including Some Optimization and Setup Which is Compatible With WordPress DOT ORG Example Settings For Nginx.

  • Changing Data With cURL for OpenStack Swift (HP Cloud CDN)

    Changing Data With cURL For Object is Quite Easy in OpenStack Swift. Here Are Examples With HP Cloud CDN To Make it Clear. Official Examples Are Bad.

  • OpenShift OctoPress Auto install Script

    OpenShift OctoPress Auto install Script is an Advanced Script to Run OctoPress on Free OpenShift PaaS Practically Without Any Knowing Ruby or Git.

  • How to Install WordPress : Ubuntu 16.04, Nginx, PHP7-FPM

    Here is Step by Step Guide on How to Install WordPress on Ubuntu 16.04, Nginx, PHP7-FPM, memcached & Percona MySQL 5.7 on Cloud Server or VPS.

performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Hybrid Multi-Cloud Environments Are Becoming UbiquitousJuly 12, 2023
  • Data Protection on the InternetJuly 12, 2023
  • Basics of BJT TransistorJuly 11, 2023
  • What is Confidential Computing?July 11, 2023
  • How a MOSFET WorksJuly 10, 2023
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy