@@ -23,30 +23,6 @@ const queries = {
2323 ` ,
2424} ;
2525
26- const baseColors = [
27- [ 54 , 162 , 235 ] , [ 255 , 99 , 132 ] ,
28- [ 255 , 206 , 86 ] , [ 75 , 192 , 192 ] ,
29- [ 153 , 102 , 255 ] , [ 255 , 159 , 64 ] ,
30- [ 46 , 204 , 113 ] , [ 231 , 76 , 60 ] ,
31- [ 52 , 73 , 94 ] , [ 241 , 196 , 15 ]
32- ] ;
33-
34- function generateColor ( index ) {
35- const base = baseColors [ index % baseColors . length ] ;
36- const variation = Math . floor ( ( index / baseColors . length ) * 50 ) ; // فرق بسيط في اللون
37- const r = Math . min ( base [ 0 ] + variation , 255 ) ;
38- const g = Math . min ( base [ 1 ] + variation , 255 ) ;
39- const b = Math . min ( base [ 2 ] + variation , 255 ) ;
40- return `rgba(${ r } , ${ g } , ${ b } , 0.8)` ;
41- }
42-
43- function getChartColors ( n ) {
44- const colors = [ ] ;
45- for ( let i = 0 ; i < n ; i ++ ) {
46- colors . push ( generateColor ( i ) ) ;
47- }
48- return colors ;
49- }
5026function createChart ( ctx , { labels, data } , title , pos , maintainAspectRatio = true ) {
5127 const chartColors = getChartColors ( labels . length ) ;
5228
@@ -69,6 +45,7 @@ function createChart(ctx, { labels, data }, title, pos, maintainAspectRatio = tr
6945 aspectRatio : 2 ,
7046 plugins : {
7147 legend : {
48+ display : false ,
7249 position : pos ,
7350 rtl : true ,
7451 textDirection : "rtl" ,
@@ -136,10 +113,8 @@ async function make_main_data(json) {
136113 const sortedFeatures = Array . from ( featureMap . values ( ) )
137114 . sort ( ( a , b ) => b . count - a . count ) ;
138115
139- const featureArray = Array . from ( sortedFeatures . values ( ) ) ;
140- const uzz = aggregateOthers ( featureArray , 21 ) ;
141116
142- return uzz ;
117+ return aggregateOthers ( sortedFeatures , 19 ) ;
143118}
144119
145120async function make_category_feature_data ( json ) {
@@ -186,32 +161,34 @@ async function make_category_feature_data(json) {
186161 return result ;
187162}
188163
189- async function one_chart ( n , char1Data , title , pos , maintainAspectRatio = true ) {
164+ async function one_chart ( n , char1Data , maintainAspectRatio ) {
190165 // ---
191166 const loader = document . getElementById ( `loader${ n } ` ) ;
192167 let ctx = document . getElementById ( `chart${ n } ` ) ;
193168 // ---
169+ let pos = ( char1Data . labels . length > 15 ) ? "bottom" : "right" ;
170+ // ---
194171 if ( ! ctx ) return ;
195172 // ---
196173 let ctx2d = ctx . getContext ( '2d' ) ;
197174 // ---
198175 // رسم المخطط وإخفاء مؤشر التحميل الخاص به
199176 if ( char1Data . labels . length > 0 ) {
200- createChart ( ctx2d , char1Data , title , pos , maintainAspectRatio ) ;
177+ createChart ( ctx2d , char1Data , "" , pos , maintainAspectRatio ) ;
201178 // ---
179+ // إضافة الـ legend داخل card
180+ const chartColors = getChartColors ( char1Data . labels . length ) ;
181+ const legendContainer = document . getElementById ( `legend${ n } ` ) ;
182+ if ( legendContainer ) {
183+ legendContainer . innerHTML = createLegendHTML ( char1Data . labels , chartColors ) ;
184+ }
202185 }
203186 // ---
204187 // تحديث إجمالي عدد المفردات
205188 const all_lemmas = document . getElementById ( `all_lemmas_${ n } ` ) ;
206189 // ---
207190 if ( all_lemmas ) {
208- // sum achar1Data.data
209- let total = char1Data . data . reduce ( ( a , b ) => a + b , 0 ) ;
210- // ---
211- // format total
212- total = total . toLocaleString ( ) ;
213- // ---
214- all_lemmas . innerHTML = ` (${ total } ) `
191+ all_lemmas . innerHTML = ` (${ char1Data . labels . length . toLocaleString ( ) } ) `
215192 }
216193 // ---
217194 if ( loader ) {
@@ -229,16 +206,25 @@ function make_card(index, title, height) {
229206 </h2>
230207 </div>
231208 <div class="card-body">
232- <div class="position-relative" style="min-height: ${ height } ; width: 100%;">
233- <div id="loader${ index } " class="loader">
234- <div class="d-flex align-items-center">
235- <div class="spinner-border text-primary" role="status">
236- <span class="visually-hidden">Loading...</span>
209+ <div class="row">
210+ <div class="col-md-7 col-sm-12">
211+ <div id="legend${ index } " class="ms-1 mt-1">
212+ <!-- Legend سيضاف هنا -->
213+ </div>
214+ </div>
215+ <div class="col-md-5 col-sm-12">
216+ <div class="position-relative" style="height: ${ height } ; width: 100%;">
217+ <div id="loader${ index } " class="loader">
218+ <div class="align-items-center">
219+ <div class="spinner-border text-primary" role="status">
220+ <span class="visually-hidden">Loading...</span>
221+ </div>
222+ <span class="ms-3 h5 fw-semibold text-secondary">جاري تحميل البيانات...</span>
223+ </div>
237224 </div>
238- <span class="ms-3 h5 fw-semibold text-secondary">جاري تحميل البيانات...</span >
225+ <canvas id="chart ${ index } "></canvas >
239226 </div>
240227 </div>
241- <canvas id="chart${ index } "></canvas>
242228 </div>
243229 </div>
244230 </div>
@@ -325,6 +311,11 @@ function change_labels(data) {
325311 return data ;
326312}
327313
314+
315+ function hideLoading ( ) {
316+ document . getElementById ( "loading" ) . classList . add ( "d-none" ) ;
317+ }
318+
328319async function initializeCharts ( ) {
329320 let title = ' إجمالي استخدامات الميزات النحوية في المفردات العربية <span id="all_lemmas_00"></span>' ;
330321 // ---
@@ -336,12 +327,11 @@ async function initializeCharts() {
336327 // ---
337328 let main_data = await make_main_data ( json ) ;
338329 // ---
339- let pos = ( main_data . labels . length > 15 ) ? "bottom" : "right" ;
340- let height = "320px" ; //(main_data.labels.length > 15) ? "400px" : "220px";
330+ let height = "15rem" ; //(main_data.labels.length > 15) ? "400px" : "220px";
341331 // ---
342332 $ ( "#canvas_container" ) . append ( make_card ( 0 , title , height ) ) ;
343333 // ---
344- await one_chart ( 0 , main_data , title , "right" , false ) ;
334+ await one_chart ( 0 , main_data , false ) ;
345335 // ---
346336 let all_categories_data = await make_category_feature_data ( json ) ;
347337 // ---
@@ -356,12 +346,12 @@ async function initializeCharts() {
356346 <span id="all_lemmas_${ index2 } "></span>
357347 ` ;
358348 // ---
359- let pos = ( categoryData . labels . length > 20 ) ? "bottom" : "right" ;
360- let height = ( categoryData . labels . length > 20 ) ? "400px" : "220px" ;
361- let maintainAspectRatio = ( categoryData . labels . length > 20 ) ? true : false ;
349+ let height = "15rem" ;
362350 // ---
363351 $ ( "#canvas_container" ) . append ( make_card ( index2 , title , height ) ) ;
364352 // ---
365- await one_chart ( index2 , categoryData , title , pos , false ) ;
353+ await one_chart ( index2 , categoryData , false ) ;
366354 }
355+ // ---
356+ hideLoading ( ) ;
367357}
0 commit comments