1
- import { barChart , calendarHeatmap } from "./charts.js" ;
1
+ import { calendarHeatmap } from "./charts.js" ;
2
2
import { STATS_PATTERNS , Statistics } from "querylib" ;
3
3
import moment from "moment" ;
4
4
5
+ const TOP_CRATE_LENGTH = 15 ;
5
6
let chartWidth = 460 ;
6
7
const TYPE_OTHER = "other" ;
7
8
export const CHART_COLOR = "rgba(249, 188, 45, 0.5)" ;
@@ -79,13 +80,10 @@ function calculateSavedTime(times) {
79
80
}
80
81
}
81
82
82
- function renderSearchTimes ( length = 0 , searchTime ) {
83
+ function renderSearchTimes ( length = 0 ) {
83
84
let searchTimes = document . querySelector ( ".search-time" ) ;
84
85
let frequency = searchTimes . querySelectorAll ( "b" ) ;
85
86
frequency [ 0 ] . textContent = `${ length } ` ;
86
- if ( searchTime ) {
87
- frequency [ 1 ] . textContent = `${ searchTime } ` ;
88
- }
89
87
frequency [ 2 ] . textContent = calculateSavedTime ( length ) ;
90
88
}
91
89
@@ -157,35 +155,7 @@ function renderSearchStats(typeDataObj, total) {
157
155
} ) ;
158
156
}
159
157
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 ) {
189
159
chartWidth = Math . min ( chartWidth , document . getElementById ( "chart" ) . clientWidth ) - 10 ;
190
160
const { timeline } = await Statistics . load ( ) ;
191
161
@@ -209,9 +179,7 @@ export async function renderCharts(now, yearAgo, searchTime) {
209
179
let typeTotal = 0 ;
210
180
const typeDataObj = Object . create ( null ) ;
211
181
212
- const topCratesObj = Object . create ( null ) ;
213
-
214
- data . forEach ( ( [ t , content , type ] ) => {
182
+ data . forEach ( ( [ t , content ] ) => {
215
183
const time = moment ( t ) ;
216
184
const hour = time . hour ( ) ;
217
185
@@ -225,15 +193,11 @@ export async function renderCharts(now, yearAgo, searchTime) {
225
193
typeDataObj [ typeName ] = ( typeDataObj [ typeName ] || 0 ) + 1 ;
226
194
typeTotal += 1 ;
227
195
}
228
- if ( type ) {
229
- topCratesObj [ type ] = ( topCratesObj [ type ] || 0 ) + 1 ;
230
- }
231
196
} ) ;
232
197
233
- renderSearchTimes ( data . length , searchTime ) ;
198
+ renderSearchTimes ( data . length ) ;
234
199
renderHeatmap ( heatMapData , now , yearAgo ) ;
235
200
renderSearchStats ( typeDataObj , typeTotal ) ;
236
- renderTopCratesChart ( topCratesObj ) ;
237
201
}
238
202
239
203
/**
@@ -251,7 +215,9 @@ export async function getHistogramEchartDatas(now, yearAgo) {
251
215
const dateArr = DATES_LABEL . map ( ( ) => 0 ) ;
252
216
const hourArr = HOURS_LABEL . map ( ( ) => 0 ) ;
253
217
254
- for ( const [ t ] of data ) {
218
+ const topCratesObj = Object . create ( null ) ;
219
+
220
+ for ( const [ t , , type ] of data ) {
255
221
const time = moment ( t ) ;
256
222
const hour = time . hour ( ) ;
257
223
@@ -260,11 +226,26 @@ export async function getHistogramEchartDatas(now, yearAgo) {
260
226
if ( hour !== 0 ) {
261
227
hourArr [ hour - 1 ] += 1 ;
262
228
}
229
+ if ( type ) {
230
+ topCratesObj [ type ] = ( topCratesObj [ type ] || 0 ) + 1 ;
231
+ }
263
232
} ;
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 ) ;
264
244
return {
265
245
weeksArr,
266
246
dateArr,
267
247
hourArr,
248
+ topCratesArr,
268
249
}
269
250
}
270
251
0 commit comments