1+ import type { AriaComponentOption , ComposeOption , GridComponentOption , LegendComponentOption , PieSeriesOption , SeriesOption } from "echarts"
2+ import { isRtl } from "./document"
3+
4+ export const processAria = ( option : ComposeOption < AriaComponentOption > , chartDecal : boolean ) => {
5+ if ( ! option ) return
6+ const ariaOption = generateAriaOption ( chartDecal )
7+ option . aria = ariaOption
8+ }
9+
10+ const generateAriaOption = ( chartDecal : boolean ) : AriaComponentOption => {
11+ if ( ! chartDecal ) {
12+ return { enabled : false }
13+ }
14+ const color = "rgba(0, 0, 0, 0.2)"
15+ return {
16+ enabled : true ,
17+ decal : {
18+ show : true ,
19+ decals : [ {
20+ color,
21+ dashArrayX : [ 1 , 0 ] ,
22+ dashArrayY : [ 2 , 5 ] ,
23+ rotation : .5235987755982988 ,
24+ } , {
25+ color,
26+ symbol : 'circle' ,
27+ dashArrayX : [ [ 8 , 8 ] , [ 0 , 8 , 8 , 0 ] ] ,
28+ dashArrayY : [ 6 , 0 ] ,
29+ symbolSize : .8 ,
30+ } , {
31+ color,
32+ dashArrayX : [ 1 , 0 ] ,
33+ dashArrayY : [ 4 , 3 ] ,
34+ rotation : - .7853981633974483
35+ } , {
36+ color,
37+ dashArrayX : [ [ 6 , 6 ] , [ 0 , 6 , 6 , 0 ] ] ,
38+ dashArrayY : [ 6 , 0 ] ,
39+ } , {
40+ color,
41+ dashArrayX : [ [ 1 , 0 ] , [ 1 , 6 ] ] ,
42+ dashArrayY : [ 1 , 0 , 6 , 0 ] ,
43+ rotation : .7853981633974483 ,
44+ } , {
45+ color,
46+ symbol : 'triangle' ,
47+ dashArrayX : [ [ 9 , 9 ] , [ 0 , 9 , 9 , 0 ] ] ,
48+ dashArrayY : [ 7 , 2 ] ,
49+ symbolSize : .75 ,
50+ } ]
51+ }
52+ }
53+ }
54+
55+ export const processRtl = ( option : unknown ) => {
56+ if ( ! isRtl ( ) || ! option ) return
57+
58+ const { grid, legend, series } = option as ComposeOption <
59+ | GridComponentOption | LegendComponentOption
60+ | PieSeriesOption
61+ >
62+ processArrayLike ( grid , processGridRtl )
63+ processArrayLike ( legend , processLegendRtl )
64+ processArrayLike ( series , processSeriesRtl )
65+ }
66+
67+ const processArrayLike = < T , > ( arr : T | T [ ] , processor : ( t : T ) => void ) => {
68+ if ( ! arr ) return
69+ if ( Array . isArray ( arr ) ) {
70+ arr . filter ( e => ! ! e ) . forEach ( processor )
71+ } else {
72+ processor ?.( arr )
73+ }
74+ }
75+
76+ const processGridRtl = ( option : GridComponentOption ) => {
77+ // Swap right and left
78+ swapPosition ( option )
79+ }
80+
81+ const processLegendRtl = ( option : LegendComponentOption ) => {
82+ swapPosition ( option )
83+ swapAlign ( option , 'align' )
84+ }
85+
86+ const processSeriesRtl = ( option : PieSeriesOption ) => {
87+ if ( option . type === 'pie' ) {
88+ const center = option . center
89+ if ( ! Array . isArray ( center ) ) return
90+ const left = center [ 0 ]
91+ if ( typeof left !== 'string' || ! left . endsWith ( '%' ) ) return
92+ try {
93+ const originPercentStr = left . substring ( 0 , left . length - 1 )
94+ const originPercent = left . includes ( '.' ) ? parseFloat ( originPercentStr ) : parseInt ( originPercentStr )
95+ center [ 0 ] = `${ 100 - originPercent } %`
96+ } catch ( ignored ) { }
97+ }
98+ }
99+
100+ const swapAlign = < K extends string > ( option : { [ k in K ] ?: 'right' | 'left' | 'auto' } , key : K ) => {
101+ const { [ key ] : align } = option || { }
102+ if ( align === 'left' ) {
103+ option [ key ] = 'right'
104+ } else if ( align === 'right' ) {
105+ option [ key ] = 'left'
106+ }
107+ }
108+
109+ const swapPosition = ( option : { left ?: string | number , right ?: string | number } ) => {
110+ const right = option . right
111+ option . right = option . left
112+ option . left = right
113+ }
0 commit comments