Creating a simple Bar Chart with SQL Query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • azghanvi1
    New Member
    • Mar 2015
    • 2

    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:

    Code:
    select c.categoryname, sum(a.quantity) as Sales
    				from products b, `order details` a, categories c
    				where a.productid = b.productid and c.categoryid = b.categoryid
    				group by c.categoryid
    				order by c.categoryid
    This would result in following data:
    Code:
    "Beverages"		"9532"
    "Condiments"		"5298"
    "Confections"		"7906"
    "Dairy Products"	"9149"
    "Grains/Cereals"	"4562"
    "Meat/Poultry"		"4199"
    "Produce"		"2990"
    "Seafood"		"7681"

    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


    Code:
    $p = new chartphp();
    
    $p->data_sql = "select c.categoryname, sum(a.quantity) as Sales
    				from products b, `order details` a, categories c
    				where a.productid = b.productid and c.categoryid = b.categoryid
    				group by c.categoryid
    				order by c.categoryid";
    					
    // Line Data
    $p->chart_type = "bar";
    
    // Common Options
    $p->title = "Category Sales";
    $p->xlabel = "Category";
    $p->ylabel = "Sales";
    
    $out = $p->render('c1');
    This code will generate a database driven bar chart directly from your database.

    See screenshot: Click image for larger version

Name:	bar-db-chart.jpg
Views:	1
Size:	32.7 KB
ID:	5418870

    Variety of charts are available in this framework, yet simplicity intact.
    Last edited by Niheel; Jun 19 '15, 01:20 AM. Reason: commercial links
  • Shyam Jaiswal
    New Member
    • Jun 2015
    • 1

    #2
    nice site for a developer

    Comment

    • Ajay Bhalala
      New Member
      • Nov 2014
      • 119

      #3
      Hi,

      It's very nice.

      Comment

      • RankFuse
        New Member
        • Sep 2015
        • 1

        #4
        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.

        Comment

        • fizzwizz
          New Member
          • Nov 2016
          • 1

          #5
          What a code, always wanna be a programer :D

          Comment

          • azghanvi1
            New Member
            • Mar 2015
            • 2

            #6
            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/

            Comment

            Working...