Skip to content

Commit

Permalink
feat: echarts按需引入
Browse files Browse the repository at this point in the history
  • Loading branch information
shi-gui committed Jul 4, 2023
1 parent af51396 commit dd4f54a
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"antd": "^5.6.0",
"axios": "^1.4.0",
"dayjs": "^1.11.8",
"echarts": "^5.4.2",
"i18next": "^23.2.3",
"js-cookie": "^3.0.5",
"mobx": "^6.9.0",
Expand Down
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions src/libs/echarts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* @Author: liuhua
* @Date: 2023-07-04 10:10:20
* @LastEditors: liuhua
* @LastEditTime: 2023-07-04 10:46:58
* @Description: echarts按需引入
*/

import * as echarts from 'echarts/core';
import { BarChart, LineChart } from 'echarts/charts';
import {
TitleComponent,
TooltipComponent,
GridComponent,
// 数据集组件
DatasetComponent,
// 内置数据转换器组件 (filter, sort)
TransformComponent
} from 'echarts/components';
import { LabelLayout, UniversalTransition } from 'echarts/features';
import { CanvasRenderer } from 'echarts/renderers';
import type {
// 系列类型的定义后缀都为 SeriesOption
BarSeriesOption,
LineSeriesOption
} from 'echarts/charts';
import type {
// 组件类型的定义后缀都为 ComponentOption
TitleComponentOption,
TooltipComponentOption,
GridComponentOption,
DatasetComponentOption
} from 'echarts/components';
import type { ComposeOption } from 'echarts/core';

// 通过 ComposeOption 来组合出一个只有必须组件和图表的 Option 类型
export type ECOption = ComposeOption<
| BarSeriesOption
| LineSeriesOption
| TitleComponentOption
| TooltipComponentOption
| GridComponentOption
| DatasetComponentOption
>;

// 注册必须的组件
echarts.use([
TitleComponent,
TooltipComponent,
GridComponent,
DatasetComponent,
TransformComponent,
BarChart,
LineChart,
LabelLayout,
UniversalTransition,
CanvasRenderer
]);
export default echarts;
33 changes: 32 additions & 1 deletion src/views/home/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,45 @@
import { observer } from 'mobx-react';
import { useRef, useEffect } from 'react';
import { DatePicker, type DatePickerProps } from 'antd';
import { observer } from 'mobx-react';
import echarts, { type ECOption } from '@/libs/echarts';
import rootStore from '@/store';

function Home() {
const { theme } = rootStore;
const onChange: DatePickerProps['onChange'] = (date, dateString) => {
console.log(date, dateString);
};

const chartRef = useRef(null);
useEffect(() => {
if (chartRef.current) {
const chart = echarts.init(chartRef.current);
const options: ECOption = {
color: theme.primaryColor,
title: {
text: 'ECharts 入门示例'
},
tooltip: {},
xAxis: {
data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
},
yAxis: {},
series: [
{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}
]
};
chart.setOption(options);
}
}, [theme.primaryColor]);

return (
<>
<DatePicker onChange={onChange} />
<div ref={chartRef} className="w-[500px] h-[300px]" />
</>
);
}
Expand Down

0 comments on commit dd4f54a

Please sign in to comment.