473,320 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,320 developers and data experts.

Creating a simple Bar Chart with SQL Query

Introudction

A lot of PHP based libraries are currently in market that support chart types. The important aspect is how they are connecting with our data source as mostly they are used with some database (e.g. mysql or sqlite).

Getting Chart data from Database

For this article, i am fetching data from northwind sqlite database. Lets say we like to display bar chart of product category and overall sales. The query would be like:

Expand|Select|Wrap|Line Numbers
  1. select c.categoryname, sum(a.quantity) as Sales
  2.                 from products b, `order details` a, categories c
  3.                 where a.productid = b.productid and c.categoryid = b.categoryid
  4.                 group by c.categoryid
  5.                 order by c.categoryid
  6.  
This would result in following data:
Expand|Select|Wrap|Line Numbers
  1. "Beverages"        "9532"
  2. "Condiments"        "5298"
  3. "Confections"        "7906"
  4. "Dairy Products"    "9149"
  5. "Grains/Cereals"    "4562"
  6. "Meat/Poultry"        "4199"
  7. "Produce"        "2990"
  8. "Seafood"        "7681"
  9.  

Chart Code

I will be using PHP Charts Framework that uses pretty simple API to connect to database and draw your desired chart type.

Here are the steps:
  1. We create a chart object
  2. Set data SQL query
  3. Set Chart properties and Labels
  4. Get chart output

Expand|Select|Wrap|Line Numbers
  1. $p = new chartphp();
  2.  
  3. $p->data_sql = "select c.categoryname, sum(a.quantity) as Sales
  4.                 from products b, `order details` a, categories c
  5.                 where a.productid = b.productid and c.categoryid = b.categoryid
  6.                 group by c.categoryid
  7.                 order by c.categoryid";
  8.  
  9. // Line Data
  10. $p->chart_type = "bar";
  11.  
  12. // Common Options
  13. $p->title = "Category Sales";
  14. $p->xlabel = "Category";
  15. $p->ylabel = "Sales";
  16.  
  17. $out = $p->render('c1');
  18.  
This code will generate a database driven bar chart directly from your database.

See screenshot: bar-db-chart.jpg

Variety of charts are available in this framework, yet simplicity intact.
Apr 1 '15 #1
5 16168
nice site for a developer
Jun 16 '15 #2
Ajay Bhalala
119 64KB
Hi,

It's very nice.
Jul 1 '15 #3
nice solution. I'm starting to run all dashboards on Cyfe. It's cheap and easy, plus it looks good and I can share it.
Sep 23 '15 #4
What a code, always wanna be a programer :D
Nov 21 '16 #5
Just to update followers: Chart4PHP v2.0 is released with a lot of new features and simplicity. More details: http://www.chartphp.com/charts-4-php-v2-0-released/
Oct 24 '17 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
3
by: somesteve | last post by:
I feel so stupid for posting this but I cannot figure out why I'm getting an error with this simple update query. update set = '02/14/06 06:00:00 AM' where = '02/14/20 06:00:00 AM' Gives...
5
by: Kuna | last post by:
Hi All, I am trying to create a gantt chart in php by getting data from database. I am using WindowsXp OS and having php-4 and my-sql DB. I have installed the JPGRAPH package to my system and...
4
by: BerkshireGuy | last post by:
I have the following dataset: PolNum DateInterviewComplete --------------------------------------------- 1234 11/23/2006 1323 11/03/2006 4342 Null 2322 Null I want to...
2
by: alecitacruz | last post by:
Hello guys!!! I am new here and I am trying to create a chart resulting from a filter created in VBA, in which I first filter data with an SQL statement (Select...) and then I make a filter of time...
0
by: rGh | last post by:
Hello, I am trying to create an Excel chart from C#. I am not a very experieced programmer, so my mistake could be very trivial. The code included is a very simple example. I just wanted to...
16
by: iheartvba | last post by:
Hi, I have a simple append query which takes data from a form and appends it into a table. INSERT INTO tblSmsSent ( MobileNumber, ClientName, TimeSent, AppointmentTime, AppointmentDate )...
1
by: Dave Smith | last post by:
I need to take four queries and make them into one table This is what I get when I just do a simple select query. SELECT Wholesale_Rank_qry_2.OMNI_Number, Wholesale_Rank_qry_2.branch, ...
4
by: jamil afridi | last post by:
Can some one show me that how a Simple Search Query make which can fined any thing from a database with whole record.
0
by: tjn825 | last post by:
cant seem to change the total line name for simple Access Query: Total $450,000 to say Total Cost $450,000
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.