• 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

This Article Has Been Shared 945 Times!

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

  • Remodeling in the Data Center and Integrated Systems

    Remodeling in the Data Center is the result from pressure from all sides and Integrated Systems are booming to seek low-cost, higher performance, easier model.

  • OpenShift SSH : Video Guide

    OpenShift SSH is not very difficult to use by an user used with Git and SSH/Telnet through Command Line. Here is a Video Guide keeping the new users in mind.

  • IBM Cloud : Why it is a Complete Failure

    IBM Cloud is possibly an example of level of failure from the point of outages. Its not only Cloud but IBM is failing company due to strategies which are wrong.

  • Application Service Provider and Cloud Computing

    Application Service Provider and Cloud Computing are sometimes made equal by the business owners either knowingly or unknowingly by the third parties.

  • Key Issues for Data Portability

    Key issues for data portability are Contractual arrangements, Access limits and speed restrictions, Availability of suitable interfaces and other points.

Additionally, 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

  • Cyberpunk Aesthetics: What’s in it Special January 27, 2023
  • How to Do Electrical Layout Plan for Adding Smart Switches January 26, 2023
  • What is a Data Mesh? January 25, 2023
  • What is Vehicular Ad-Hoc Network? January 24, 2023
  • Difference Between Panel Light, COB Light, Track Light January 21, 2023

About This Article

Cite this article as: Abhishek Ghosh, "Create Graph From MySQL Data," in The Customize Windows, August 19, 2014, January 29, 2023, https://thecustomizewindows.com/2014/08/create-graph-mysql-data/.

Source:The Customize Windows, JiMA.in

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

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT