Skip to main content

Creating an APEX plugin for an Oracle JET component - Part 1

In APEX 5.1 (still Early Adaptor 1 at this moment), Oracle JET - Javascript Extension Toolkit -  is included to facilitate charting.
The APEX Development Team recently mentioned that not only the Data Visualisations part of JET will be included in APEX 5.1, but the complete installation of JET. The whole package. That won't do the size of the downloadable install file any good, but more important is: what can we do with it?

A number of the Data Visualisations will be exposed in APEX for the declarative definition of charts as we are used to now using the Anycharts library. But there are more Data Visualisations - check the JET Cookbook for all examples - and other components that might be of interest for an APEX application. So how can we use these in our apps?

As an example, I would like to use the PictoChart component in my application.
Fist of all - as we don't have APEX 5.1 yet - we need to download the Oracle JET library and install the files on either your web server or upload all of them into the APEX Static Files. As there are a lot of files - and I mean, really a lot - I would recommend storing the files on your web server.

Next, if you follow the steps of the JET Cookbook, you'll notice that they all make heavy use of the Knockout Javascript library. Knockout is extremely powerful, but also rather complex for most APEX developers. And in an APEX environment we don't really need it. So can we use Oracle JET components without Knockout, as there is no example on the (official) JET pages?
But there is an un(der)documented feature, which is the "initializer" call of an Oracle JET component, click here to open that part of the documentation of the PictoChart component. That looks more familiar to us, as it is just an (old fashioned) Javascript call with a JSON object as a parameter! So how do we get that on an APEX Page?

First of all we need to include RequireJS into our application by referencing

    #IMAGE_PREFIX#jet/js/libs/require/require.js

in the "File URLs" property of our Page. Of course, the exact location is dependent on where you stored the JET library. Next we have to tell RequireJS where all the Oracle JET files are that we need to load. Therefore we call (in the "Execute when Page Loads" property of the page) :

    requirejs.config({
        baseUrl: apex_img_dir + "oraclejet/js/libs",
        paths: {
            "jquery"         : "jquery/jquery-2.1.3.min",
            "jqueryui-amd"   : "jquery/jqueryui-amd-1.11.4.min",
            "ojs"            : "oj/v2.0.0/min",
            "ojL10n"         : "oj/v2.0.0/ojL10n",
            "ojtranslations" : "oj/v2.0.0/resources",
            "promise"        : "es6-promise/promise-1.0.0.min"
        },
        shim: {
            jquery: {
                exports: ["jQuery", "$"]
            }
        }
    })

Now, also in the "Execute when Page Loads" property of the page, we can call the initializer of the PictoChart component :

    require(['ojs/ojcore', 'jquery', 'ojs/ojpictochart'],
      function (oj, $) {
        $("#pc1").ojPictoChart(
           {items: [
              { name  : 'Have  Sleep Problems'
              , shape : 'human'
              , count : 7
              , color : '#ed6647'
              },
              { name  : 'Sleep Well'
              , shape : 'human'
              , count: 3
              }]
           , animationOnDisplay: 'zoom'
           , columnCount: 5          
        });    
    }); 

and finally we need to define component with the id "pc1" above mentioned into where the PictoChart is rendered by creating a region serving static content:

<div  id="picto-container">
  <div id='pc1'style="vertical-align:middle; margin-right:15px">
  </div>
  <div style="display:inline-block; vertical-align:middle; font-weight:bold">
    <span style="color:#333333; font-size:1.1em">7 out of 10 college students</span><br>
    <span style="color:#ed6647; font-size:1.3em">have sleep problems.</span>
  </div>
</div>

As a result we will see the exact same PictoChart as shown above in our APEX Page.
In the next blog post I will show how to convert this hard-coded data into an easy-to-use APEX plugin.

So stay tuned !








Comments

Popular posts from this blog

Filtering in the APEX Interactive Grid

Remember Oracle Forms? One of the nice features of Forms was the use of GLOBAL items. More or less comparable to Application Items in APEX. These GLOBALS where often used to pre-query data. For example you queried Employee 200 in Form A, then opened Form B and on opening that Form the Employee field is filled with that (GLOBAL) value of 200 and the query was executed. So without additional keys strokes or entering data, when switching to another Form a user would immediately see the data in the same context. And they loved that. In APEX you can create a similar experience using Application Items (or an Item on the Global Page) for Classic Reports (by setting a Default Value to a Search Item) and Interactive Reports (using the  APEX_IR.ADD_FILTER  procedure). But what about the Interactive Grid? There is no APEX_IG package ... so the first thing we have to figure out is how can we set a filter programmatically? Start with creating an Interactive Grid based upon the good old Employ

apex_application.g_f0x array processing in Oracle 12

If you created your own "updatable reports" or your custom version of tabular forms in Oracle Application Express, you'll end up with a query that looks similar to this one: then you disable the " Escape special characters " property and the result is an updatable multirecord form. That was easy, right? But now we need to process the changes in the Ename column when the form is submitted, but only if the checkbox is checked. All the columns are submitted as separated arrays, named apex_application.g_f0x - where the "x" is the value of the "p_idx" parameter you specified in the apex_item calls. So we have apex_application.g_f01, g_f02 and g_f03. But then you discover APEX has the oddity that the "checkbox" array only contains values for the checked rows. Thus if you just check "Jones", the length of g_f02 is 1 and it contains only the empno of Jones - while the other two arrays will contain all (14) rows. So for

Stop using validations for checking constraints !

 If you run your APEX application - like a Form based on the EMP table - and test if you can change the value of Department to something else then the standard values of 10, 20, 30 or 40, you'll get a nice error message like this: But it isn't really nice, is it? So what do a lot of developers do? They create a validation (just) in order to show a nicer, better worded, error message like "This is not a valid department".  And what you then just did is writing code twice : Once in the database as a (foreign key) check constraint and once as a sql statement in your validation. And we all know : writing code twice is usually not a good idea - and executing the same query twice is not enhancing your performance! So how can we transform that ugly error message into something nice? By combining two APEX features: the Error Handling Function and the Text Messages! Start with copying the example of an Error Handling Function from the APEX documentation. Create this function