1- import  {  barChart ,   calendarHeatmap  }  from  "./charts.js" ; 
1+ import  {  calendarHeatmap  }  from  "./charts.js" ; 
22import  {  STATS_PATTERNS ,  Statistics  }  from  "querylib" ; 
33import  moment  from  "moment" ; 
44
5+ const  TOP_CRATE_LENGTH  =  15 ; 
56let  chartWidth  =  460 ; 
67const  TYPE_OTHER  =  "other" ; 
78export  const  CHART_COLOR  =  "rgba(249, 188, 45, 0.5)" ; 
@@ -79,13 +80,10 @@ function calculateSavedTime(times) {
7980    } 
8081} 
8182
82- function  renderSearchTimes ( length  =  0 ,   searchTime )  { 
83+ function  renderSearchTimes ( length  =  0 )  { 
8384    let  searchTimes  =  document . querySelector ( ".search-time" ) ; 
8485    let  frequency  =  searchTimes . querySelectorAll ( "b" ) ; 
8586    frequency [ 0 ] . textContent  =  `${ length }  ; 
86-     if  ( searchTime )  { 
87-         frequency [ 1 ] . textContent  =  `${ searchTime }  ; 
88-     } 
8987    frequency [ 2 ] . textContent  =  calculateSavedTime ( length ) ; 
9088} 
9189
@@ -157,35 +155,7 @@ function renderSearchStats(typeDataObj, total) {
157155    } ) ; 
158156} 
159157
160- function  renderTopCratesChart ( topCratesObj )  { 
161-     const  topCratesContainer  =  document . querySelector ( ".topCratesData" ) ; 
162-     if  ( topCratesContainer . hasChildNodes ( ) )  { 
163-         topCratesContainer . innerHTML  =  null ; 
164-     } 
165-     const  topCratesData  =  Object . entries ( topCratesObj ) 
166-         . sort ( ( a ,  b )  =>  b [ 1 ]  -  a [ 1 ] ) 
167-         . map ( ( [ key ,  value ] ,  index )  =>  { 
168-             return  { 
169-                 label : `#${ index  +  1 }  , 
170-                 name : key , 
171-                 value
172-             } ; 
173-         } ) ; 
174-     topCratesData . splice ( 15 ) ; 
175-     barChart ( { 
176-         margin : ( {  top : 30 ,  right : 0 ,  bottom : 10 ,  left : 30  } ) , 
177-         // Calculate height dynamically to keep the bar with consistence width regardless of the topCratesData length. 
178-         height : 800  /  15  *  topCratesData . length  +  40 , 
179-         barHeight : 25 , 
180-         width : chartWidth , 
181-         data : topCratesData , 
182-         selector : ".topCratesData" , 
183-         color : CHART_COLOR , 
184-     } ) ; 
185- } 
186- 
187- 
188- export  async  function  renderCharts ( now ,  yearAgo ,  searchTime )  { 
158+ export  async  function  renderCharts ( now ,  yearAgo )  { 
189159    chartWidth  =  Math . min ( chartWidth ,  document . getElementById ( "chart" ) . clientWidth )  -  10 ; 
190160    const  {  timeline }  =  await  Statistics . load ( ) ; 
191161
@@ -209,9 +179,7 @@ export async function renderCharts(now, yearAgo, searchTime) {
209179    let  typeTotal  =  0 ; 
210180    const  typeDataObj  =  Object . create ( null ) ; 
211181
212-     const  topCratesObj  =  Object . create ( null ) ; 
213- 
214-     data . forEach ( ( [ t ,  content ,  type ] )  =>  { 
182+     data . forEach ( ( [ t ,  content ] )  =>  { 
215183        const  time  =  moment ( t ) ; 
216184        const  hour  =  time . hour ( ) ; 
217185
@@ -225,15 +193,11 @@ export async function renderCharts(now, yearAgo, searchTime) {
225193            typeDataObj [ typeName ]  =  ( typeDataObj [ typeName ]  ||  0 )  +  1 ; 
226194            typeTotal  +=  1 ; 
227195        } 
228-         if  ( type )  { 
229-             topCratesObj [ type ]  =  ( topCratesObj [ type ]  ||  0 )  +  1 ; 
230-         } 
231196    } ) ; 
232197
233-     renderSearchTimes ( data . length ,   searchTime ) ; 
198+     renderSearchTimes ( data . length ) ; 
234199    renderHeatmap ( heatMapData ,  now ,  yearAgo ) ; 
235200    renderSearchStats ( typeDataObj ,  typeTotal ) ; 
236-     renderTopCratesChart ( topCratesObj ) ; 
237201} 
238202
239203/** 
@@ -251,7 +215,9 @@ export async function getHistogramEchartDatas(now, yearAgo) {
251215    const  dateArr  =  DATES_LABEL . map ( ( )  =>  0 ) ; 
252216    const  hourArr  =  HOURS_LABEL . map ( ( )  =>  0 ) ; 
253217
254-     for  ( const  [ t ]  of  data )  { 
218+     const  topCratesObj  =  Object . create ( null ) ; 
219+ 
220+     for  ( const  [ t ,  ,  type ]  of  data )  { 
255221        const  time  =  moment ( t ) ; 
256222        const  hour  =  time . hour ( ) ; 
257223
@@ -260,11 +226,26 @@ export async function getHistogramEchartDatas(now, yearAgo) {
260226        if  ( hour  !==  0 )  { 
261227            hourArr [ hour  -  1 ]  +=  1 ; 
262228        } 
229+         if  ( type )  { 
230+             topCratesObj [ type ]  =  ( topCratesObj [ type ]  ||  0 )  +  1 ; 
231+         } 
263232    } ; 
233+ 
234+     const  topCratesArr  =  Object . entries ( topCratesObj ) 
235+         . sort ( ( a ,  b )  =>  b [ 1 ]  -  a [ 1 ] ) 
236+         . map ( ( [ key ,  value ] ,  index )  =>  { 
237+             return  { 
238+                 label : `#${ index  +  1 }  , 
239+                 name : key , 
240+                 value
241+             } ; 
242+         } ) ; 
243+     topCratesArr . splice ( TOP_CRATE_LENGTH ) ; 
264244    return  { 
265245        weeksArr, 
266246        dateArr, 
267247        hourArr, 
248+         topCratesArr, 
268249    } 
269250} 
270251
0 commit comments