Skip to content

Commit 8639847

Browse files
committed
feat(platform): add echarts route
1 parent 5b134bc commit 8639847

File tree

16 files changed

+4367
-127
lines changed

16 files changed

+4367
-127
lines changed

packages/platform/src/app/Routes.tsx

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import AppLayout from './routes/layout/Layout';
1818
import AppLoginRoute from './routes/login/Login';
1919

2020
const AppAMapRoute = React.lazy(() => import('./routes/dashboard/AMap'));
21-
const AppEChartsRoute = React.lazy(() => import('./routes/dashboard/ECharts'));
21+
const AppEChartsRoute = React.lazy(() => import('./routes/dashboard/echarts/ECharts'));
2222

2323
const AppACLRoute = React.lazy(() => import('./routes/test/acl/ACL'));
2424
const AppHttpRoute = React.lazy(() => import('./routes/test/http/Http'));
@@ -105,19 +105,6 @@ export function AppRoutes() {
105105
canActivate: [ACLGuard],
106106
},
107107
},
108-
{
109-
path: 'amap/:ssssss',
110-
element: (
111-
<React.Suspense fallback={<AppFCPLoader />}>
112-
<AppAMapRoute />
113-
</React.Suspense>
114-
),
115-
data: {
116-
titleI18n: 'dashboard.amap',
117-
acl: ROUTES_ACL.dashboard.amap,
118-
canActivate: [ACLGuard],
119-
},
120-
},
121108
{
122109
path: 'echarts',
123110
element: (
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import type { AppTheme } from '../../App';
2+
3+
import * as echarts from 'echarts';
4+
import { cloneDeep, merge } from 'lodash';
5+
import React, { useCallback, useEffect, useImperativeHandle, useRef, useState } from 'react';
6+
7+
import { useResize, useStorage } from '@react-devui/hooks';
8+
import { getClassName } from '@react-devui/utils';
9+
10+
import { STORAGE_KEY } from '../../../config/storage';
11+
import chartTheme from './theme.json';
12+
13+
echarts.registerTheme('light', chartTheme.light);
14+
echarts.registerTheme('dark', merge(cloneDeep(chartTheme.light), chartTheme.dark));
15+
16+
export interface AppChartProps<O extends echarts.EChartsOption> extends Omit<React.HTMLAttributes<HTMLDivElement>, 'children'> {
17+
aOption: O | null;
18+
}
19+
20+
function Chart<O extends echarts.EChartsOption>(props: AppChartProps<O>, ref: React.ForwardedRef<echarts.ECharts>): JSX.Element | null {
21+
const {
22+
aOption,
23+
24+
...restProps
25+
} = props;
26+
27+
//#region Ref
28+
const elRef = useRef<HTMLDivElement>(null);
29+
//#endregion
30+
31+
const themeStorage = useStorage<AppTheme>(...STORAGE_KEY.theme);
32+
33+
const [instance, setInstance] = useState<echarts.ECharts | null>(null);
34+
const containerRef = useCallback(
35+
(el: HTMLElement | null) => {
36+
setInstance((draft) => {
37+
draft?.dispose();
38+
return el ? echarts.init(el, themeStorage.value, { renderer: 'svg' }) : null;
39+
});
40+
},
41+
[themeStorage.value]
42+
);
43+
44+
useEffect(() => {
45+
if (instance && aOption) {
46+
instance.setOption(aOption);
47+
}
48+
});
49+
50+
useResize(elRef, () => {
51+
if (instance) {
52+
instance.resize();
53+
}
54+
});
55+
56+
useImperativeHandle<echarts.ECharts | null, echarts.ECharts | null>(ref, () => instance, [instance]);
57+
58+
return (
59+
<div {...restProps} ref={elRef} className={getClassName(restProps.className, 'app-chart')}>
60+
<div ref={containerRef} className="app-chart__container"></div>
61+
</div>
62+
);
63+
}
64+
65+
export const AppChart = React.forwardRef(Chart);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './Chart';

0 commit comments

Comments
 (0)