@@ -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" ) ;
0 commit comments