Community Menu

KBACE Login

Oracle E-Business Suite Blog

Information and Articles related to the Oracle E-Business Suite

Google Charts

Posted by: Siddharth Gandhi in Untagged  on Print PDF

Siddharth Gandhi
 
Have you ever desired using Charts without doing any installations, setting classpaths, copying jar files etc?
 
What if we have a database centric PL/SQL application and customer wish to see Charts without buying into Oracle Developer products? Or what about embedding charts in OAF pages without pushing .jar files and setting classpaths? And the list is endless from BI Pub reports to SSHR pages..
 
But then how easy can it get?
Can we generate a chart by just providing the URL?
Yes, we can now! Read on.
 
With Google Charts API you can build a URL that will dynamically create a chart for you.
 
Google Chart API URLs must be in the following format:
 
Parameters are separated using the ampersand character (&). You can specify as many parameters as you like, in any order. For example, the Chart API returns the following chart in response to the URL below:
 
Chart
 
http://chart.apis.google.com/chart?chs=250x100&chd=t:60,40&cht=p3&chl=Hello|World
 
Where:
  • http://chart.apis.google.com/chart? is the Chart API's location.
  • & separates parameters.
  • chs=250x100 is the chart's size in pixels.
  • chd=t:60,40 is the chart's data.
  • cht=p3 is the chart's type.
  • chl=Hello|World is the chart's label.

You can include a Chart API image in an HTML document by embedding a URL within an tag. When the webpage is displayed in a browser, the Chart API renders the image within the page. For example, the following tag results in the same image as above:

<img src="http://chart.apis.google.com/chart?
chs=250x100
&chd=t:60,40
&cht=p3
&chl=Hello|World"
alt="Sample chart" />

When you embed a URL in an HTML <img> tag, take care to use the character entity reference &amp; in place of an ampersand (&).

There are many more types of charts available (Bars, Pie, Scatter etc)

To try it out, we took a simple requirement: Make a pie chart grouping all the employee by departments.

Lets create a simple anonymous block to illustrate the same. This PL/SQL block will get us a simple URL which we can copy/paste in browser and get a chart. Or better, use as an tag for a report!

Here's the PL/SQL block:

---------------------------------

DECLARE 
  chd  VARCHAR2(4000);
  chdl VARCHAR2(4000);
  chl  VARCHAR2(4000);
  CURSOR c IS
  SELECT  ROUND(cnt / SUM(cnt) OVER () * 100, 2) pct, cnt, dname
  FROM
  (
     SELECT department_name dname, COUNT(*) cnt
     FROM employees, departments
     WHERE employees.department_id= departments.department_id
     GROUP BY departments.department_name
  )
  WHERE cnt>1
  ORDER BY 3 DESC;
BEGIN
  FOR usage IN c
  LOOP
    IF chd IS NULL THEN
      chd  := usage.pct;
    ELSE
      chd := chd || ',' || usage.pct;
    END IF;
   
    IF chdl IS NULL THEN
      chdl  := usage.dname;
    ELSE
      chdl := chdl || '|' || usage.dname;
    END IF;
   
    IF chl IS NULL THEN
      chl  := usage.cnt;
    ELSE
      chl := chl || '|' || usage.cnt;
    END IF;
  END LOOP;
  dbms_output.put_line('http://chart.apis.google.com/chart?cht=p&chs=300x200&&chco=FF0000'
    || '&chtt=Employees per department'
    || '&chl=' || chl || '&chd=t:' || chd || '&chdl=' || chdl);
END;

---------------------------------

The generated URL is:

http://chart.apis.google.com/chart?cht=p&chs=300x200&&chco=FF0000&chtt=Employees per department&chl=45|34|6|2|5|6|3|2&chd=t:43.69,33.01,5.83,1.94,4.85,5.83,2.91,1.94&chdl=Shipping|Sales|Purchasing|Marketing|IT|Finance|Executive|Accounting

And the image generated by Google Chart API for this URL is:

Image

Isn't that easy? Try it!

Just replace the SQL with yours and ensure the cursor logic works for you. And you're done.

Comments (2)Add Comment
Harsha V
...
written by Harsha V, February 04, 2011
Hi,

Is thier any chance to enable links on Pie chartfor each % of portion.

Ex:if I click on 35%,It should navigate to one page and if click on 45% It should navigate to other page.

Please provide input for this requirmnet.
Siddharth Gandhi
...
written by Siddharth Gandhi, February 04, 2011
For these drill down scenarios, you'll have to use Image Maps. Refer: http://code.google.com/apis/ch...ormat.html

But if this is for extensive reporting requirement, do look at Oracle BI charts which offers this out-of-the-box.

Write comment
You must be logged in to post a comment. Please register if you do not have an account yet.

busy