Skip to main content

Single Callback to Draw Multiple Google Charts in Oracle APEX


Here, I am giving you an elegant way to draw multiple google charts in Oracle APEX using single callback method. This has been inspired by Google ChartsGoogle chart tools are powerful, simple to use, and free. We can try out Google's rich gallery of interactive charts and data tools.

For example, suppose you want to draw multiple charts (Pie, Bar, Line and Donut), showing top 10 covid-19 infected countries as on today. The following example shows how to display both charts side-by-side.

If you want to draw multiple charts for the same data, it may be more convenient to write a single callback for both charts.

Fig 1: Pie, Bar, Line & Donut Charts

Step 1: Create required objects (table, sequence, trigger) and populate sample data into a table. (As per your requirements)

Step 2: Create a new blank page.

Step 3: Create a region to the page. Change region type to PLSQL Dynamic Content.

Drawing Multiple Charts using Single Callback:

Copy and paste below PLSQL code into the region's PLSQL code source.

DECLARE
l_title VARCHAR2(240) := 'Breakdown of Top 10 Infected Countries';
l_chart_values VARCHAR2(4000);

CURSOR cur_top10_countries IS
SELECT country, total_cases
  FROM (SELECT COUNTRY, TOTAL_CASES,
        RANK() OVER (ORDER BY total_cases DESC) AS rank 
      FROM fxgn_global_data)
 WHERE rank <= 10;
BEGIN 
  htp.p('<html>');
  htp.p('<head>');
  htp.p('<script type="text/javascript" 
src="https://www.gstatic.com/charts/loader.js"></script>');
  htp.p('<script type="text/javascript">');

  -- Load Charts and the corechart and barchart packages.
  htp.p('google.charts.load(''current'', {''packages'':[''corechart'']})');

  -- Draw the pie chart and bar chart when Charts is loaded.
  htp.p('google.charts.setOnLoadCallback(drawChart)');

  -- Construct chart values
  FOR i IN cur_top10_countries
  LOOP
    l_chart_values := l_chart_values || '['''||i.country||''','||i.total_cases||'],';
  END LOOP;
    l_chart_values := substr(l_chart_values,1, length(l_chart_values)-1);
  --dbms_output.put_line (l_chart_values);

-- draw chart
  htp.p('function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn(''string'', ''Country'');
        data.addColumn(''number'', ''Total Cases'');
        data.addRows(['||l_chart_values||'
        ])');

-- setup pie chart options
  htp.p('var piechart_options = {title:''Piechart: '||l_title||''',
                                 width:500,
                                 height:300}');
  htp.p('var piechart = new google.visualization.PieChart
(document.getElementById(''piechart_div''))');
  htp.p('piechart.draw(data, piechart_options)');

-- setup bar chart options
  htp.p('var barchart_options = {title:''Barchart: '||l_title||''',
                                 width:500,
                                 height:300,
                                 legend: ''none''}');
  htp.p('var barchart = new google.visualization.BarChart
(document.getElementById(''barchart_div''))');
  htp.p('barchart.draw(data, barchart_options)');

-- setup line chart options
htp.p('var linechart_options = {title:''Linechart: '||l_title||''',
                                 width:500,
                                 height:300,
                                 legend: ''none''}');
  htp.p('var linechart = new google.visualization.LineChart
(document.getElementById(''linechart_div''))');
  htp.p('linechart.draw(data, linechart_options)');

-- setup donut chart options
  htp.p('var donutchart_options = {title:''Donutchart: '||l_title||''',
                                 width:500,
                                 height:300,
                                 pieHole: 0.4}');
  htp.p('var donutchart = new google.visualization.PieChart
(document.getElementById(''donutchart_div''))');
  htp.p('donutchart.draw(data, donutchart_options)');

  htp.p('}');
  htp.p('</script>');
  htp.p('</head>');
  htp.p('<body>');

  --Table and divs that hold the pie, bar, line, donut charts
  htp.p('<table class="columns">');
  htp.p('<tr>');
  htp.p('<td><div id="piechart_div" style="border: 1px solid #ccc"></div></td>');
  htp.p('<td><div id="barchart_div" style="border: 1px solid #ccc"></div></td>');
  htp.p('<tr>');
  htp.p('<tr>');
  htp.p('<td><div id="linechart_div" style="border: 1px solid #ccc"></div></td>');
  htp.p('<td><div id="donutchart_div" style="border: 1px solid #ccc"></div></td>');
  htp.p('</tr>');
  htp.p('</table>');
END;



Output: Then your output would then display like this,


The demo is here.

That's it. Happy APExing!!!...

Comments

  1. Thanks for this helpful article, Are there way like this to draw waterfall-chart like google waterfall-chart?
    thanks in advance

    ReplyDelete

Post a Comment

Popular posts from this blog

Printing Page Numbers in RTF Template [Oracle BI Publisher]

Here I am giving an example to print the page numbers dynamically in the RTF (Rich Text Format) template. Step 1:  Go to page footer and copy and paste the below script. Page |  <?fo:page-number?>  of  <?fo:page-number-citation:xdofo:lastpage-joinseq?> <fo:page-number> :   This is the object, which is used to represent the current page-number. <?fo:page-number-citation:xdofo:lastpage-joinseq?> :  This is the syntax, which is used to represent the total number of pages. Step 2:  Load the XML and preview the result. Output: That's it. References: fo:page-number Printing Page Number Code in Oracle XMLP RTF Template

Friendly URL: Redirect to Different Page after Login in Oracle APEX 20.1

Oracle has updated apex.oracle.com to APEX 20.1 which includes among other features the new Friendly URL option. Here i am giving an example to redirect to different page after login in Oracle APEX 20.1 [Friendly URL Enabled] Step 1: Define home page for each user in emp master table as below Step 2: To enable Friendly URL Syntax, follow below steps, 1) On the Workspace home page, click the App Builder icon. 2) Select an application (The Application home page appears). 3) From Application home page, you can access the Definition page in TWO ways: Click the Edit Application Properties button. From Shared Components:              1) Click Shared Components .              2) Under Application Logic, click Application Definition Attributes . The Definition page appears. 4) Under Properties, configure the Friendly URL s attribute: Click Apply Changes to save your ch...

Open modal/inline dialog in Oracle APEX using JS

Analysis: From Oracle Apex 4.2,  Inline dialog can be opened as modal dialog/popup using JS. Open inline dialog region as modal dialog: Step 1: Create static region. Step 2:  Set Template => Inline Dialog. Step 3:  Assign the static id to the region. Fig 1: Set template and Static ID Step 4:  Create button or change one of the report column type to Link. (Action: Redirect URL). Step 5:  Put the below JS to open the region as modal dialog.                javascript:openModal('unitpricedetails');            Fig 2:  Open simple modal dialog Open inline dialog region as modal dialog and pass values to the items: Common Steps:  Step 1 to Step 4. Step 5:  To pass value to that region, create P1_UNIT_ID in the inline  dialog.       javascript:$s('P1_UNIT_ID','#UNIT_ID#');javascript:openModal('unitpricedetai...