2024年8月21日水曜日

Plotly.jsをOracle APEXのアプリケーションに組み込む

Plotly.jsをOracle APEXのアプリケーションに組み込んでみます。

Oracle JETやその他のチャート描画ライブラリの組み込み作業で表示したチャートと、同じチャートを表示してみます。


直接Plotly.jsで描画



Plotly.jsのGetting started in JavaScriptのページを参照して、実装を進めます。バー・チャートの表示はこちらのページにある例を元にしています。バー・チャートのリファレンスはこちらです。

ページ・プロパティJavaScriptファイルURLに以下を記述します。

https://cdn.plot.ly/plotly-2.34.0.min.js


静的コンテンツのリージョンの名前Plotly.jsとし、ソースHTMLコードに以下を記述します。

<div id="myChart" class="w600 h400"></div>


チャートに描画するデータを保持するページ・アイテムとしてP1_GROUPSP1_VALUEを作成します。タイプ非表示です。

ページ・アイテムP1_GROUPS計算を作成し、ページの描画前にデータを設定します。計算タイプとしてSQL問合せ(単一の値を返す)を選択し、SQL問合せに以下を記述します。
select
    json_arrayagg(
        ename
    order by empno asc )
from emp where deptno = 10

ページ・アイテムP1_VALUEにも計算を作成し、ページ描画前に値を設定します。SQL問合せに以下を記述します。
select
    json_arrayagg(
        sal
    order by empno asc )
from emp where deptno = 10

チャートを描画するJavaScriptのコードを、ページ・プロパティページ・ロード時に実行に記述します。

const groups = apex.items.P1_GROUPS.value;
const value = apex.items.P1_VALUE.value;
var trace1 = {
type: 'bar',
orientation: 'h',
x: JSON.parse(value),
y: JSON.parse(groups),
marker: {
color: '#309fdb'
}
};
var data = [ trace1 ];
var layout = {
font: {size: 14}
};
var config = {responsive: true}
Plotly.newPlot('myChart', data, layout, config );


以上で実装は完了です。ページを実行すると以下のように表示されます。



テンプレート・コンポーネントの作成



Plotly.jsのバー・チャートを描画するテンプレート・コンポーネントについて、設定内容を説明します。

テンプレート・コンポーネントの名前Plotly.js Bar Chartとしました。テンプレート部分には以下を記述しています。

<div class="#CSS_CLASSES#">
<a-plotly-js-bar-chart id="#APEX$DOM_ID#" groups="#GROUPS#" value="#VALUE#" color="#COLOR#" orientation="#ORIENTATION#" width="#WIDTH#" height="#HEIGHT#"></a-plotly-js-bar-chart>
</div>


カスタム属性としてCSS ClassesGroupsValueColorOrientationWidthHeightを作成しています。GroupsValueタイプセッション・ステート値ColorテキストWidthHeightは両方とも整数です。


チャートの向きの指定であるOrientation静的LOVは、horizontalの戻り値としてhverticalの戻り値としてvを設定します。


ディレクトリjsファイル名script.jsとして、カスタム要素a-plotly-js-bar-chartの実装を記述します。

(function (debug) {
"use strict";
class PlotlyJsBarChart extends HTMLElement {
chart;
chartId;
constructor() {
super();
this.chartId = new Date().getTime();
debug.info("%s, constructor", this.chartId);
}
connectedCallback() {
debug.info("%s, %s, connectedCallback, %s", this.chartId, new Date().getTime(), this.isConnected);
if (this.isConnected) {
/* prepare div for chart region */
const div = document.createElement("div");
const width = this.getAttribute("width");
if ( width ) {
div.classList.add(`w${width}`);
};
const height = this.getAttribute("height");
if ( height ) {
div.classList.add(`h${height}`);
};
this.appendChild(div);
const groups = this.getAttribute("groups");
const value = this.getAttribute("value");
var trace1 = {
type: 'bar',
orientation: 'h',
x: JSON.parse(value),
y: JSON.parse(groups),
marker: {
color: '#309fdb'
}
};
const color = this.getAttribute("color");
if ( color ) {
trace1.marker.color = color;
};
const orientation = this.getAttribute("orientation");
if ( orientation === 'v' ) {
trace1.orientation = orientation;
};
var data = [ trace1 ];
var layout = {
font: {size: 14}
};
var config = {responsive: true};
let chart = Plotly.newPlot(div, data, layout, config);
this.chart = chart;
}
}
disconnectedCallback() {
debug.info("%s, %s, disconnectedCallback", this.chartId, new Date().getTime());
if (this.firstChild) {
this.removeChild(this.firstChild);
}
}
}
document.addEventListener('DOMContentLoaded', () => {
if ( window.customElements.get("a-plotly-js-bar-chart") === undefined ) {
debug.info("define custom element");
window.customElements.define("a-plotly-js-bar-chart", PlotlyJsBarChart);
};
});
})(apex.debug);
view raw script.js hosted with ❤ by GitHub

ロードするファイルURLJavaScriptに以下を記述します。

https://cdn.plot.ly/plotly-2.34.0.min.js
#PLUGIN_FILES#js/script#MIN#.js


以上でテンプレート・コンポーネントは完成です。

リージョンにPlotly.jsのテンプレート・コンポーネントを実装します。

ページ・アイテムP2_GROUPSP2_VALUEは、最初に作成したP1_GROUPSP1_VALUEと同じ設定で作成します。

識別名前Plotly.jsタイプPlotly.js Bar Chartとします。


リージョンの属性を開き、GroupsとしてP2_GROUPSValueとしてP2_VALUEColorとして#309fdbOrientationとしてhorizontalを設定します。


以上で実装は完了です。ページを実行すると以下のように表示されます。



対話モード・レポートへの組み込み



対話モード・レポートソースSQL問合せとして以下を記述します。
select
    dname,
    json_arrayagg(
        ename
    order by empno asc ) groups,
    json_arrayagg(
        sal
    order by empno asc ) value,
    '' chart
from emp_dept_v group by dname

対話モード・レポートの列CHARTを選択し、識別タイプPlotly.js Bar Chartに変更します。

設定CSS Classesw130GroupsGROUPSValueVALUEColor#309fdbOrientationhorizontalを設定します。Width400Height300を設定します。


以上で実装は完了です。ページを実行すると以下のように表示されます。


今回の記事は以上になります。

作成したAPEXアプリケーションのエクスポートを以下に置きました。
https://github.com/ujnak/apexapps/blob/master/exports/integrating-plotly-js.zip

Oracle APEXのアプリケーション作成の参考になれば幸いです。