Skip to content

Commit 9f5e107

Browse files
committed
fix: echarts uses fonts inherited from body (#623)
1 parent d4efb23 commit 9f5e107

File tree

2 files changed

+56
-17
lines changed

2 files changed

+56
-17
lines changed

src/pages/hooks/useEcharts.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@
66
* https://opensource.org/licenses/MIT
77
*/
88
import optionHolder from "@service/components/option-holder"
9-
import { processAnimation, processAria, processRtl } from "@util/echarts"
10-
import { type AriaComponentOption, type ComposeOption } from "echarts"
9+
import { processAnimation, processAria, processFont, processRtl } from "@util/echarts"
10+
import { type AriaComponentOption, type ComposeOption, SeriesOption, TitleComponentOption } from "echarts"
1111
import { type ECharts, init } from "echarts/core"
1212
import { ElLoading } from "element-plus"
1313
import { type Ref, isRef, onMounted, ref, watch } from "vue"
1414
import { useElementSize } from './useElementSize'
1515
import { useWindowSize } from "./useWindowSize"
1616

17-
type BaseEchartsOption = ComposeOption<AriaComponentOption>
17+
type BaseEchartsOption = ComposeOption<
18+
| AriaComponentOption
19+
| TitleComponentOption
20+
| SeriesOption
21+
>
1822

1923
export abstract class EchartsWrapper<BizOption, EchartsOption> {
2024
public instance: ECharts | undefined
@@ -27,9 +31,15 @@ export abstract class EchartsWrapper<BizOption, EchartsOption> {
2731
*/
2832
protected replaceSeries: boolean = false
2933
private lastBizOption: BizOption | undefined
34+
/**
35+
* Fix the font family
36+
* @see https://github.com/sheepzh/time-tracker-4-browser/issues/623
37+
*/
38+
private textFontFamily: string | undefined
3039

3140
init(container: HTMLDivElement) {
3241
this.instance = init(container)
42+
this.textFontFamily = getComputedStyle(container).fontFamily
3343
}
3444

3545
async render(biz: BizOption) {
@@ -40,7 +50,7 @@ export abstract class EchartsWrapper<BizOption, EchartsOption> {
4050

4151
private async innerRender() {
4252
const biz = this.lastBizOption
43-
const option = biz && await this.generateOption(biz) as (EchartsOption & BaseEchartsOption)
53+
const option = biz && await this.generateOption(biz)
4454
if (!option) return
4555

4656
await this.postChartOption(option)
@@ -54,6 +64,7 @@ export abstract class EchartsWrapper<BizOption, EchartsOption> {
5464
processAnimation(option, chartAnimationDuration)
5565
processAria(option, chartDecal)
5666
processRtl(option)
67+
this.textFontFamily && processFont(option, this.textFontFamily)
5768
}
5869

5970
async resize() {
@@ -66,7 +77,7 @@ export abstract class EchartsWrapper<BizOption, EchartsOption> {
6677
return this.instance!.getDom()
6778
}
6879

69-
protected abstract generateOption(biz: BizOption): Promise<EchartsOption> | EchartsOption
80+
protected abstract generateOption(biz: BizOption): Awaitable<EchartsOption>
7081

7182
protected getDomWidth(): number {
7283
return this.getDom()?.clientWidth ?? 0

src/util/echarts.ts

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import {
2-
type AriaComponentOption,
3-
type BarSeriesOption,
4-
type ComposeOption,
5-
type GridComponentOption,
6-
type LegendComponentOption,
7-
type LineSeriesOption,
8-
type PieSeriesOption,
9-
type ScatterSeriesOption,
10-
type ToolboxComponentOption,
11-
type VisualMapComponentOption,
1+
import type {
2+
AriaComponentOption,
3+
BarSeriesOption,
4+
ComposeOption,
5+
GridComponentOption,
6+
LegendComponentOption,
7+
LineSeriesOption,
8+
PieSeriesOption,
9+
ScatterSeriesOption,
10+
TitleComponentOption,
11+
ToolboxComponentOption,
12+
VisualMapComponentOption,
1213
} from "echarts"
1314
import { isRtl } from "./document"
1415

@@ -66,10 +67,37 @@ const generateAriaOption = (chartDecal: boolean): AriaComponentOption => {
6667
type SupportedSeriesOption = PieSeriesOption | LineSeriesOption | BarSeriesOption | ScatterSeriesOption
6768

6869
type GlobalEcOption = ComposeOption<
69-
| GridComponentOption | LegendComponentOption | ToolboxComponentOption | VisualMapComponentOption
70+
| TitleComponentOption | GridComponentOption | LegendComponentOption | ToolboxComponentOption | VisualMapComponentOption
7071
| SupportedSeriesOption
7172
>
7273

74+
export const processFont = (toProcess: unknown, elFont: string) => {
75+
const options = toProcess as GlobalEcOption
76+
const { series, title, legend } = options
77+
processArrayLike(title, t => {
78+
t.textStyle = {
79+
...t.textStyle,
80+
fontFamily: t.textStyle?.fontFamily ?? elFont,
81+
}
82+
t.subtextStyle = {
83+
...t.subtextStyle,
84+
fontFamily: t.subtextStyle?.fontFamily ?? elFont,
85+
}
86+
})
87+
processArrayLike(series, s => {
88+
s.label = {
89+
...s.label,
90+
fontFamily: s.label?.fontFamily ?? elFont,
91+
}
92+
})
93+
processArrayLike(legend, l => {
94+
l.textStyle = {
95+
...l.textStyle,
96+
fontFamily: l.textStyle?.fontFamily ?? elFont,
97+
}
98+
})
99+
}
100+
73101
export const processRtl = (toProcess: unknown) => {
74102
if (!isRtl() || !toProcess) return
75103
const option = toProcess as GlobalEcOption

0 commit comments

Comments
 (0)