Skip to content

Commit 940597e

Browse files
committed
refactor: Add chart in demo
1 parent fd83795 commit 940597e

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

internal/npm-integration-poc/consolidated-app/webapp/controller/Main.controller.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ sap.ui.define([
2727
console.log(" Available methods:", Object.keys(validator).slice(0, 10).join(", "), "...");
2828

2929
console.log("✅ Chart.js loaded:", chartjs);
30+
31+
// Initialize chart after view is rendered
32+
this.getView().addEventDelegate({
33+
onAfterRendering: this._initChart.bind(this)
34+
});
3035
},
3136

3237
onValidate: function() {
@@ -94,6 +99,67 @@ sap.ui.define([
9499
MessageToast.show("Form reset");
95100
},
96101

102+
_initChart: function() {
103+
// Only init once
104+
if (this._chartInitialized) return;
105+
this._chartInitialized = true;
106+
107+
const canvas = document.getElementById("myChart");
108+
if (!canvas) {
109+
console.error("Chart canvas not found");
110+
return;
111+
}
112+
113+
// Chart.js v4+ requires explicit registration of components
114+
const {Chart, BarController, BarElement, CategoryScale, LinearScale, Title, Tooltip, Legend} = chartjs;
115+
Chart.register(BarController, BarElement, CategoryScale, LinearScale, Title, Tooltip, Legend);
116+
117+
// Generate random data
118+
const labels = ["January", "February", "March", "April", "May", "June"];
119+
const randomData = labels.map(() => Math.floor(Math.random() * 100));
120+
const randomData2 = labels.map(() => Math.floor(Math.random() * 100));
121+
122+
// Create chart using Chart.js
123+
this._chart = new Chart(canvas, {
124+
type: "bar",
125+
data: {
126+
labels: labels,
127+
datasets: [
128+
{
129+
label: "Sales 2025",
130+
data: randomData,
131+
backgroundColor: "rgba(54, 162, 235, 0.6)",
132+
borderColor: "rgba(54, 162, 235, 1)",
133+
borderWidth: 1
134+
},
135+
{
136+
label: "Sales 2026",
137+
data: randomData2,
138+
backgroundColor: "rgba(255, 99, 132, 0.6)",
139+
borderColor: "rgba(255, 99, 132, 1)",
140+
borderWidth: 1
141+
}
142+
]
143+
},
144+
options: {
145+
responsive: true,
146+
plugins: {
147+
title: {
148+
display: true,
149+
text: "Random Sales Data (Chart.js via NPM)"
150+
}
151+
},
152+
scales: {
153+
y: {
154+
beginAtZero: true
155+
}
156+
}
157+
}
158+
});
159+
160+
console.log("✅ Chart.js chart rendered successfully");
161+
},
162+
97163
onWebComponentClick: function() {
98164
// Toggle the bundled MessageStrip visibility
99165
const oMessageStrip = this.byId("bundledMessageStrip");

internal/npm-integration-poc/consolidated-app/webapp/view/Main.view.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
xmlns:f="sap.ui.layout.form"
77
xmlns:webc="sap.ui.webc.main"
88
xmlns:wcgen="com.example.app.thirdparty.@ui5.webcomponents.dist"
9+
xmlns:core="sap.ui.core"
910
displayBlock="true">
1011

1112
<App id="app">
@@ -92,6 +93,16 @@
9293
<webc:Button id="wcButton" design="Emphasized" icon="activate" click="onWebComponentClick" text="Web Component Validate" />
9394
</Toolbar>
9495

96+
<!-- Chart.js Demo Panel -->
97+
<Panel
98+
id="chartPanel"
99+
headerText="Chart.js Demo (Random Data)"
100+
class="sapUiSmallMarginTop">
101+
<content>
102+
<core:HTML id="chartContainer" content="&lt;canvas id='myChart' width='400' height='200'&gt;&lt;/canvas&gt;" />
103+
</content>
104+
</Panel>
105+
95106
<!-- Result Panel -->
96107
<Panel
97108
id="resultPanel"

0 commit comments

Comments
 (0)