Skip to content

Commit ce8c7fd

Browse files
committed
chore: fix rendering user chart
1 parent 33de7b0 commit ce8c7fd

3 files changed

Lines changed: 19 additions & 36 deletions

File tree

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@
2929
"license": "MIT",
3030
"devDependencies": {
3131
"@commitlint/types": "^21.0.1",
32-
"@crowdin/crowdin-api-client": "^1.55.1",
32+
"@crowdin/crowdin-api-client": "^1.55.2",
3333
"@emotion/babel-plugin": "^11.13.5",
3434
"@rsdoctor/rspack-plugin": "^1.5.11",
35-
"@rspack/cli": "^2.0.3",
36-
"@rspack/core": "^2.0.3",
37-
"@rstest/core": "^0.10.0",
38-
"@rstest/coverage-istanbul": "^0.10.0",
35+
"@rspack/cli": "^2.0.4",
36+
"@rspack/core": "^2.0.4",
37+
"@rstest/core": "^0.10.1",
38+
"@rstest/coverage-istanbul": "^0.10.1",
3939
"@types/chrome": "0.1.42",
4040
"@types/decompress": "^4.2.7",
4141
"@types/firefox-webext-browser": "^143.0.0",
42-
"@types/node": "^25.8.0",
42+
"@types/node": "^25.9.1",
4343
"@vue/babel-plugin-jsx": "^2.0.1",
4444
"babel-loader": "^10.1.1",
4545
"commitlint": "^21.0.1",
@@ -50,11 +50,11 @@
5050
"husky": "^9.1.7",
5151
"jsdom": "^29.1.1",
5252
"jszip": "^3.10.1",
53-
"knip": "^6.14.0",
54-
"postcss": "^8.5.14",
53+
"knip": "^6.14.1",
54+
"postcss": "^8.5.15",
5555
"postcss-loader": "^8.2.1",
5656
"postcss-rtlcss": "^6.0.0",
57-
"puppeteer": "^25.0.2",
57+
"puppeteer": "^25.0.4",
5858
"ts-node": "^10.9.2",
5959
"tsconfig-paths": "^4.2.0",
6060
"typescript": "6.0.3",
@@ -63,7 +63,7 @@
6363
"dependencies": {
6464
"@element-plus/icons-vue": "^2.3.2",
6565
"@emotion/css": "^11.13.5",
66-
"echarts": "^6.0.0",
66+
"echarts": "^6.1.0",
6767
"element-plus": "2.14.0",
6868
"hash.js": "^1.1.7",
6969
"qrcode-generator": "^2.0.4",

script/user-chart/add.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,12 @@ function parseChrome(content: string): UserCount {
113113
if (!dateStr || !numberStr) {
114114
return
115115
}
116-
// Replace '/' to '-', then rjust month and date
117-
const date = dateStr.split('/').map(str => rjust(str, 2, '0')).join('-')
116+
// Chrome CSV format: YYYY/M/D
117+
const [, y, m, d] = /^(\d{4})\/(\d{1,2})\/(\d{1,2})$/.exec(dateStr.trim()) ?? []
118+
if (!y || !m || !d) return
119+
const date = `${y}-${m.padStart(2, '0')}-${d.padStart(2, '0')}`
118120
const number = parseInt(numberStr)
119-
date && number && (result[date] = number)
121+
number && (result[date] = number)
120122
})
121123
return result
122124
}

script/user-chart/render.ts

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ type EcOption = ComposeOption<
1616
| GridComponentOption>
1717
const ALL_BROWSERS: Browser[] = ['firefox', 'chrome', 'edge']
1818

19-
const POINT_COUNT = 500
20-
2119
type OriginData = {
2220
[browser in Browser]: UserCount
2321
}
@@ -29,11 +27,13 @@ type ChartData = {
2927
}
3028
}
3129

30+
const VALID_DATE_RE = /^\d{4}-\d{2}-\d{2}$/
31+
3232
function preProcess(originData: OriginData): ChartData {
3333
// 1. sort dates
3434
const dateSet = new Set<string>()
3535
Object.values(originData).forEach(ud => Object.keys(ud).forEach(date => dateSet.add(date)))
36-
let allDates = Array.from(dateSet).sort()
36+
let allDates = Array.from(dateSet).filter(d => VALID_DATE_RE.test(d)).sort()
3737

3838
// 2. smooth the count
3939
const ctx: { [browser in Browser]: SmoothContext } = {
@@ -45,20 +45,14 @@ function preProcess(originData: OriginData): ChartData {
4545
allDates.forEach(
4646
date => ALL_BROWSERS.forEach(b => ctx[b].process(originData[b][date]))
4747
)
48-
const result: ChartData = {
48+
return {
4949
xAxis: allDates,
5050
yAxises: {
5151
chrome: ctx.chrome.end(),
5252
firefox: ctx.firefox.end(),
5353
edge: ctx.edge.end(),
5454
}
5555
}
56-
57-
// 3. zoom
58-
const reduction = Math.floor(Object.keys(allDates).length / POINT_COUNT)
59-
result.xAxis = zoom(result.xAxis, reduction)
60-
ALL_BROWSERS.forEach(b => result.yAxises[b] = zoom(result.yAxises[b], reduction))
61-
return result
6256
}
6357

6458
class SmoothContext {
@@ -109,19 +103,6 @@ class SmoothContext {
109103
}
110104
}
111105

112-
function zoom<T>(data: T[], reduction: number): T[] {
113-
let i = 0
114-
const newData: T[] = []
115-
while (i < data.length) {
116-
const item = data[i]
117-
if (item !== undefined) {
118-
newData.push(item)
119-
}
120-
i += reduction
121-
}
122-
return newData
123-
}
124-
125106
function render2Svg(chartData: ChartData): string {
126107
const { xAxis, yAxises } = chartData
127108
const chart: EChartsType = init(null, null, {

0 commit comments

Comments
 (0)