-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
549 lines (524 loc) · 17.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
// This file is modified by guole
// * Translated into Chinese.
// * Remove title of category statistics chart.
// * Enlarge size of category statistics chart.
// * Add post canlendar.
// * Add category radar chart.
const cheerio = require('cheerio')
const moment = require('moment')
hexo.extend.filter.register('after_generate', function () {
// 首先获取整体的配置项名称
const config = hexo.config.charts ? hexo.config.charts : hexo.theme.config.charts
// 如果配置开启
if (!(config && config.enable)) return
// 集体声明配置项
const data = {
postsChart_Title: config.postsChart.title ? config.postsChart.title : "",
postsCalendar_Title: config.postsCalendar_title ? config.postsCalendar_title : "",
tagsChart_Title: config.tagsChart.title ? config.tagsChart.title : "",
categoriesChart_Title: config.categoriesChart_title ? config.categoriesChart_title : "",
categoriesRadar_Title: config.categoriesRadar_title ? config.categoriesRadar_title : "",
postsChart_interval: config.postsChart.interval ? config.postsChart.interval : 0,
tagsChart_interval: config.tagsChart.interval ? config.tagsChart.interval : 0,
echarts_CDN: config.echarts_CDN ? config.echarts_CDN : 'https://lib.baomitu.com/echarts/4.7.0/echarts.min.js'
}
hexo.extend.filter.register('after_render:html', function (locals) {
const $ = cheerio.load(locals)
const post = $('#posts-chart')
const tag = $('#tags-chart')
const category = $('#categories-chart')
const calendar = $('#posts-calendar')
const radar = $('#categories-radar')
let htmlEncode = false
if (post.length > 0 || tag.length > 0 || category.length > 0 || calendar.length > 0 || radar.length > 0) {
$('head').after('<style type="text/css">#posts-chart,#posts-calendar,#categories-chart,#categories-radar,#tags-chart{width: 100%;height: 300px;margin: 0.5rem auto;padding: 0.5rem;display: flex;}</style><script type="text/javascript" data-pjax src="' + data.echarts_CDN + '"></script>')
if (post.length > 0 && $('#postsChart').length === 0) {
if (post.attr('data-encode') === 'true') htmlEncode = true
post.after(postsChart())
}
if (calendar.length > 0 && $('#postsCalendar').length === 0) {
if (calendar.attr('data-encode') === 'true') htmlEncode = true
calendar.after(postsCalendar())
}
if (tag.length > 0 && $('#tagsChart').length === 0) {
if (tag.attr('data-encode') === 'true') htmlEncode = true
tag.after(tagsChart(tag.attr('data-length')))
}
if (category.length > 0 && $('#categoriesChart').length === 0) {
if (category.attr('data-encode') === 'true') htmlEncode = true
category.after(categoriesChart())
}
if (radar.length > 0 && $('#categoriesRadar').length === 0) {
if (radar.attr('data-encode') === 'true') htmlEncode = true
radar.after(categoriesRadar())
}
if (htmlEncode) {
return $.root().html().replace(/&#/g, '&#')
} else {
return $.root().html()
}
} else {
return locals
}
}, 25)
function postsChart () {
const startDate = moment().subtract(1, 'years').startOf('month')
const endDate = moment().endOf('month')
const monthMap = new Map()
const dayTime = 3600 * 24 * 1000
for (let time = startDate; time <= endDate; time += dayTime) {
const month = moment(time).format('YYYY-MM')
if (!monthMap.has(month)) {
monthMap.set(month, 0)
}
}
hexo.locals.get('posts').forEach(function (post) {
const month = post.date.format('YYYY-MM')
if (monthMap.has(month)) {
monthMap.set(month, monthMap.get(month) + 1)
}
})
const monthArr = JSON.stringify([...monthMap.keys()])
const monthValueArr = JSON.stringify([...monthMap.values()])
return `
<script id="postsChart" data-pjax>
var color = '#6e7f98';
var color = document.documentElement.getAttribute('data-theme') === 'light' ? '#4c4948' : '#afb8c5';
let postsChart = echarts.init(document.getElementById('posts-chart'));
let postsOption = {
title: {
text: '${data.postsChart_Title}',
top: 5,
x: 'center',
textStyle: {
color: color
}
},
grid: {
top: 90,
containLabel: true
},
tooltip: {
trigger: 'axis'
},
xAxis: {
name: '日期',
type: 'category',
axisTick: {
show: false
},
axisLine: {
show: true,
lineStyle: {
color: color
}
},
axisLabel: {
interval:${data.postsChart_interval},
rotate:20
},
data: ${monthArr}
},
yAxis: {
name: '文章篇数',
type: 'value',
splitLine: {
show: false
},
axisTick: {
show: false
},
axisLine: {
show: true,
lineStyle: {
color: color
}
}
},
series: [
{
// name: 'Number of Post',
name: '文章篇数',
type: 'line',
smooth: true,
lineStyle: {
width: 0
},
showSymbol: false,
itemStyle: {
opacity: 1,
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgba(128, 255, 165)'
},
{
offset: 1,
color: 'rgba(1, 191, 236)'
}])
},
areaStyle: {
opacity: 1,
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgba(128, 255, 165)'
}, {
offset: 1,
color: 'rgba(1, 191, 236)'
}])
},
data: ${monthValueArr},
markLine: {
itemStyle: {color: ['#ab47bc']},
data: [
{type: 'average', name: '平均值'}
]
}
}
]
};
postsChart.setOption(postsOption);
window.addEventListener("resize", () => {
postsChart.resize();
});
</script>`
}
// Post Canlendar added by guole
function postsCalendar () {
// calculate range.
const startDate = moment().subtract(1, 'years')
const endDate = moment()
const rangeArr = '["' + startDate.format('YYYY-MM-DD') + '", "' + endDate.format('YYYY-MM-DD') + '"]'
// post and count map.
const dateMap = new Map()
hexo.locals.get('posts').forEach(function (post) {
const date = post.date.format('YYYY-MM-DD')
const count = dateMap.get(date)
dateMap.set(date, count === null || count === undefined ? 1 : count + 1)
})
// loop the data for the current year, generating the number of post per day
var i = 0
var datePosts = '['
const dayTime = 3600 * 24 * 1000
for (let time = startDate; time <= endDate; time += dayTime) {
const date = moment(time).format('YYYY-MM-DD')
datePosts = (i === 0 ? datePosts + '["' : datePosts + ', ["') + date + '", ' + (dateMap.has(date) ? dateMap.get(date) : 0) + ']'
i++
}
datePosts += ']'
return `
<script id="postsCalendar" data-pjax>
var color = '#6e7f98';
var color = document.documentElement.getAttribute('data-theme') === 'light' ? '#4c4948' : '#afb8c5';
let postsCalendar = echarts.init(document.getElementById('posts-calendar'));
let postsCalendarOption = {
title: {
top: 5,
text: '${data.postsCalendar_Title}',
left: 'center',
textStyle: {
color: color
}
},
tooltip: {
padding: 10,
backgroundColor: '#555',
borderColor: '#777',
borderWidth: 1,
formatter: function (obj) {
var value = obj.value;
return '<div style="font-size: 14px;">' + value[0] + '<br/> ✒️文章 : ' + value[1] + '</div>';
}
},
visualMap: {
show: true,
showLabel: true,
categories: [0, 1, 2, 3, ">=4"],
calculable: true,
inRange: {
symbol: 'rect',
color: ['#ebedf0', '#c6e48b', '#7bc96f', '#239a3b', '#196127']
},
itemWidth: 12,
itemHeight: 12,
orient: 'horizontal',
left: 'center',
textStyle: {
color: color
},
bottom: 30
},
calendar: [{
top: 90,
left: 'center',
range: ${rangeArr},
cellSize: [14, 14],
splitLine: {
show: false
},
itemStyle: {
color: color,
borderColor: '#fff',
borderWidth: 2
},
yearLabel: {
show: false
},
monthLabel: {
nameMap: 'cn',
fontSize: 12,
textStyle: {
color: color
}
},
dayLabel: {
formatter: '{start} 1st',
nameMap: ['', '周一', '', '周三', '', '周五', ''],
fontSize: 12,
textStyle: {
color: color
}
}
}],
series: [{
type: 'heatmap',
coordinateSystem: 'calendar',
calendarIndex: 0,
data: ${datePosts}
}]
};
postsCalendar.setOption(postsCalendarOption);
</script>`
}
function tagsChart (dataLength = 10) {
const tagArr = []
hexo.locals.get('tags').map(function (tag) {
tagArr.push({ name: tag.name, value: tag.length })
})
tagArr.sort((a, b) => { return b.value - a.value })
const tagNameArr = []
const tagCountArr = []
for (let i = 0, len = Math.min(tagArr.length, dataLength); i < len; i++) {
tagNameArr.push(tagArr[i].name)
tagCountArr.push(tagArr[i].value)
}
const tagNameArrJson = JSON.stringify(tagNameArr)
const tagCountArrJson = JSON.stringify(tagCountArr)
return `
<script id="tagsChart" data-pjax>
var color = '#6e7f98';
var color = document.documentElement.getAttribute('data-theme') === 'light' ? '#4c4948' : '#afb8c5';
let tagsChart = echarts.init(document.getElementById('tags-chart'));
let tagsOption = {
title: {
text: '${data.tagsChart_Title}',
top: 5,
textStyle: {
color: color
},
x: 'center'
},
grid: {
top: 90,
containLabel: true
},
tooltip: {
formatter: "{b} : {c}"
},
xAxis: {
name: '标签',
type: 'category',
axisTick: {
show: false
},
axisLine: {
show: true,
lineStyle: {
color: color
}
},
axisLabel: {
interval:${data.tagsChart_interval},
rotate:20
},
grid: {
left: '10%',
bottom:'25%'
},
data: ${tagNameArrJson}
},
yAxis: {
name: '文章篇数',
type: 'value',
splitLine: {
show: false
},
axisTick: {
show: false
},
axisLine: {
show: true,
lineStyle: {
color: color
}
}
},
series: [
{
name: '文章篇数',
type: 'bar',
color: ['#82d3f4'],
data: ${tagCountArrJson},
itemStyle: {
opacity: 1,
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgba(128, 255, 165)'
},
{
offset: 1,
color: 'rgba(1, 191, 236)'
}])
},
emphasis: {
itemStyle: {
opacity: 1,
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgba(128, 255, 195)'
},
{
offset: 1,
color: 'rgba(1, 211, 255)'
}])
}
},
markPoint: {
symbolSize: 45,
data: [{
type: 'max',
itemStyle: {color: ['#3ecf8e']},
name: '最大值'
}, {
type: 'min',
itemStyle: {color: ['#fa755a']},
name: '最小值'
}],
},
markLine: {
itemStyle: {color: ['#ab47bc']},
data: [
{type: 'average', name: '平均值'}
]
}
}
]
};
tagsChart.setOption(tagsOption);
</script>`
}
function categoriesChart () {
const categoryArr = []
hexo.locals.get('categories').map(function (category) {
categoryArr.push({ name: category.name, value: category.length })
})
const categoryArrJson = JSON.stringify(categoryArr)
return `
<script id="categoriesChart" data-pjax>
var color = '#6e7f98';
var color = document.documentElement.getAttribute('data-theme') === 'light' ? '#4c4948' : '#afb8c5';
let categoriesChart = echarts.init(document.getElementById('categories-chart'));
let categoriesOption = {
title: {
text: '${data.categoriesChart_Title}',
top: 5,
x: 'center',
textStyle: {
color: color
}
},
tooltip: {
trigger: 'item',
formatter: "{a} <br/>{b} : {c} ({d}%)"
},
series: [
{
// name: 'Category:',
name: '✒️文章篇数',
type: 'pie',
radius: '75%',
color: ['#6772e5', '#ff9e0f', '#fa755a', '#3ecf8e', '#82d3f4', '#ab47bc', '#525f7f', '#f51c47', '#26A69A'],
data: ${categoryArrJson},
itemStyle: {
emphasis: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
};
categoriesChart.setOption(categoriesOption);
</script>`
}
// Category Radar added by guole
function categoriesRadar () {
const categories = hexo.locals.get('categories')
// Find the maximum and average values of the post categories.
const radarValueArr = []
categories.some(function (category) {
radarValueArr.push(category.length)
})
const max = Math.max.apply(null, radarValueArr) + Math.min.apply(null, radarValueArr)
// Calculate the data needed for the radar chart.
const indicatorArr = [];
categories.map(function (category) {
indicatorArr.push({ name: category.name, max: max });
});
const indicatorData = JSON.stringify(indicatorArr);
const radarValueData = JSON.stringify(radarValueArr);
return `
<script id="categoriesRadar" data-pjax>
var color = '#6e7f98';
var color = document.documentElement.getAttribute('data-theme') === 'light' ? '#4c4948' : '#afb8c5';
let categoriesRadar = echarts.init(document.getElementById('categories-radar'));
let categoriesRadarOption = {
title: {
left: 'center',
text: '${data.categoriesRadar_Title}',
textStyle: {
fontWeight: 500,
fontSize: 22,
color: color
}
},
tooltip: {},
radar: {
name: {
textStyle: {
color: color
}
},
indicator: ${indicatorData},
nameGap: 5,
center: ['50%','55%'],
radius: '75%',
left: 'center'
},
series: [{
type: 'radar',
color: ['#3ecf8e'],
itemStyle: {normal: {areaStyle: {type: 'default'}}},
data : [
{
value : ${radarValueData},
// name : 'Categories:'
name: '✒️文章篇数'
}
]
}]
};
categoriesRadar.setOption(categoriesRadarOption);
</script>`
}
})