Skip to content

Commit 13ca3f1

Browse files
author
sheepzh
committed
Semi
1 parent 3a6aadb commit 13ca3f1

File tree

1 file changed

+68
-53
lines changed

1 file changed

+68
-53
lines changed

script/user-chart/render.ts

Lines changed: 68 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,62 @@ import { Argv, Browser } from "./argv"
1212
import { filenameOf, getExistGist } from "./common"
1313
import { EChartsType, init } from "echarts"
1414

15-
const ALL_BROWSERS: Browser[] = ['chrome', 'firefox', "edge"]
15+
const ALL_BROWSERS: Browser[] = ['firefox', 'edge', 'chrome']
16+
17+
type OriginData = {
18+
[browser in Browser]: UserCount
19+
}
20+
21+
type ChartData = {
22+
xAixs: string[]
23+
yAixses: {
24+
[browser in Browser]: number[]
25+
}
26+
}
1627

1728
export async function render(argv: Argv): Promise<void> {
1829
const token = argv.gistToken
1930
// 1. get all data
20-
const [chromeUser, firefoxUser, edgeUser]: UserCount[] = await Promise.all(
21-
ALL_BROWSERS.map(b => getDataFromGist(token, b))
22-
)
23-
// 2. Find all dates and sort them
31+
const originData: OriginData = await getOriginData(token)
32+
// 2. pre-process data
33+
const chartData = preProcess(originData)
34+
// 3. render csv
35+
const svg = render2Svg(chartData)
36+
// 4. upload
37+
await upload2Gist(token, svg)
38+
process.exit()
39+
}
40+
41+
function preProcess(originData: OriginData): ChartData {
42+
// 1. sort datess
2443
const dateSet = new Set<string>()
25-
Object.keys(chromeUser).forEach(d => dateSet.add(d))
26-
Object.keys(firefoxUser).forEach(d => dateSet.add(d))
27-
Object.keys(edgeUser).forEach(d => dateSet.add(d))
44+
Object.values(originData).forEach(ud => Object.keys(ud).forEach(date => dateSet.add(date)))
2845
let allDates = Array.from(dateSet).sort()
2946

30-
// 3. smooth the dates
31-
const chromeCtx = new SmoothContext()
32-
const edgeCtx = new SmoothContext()
33-
const firefoxCtx = new SmoothContext()
47+
// 2. smooth the count
48+
const ctx: { [browser in Browser]: SmoothContext } = {
49+
chrome: new SmoothContext(),
50+
firefox: new SmoothContext(),
51+
edge: new SmoothContext(),
52+
}
3453

35-
allDates.forEach(date => {
36-
chromeCtx.process(chromeUser[date])
37-
edgeCtx.process(edgeUser[date])
38-
firefoxCtx.process(firefoxUser[date])
39-
})
54+
allDates.forEach(
55+
date => ALL_BROWSERS.forEach(b => ctx[b].process(originData[b][date]))
56+
)
57+
const result = {
58+
xAixs: allDates,
59+
yAixses: {
60+
chrome: ctx.chrome.end(),
61+
firefox: ctx.firefox.end(),
62+
edge: ctx.edge.end(),
63+
}
64+
}
4065

41-
let chromeData = chromeCtx.end()
42-
let edgeData = edgeCtx.end()
43-
let firefoxData = firefoxCtx.end()
44-
45-
// 4. zoom
46-
const dataSize = Object.keys(chromeData)?.length
47-
const reduction = Math.floor(dataSize / 150)
48-
chromeData = zoom(chromeData, reduction)
49-
edgeData = zoom(edgeData, reduction)
50-
firefoxData = zoom(firefoxData, reduction)
51-
allDates = zoom(allDates, reduction)
52-
53-
// 5. render csv
54-
const svg = render2Svg(chromeData, edgeData, firefoxData, allDates)
55-
// 6. upload
56-
await upload2Gist(token, svg)
57-
process.exit()
66+
// 3. zoom
67+
const reduction = Math.floor(Object.keys(allDates).length / 150)
68+
result.xAixs = zoom(result.xAixs, reduction)
69+
ALL_BROWSERS.forEach(b => result.yAixses[b] = zoom(result.yAixses[b], reduction))
70+
return result
5871
}
5972

6073
class SmoothContext {
@@ -115,24 +128,20 @@ function zoom<T>(data: T[], reduction: number): T[] {
115128
return newData
116129
}
117130

118-
function render2Svg(chromeData: number[], edgeData: number[], firefoxData: number[], allDates: string[]): string {
131+
function render2Svg(chartData: ChartData): string {
132+
const { xAixs, yAixses } = chartData
119133
const chart: EChartsType = init(null, null, {
120134
renderer: 'svg',
121135
ssr: true,
122-
width: 1280,
136+
width: 960,
123137
height: 640
124138
})
125-
const legendsData: Record<string, number[]> = {
126-
Firefox: firefoxData,
127-
Edge: edgeData,
128-
Chrome: chromeData,
129-
}
130139
chart.setOption({
131140
title: {
132141
text: 'Total Active User Count',
133-
subtext: `${allDates[0]} to ${allDates[allDates.length - 1]}`
142+
subtext: `${xAixs[0]} to ${xAixs[xAixs.length - 1]}`
134143
},
135-
legend: { data: Object.keys(legendsData) },
144+
legend: { data: ALL_BROWSERS },
136145
grid: {
137146
left: '3%',
138147
right: '4%',
@@ -142,27 +151,33 @@ function render2Svg(chromeData: number[], edgeData: number[], firefoxData: numbe
142151
xAxis: [{
143152
type: 'category',
144153
boundaryGap: false,
145-
data: allDates
154+
data: xAixs
146155
}],
147156
yAxis: [
148157
{ type: 'value' }
149158
],
150-
series: Object.entries(legendsData)
151-
.map(([legend, data]) => ({
152-
name: legend,
153-
type: 'line',
154-
stack: 'Total',
155-
// Fill the area
156-
areaStyle: {},
157-
data
158-
}))
159+
series: ALL_BROWSERS.map(b => ({
160+
name: b,
161+
type: 'line',
162+
stack: 'Total',
163+
// Fill the area
164+
areaStyle: {},
165+
data: yAixses[b]
166+
}))
159167
})
160168
return chart.renderToSVGString()
161169
}
162170

163171
const USER_COUNT_GIST_DESC = "User count of timer, auto-generated"
164172
const USER_COUNT_SVG_FILE_NAME = "user_count.svg"
165173

174+
async function getOriginData(token: string): Promise<OriginData> {
175+
const [firefox, edge, chrome]: UserCount[] = await Promise.all(
176+
ALL_BROWSERS.map(b => getDataFromGist(token, b))
177+
)
178+
return { chrome, firefox, edge }
179+
}
180+
166181
/**
167182
* Get the data from gist
168183
*/

0 commit comments

Comments
 (0)