Oracle JETやその他のチャート描画ライブラリの組み込み作業で表示したチャートと、同じチャートを表示してみます。
直接CanvasJSで描画
ページ・プロパティのJavaScriptのファイルURLに以下を記述します。
https://cdn.canvasjs.com/canvasjs.min.js
<div id="myChart" class="h300"></div>
ページ・アイテムP1_VALUEに計算を作成し、ページの描画前にデータを設定します。計算のタイプとしてSQL問合せ(単一の値を返す)を選択し、SQL問合せに以下を記述します。
select
json_arrayagg(
json_object(
'y' value sal,
'label' value ename
)
order by empno asc )
from emp where deptno = 10
チャートを描画するJavaScriptのコードを、ページ・プロパティのページ・ロード時に実行に記述します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let data = [ | |
{ | |
type: "bar", | |
color: "#309fdb", | |
dataPoints: JSON.parse(apex.items.P1_VALUE.value) | |
} | |
]; | |
let config = { | |
animationEnabled: true, | |
title: {}, | |
axisX: { | |
interval: 1 | |
}, | |
axisY: { | |
minimum: 0 | |
}, | |
data: data | |
}; | |
let div = document.getElementById("myChart"); | |
let myChart = new CanvasJS.Chart(div, config); | |
myChart.render(); |
以上で実装は完了です。ページを実行すると以下のように表示されます。
テンプレート・コンポーネントの作成
テンプレート・コンポーネントの名前はCanvasJS Bar Chartとしました。テンプレートの部分には以下を記述しています。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="#CSS_CLASSES#"> | |
<a-canvasjs-bar-chart id="#APEX$DOM_ID#" value="#VALUE#" color="#COLOR#" orientation="#ORIENTATION#" width="#WIDTH#" height="#HEIGHT#"></a-canvasjs-bar-chart> | |
</div> |
カスタム属性としてCSS Classes、Value、Color、Orientationを作成しています。Valueのタイプはセッション・ステート値、Colorはカラーです。チャートが描画される範囲をWidthとHeightで指定します。これらのタイプは整数です。CanvasJSでは、Groupsとして与える列名はValueに含まれているため、カスタム属性のGroupsは作成していません。
ディレクトリをjs、ファイル名をscript.jsとして、カスタム要素a-canvasjs-bar-chartの実装を記述します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (debug) { | |
"use strict"; | |
class CanvasJSBarChart 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) { | |
/* append div as a first Child */ | |
const div = document.createElement("div"); | |
/* | |
* Set the width and height of the div element that CanvasJS uses for rendering. | |
* CanvasJS seems to create a canvas element within the div and inherit the size of its parent | |
* when actually drawing the chart. | |
*/ | |
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); | |
let data = [ | |
{ | |
type: "bar", | |
color: "#309fdb", | |
dataPoints: JSON.parse(this.getAttribute("value")) | |
} | |
]; | |
/* change color if the attribute exists */ | |
const color = this.getAttribute("color"); | |
if ( color ) { | |
data[0].color = color; | |
} | |
/* change orientation if the attribute exists */ | |
const orientation = this.getAttribute("orientation"); | |
if ( orientation === "column" ) { | |
data[0].type = orientation; | |
} | |
let config = { | |
animationEnabled: true, | |
title: {}, | |
axisX: { | |
interval: 1 | |
}, | |
axisY: { | |
minimum: 0 | |
}, | |
data: data | |
}; | |
debug.info(div); | |
/* draw chart */ | |
debug.info(config); | |
let chart = new CanvasJS.Chart(div, config); | |
chart.render(); | |
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-canvasjs-bar-chart") === undefined ) { | |
debug.info("define custom element"); | |
window.customElements.define("a-canvasjs-bar-chart", CanvasJSBarChart); | |
}; | |
}); | |
})(apex.debug); |
https://cdn.canvasjs.com/canvasjs.min.js
#PLUGIN_FILES#js/script#MIN#.js
以上でテンプレート・コンポーネントは完成です。
リージョンにCanvasJSのテンプレート・コンポーネントを実装します。
ページ・アイテムP2_VALUEは、最初に作成したP1_VALUEと同じ設定で作成します。
識別の名前をCanvasJS、タイプをCanvasJS Bar Chartとします。
リージョンの属性を開き、ValueとしてP2_VALUE、Colorとして#309fdb、Orientationとしてhorizontal、Heightに400を設定します。
以上で実装は完了です。ページを実行すると以下のように表示されます。
対話モード・レポートへの組み込み
対話モード・レポートのソースのSQL問合せとして以下を記述します。
select
dname,
json_arrayagg(
json_object(
'y' value sal,
'label' value ename
)
order by empno asc ) value,
'' chart
from emp_dept_v group by dname
対話モード・レポートの列CHARTを選択し、識別のタイプをCanvasJS Bar Chartに変更します。
設定のValueにVALUE、Colorに#309fdb、Orientationにhorizontalを設定します。Widthは400、Heightに200を設定します。