第三方组件
ECharts
提取组件
- 针对图表的挂载操作进行封装,并提取到npm源中
- 由于使用全量Echarts组件,打包后,相关文件将达到700k以上;所以将注册Echarts组件部分提取出来,由使用者按需,决定引入哪些组件,经测试,当只使用柱状图时,打包后相关文件仅200k大小
安装依赖
pnpm i echarts@5.4.0 @cpit-cpvf/libs
添加强制加载
vite.config.ts
ts
optimizeDeps: {
include: [
'echarts',
'echarts/core',
'echarts/charts',
'echarts/components',
'echarts/renderers',
]
}
6.2. 使用
- 按需引用echarts
ts
import * as echartsComponent from 'echarts/core';
import {
BarChart,
LineChart,
PieChart,
// MapChart,
PictorialBarChart,
// RadarChart,
} from 'echarts/charts';
import {
TitleComponent,
TooltipComponent,
GridComponent,
// PolarComponent,
// AriaComponent,
// ParallelComponent,
LegendComponent,
// RadarComponent,
// ToolboxComponent,
// DataZoomComponent,
// VisualMapComponent,
// TimelineComponent,
// CalendarComponent,
// GraphicComponent,
} from 'echarts/components';
import { SVGRenderer } from 'echarts/renderers';
const customEchart = [
PieChart,
TitleComponent,
TooltipComponent,
LegendComponent,
GridComponent,
BarChart,
LineChart,
SVGRenderer,
PictorialBarChart,
]; // 按需加载Echarts
const allEcharts = [
// LegendComponent,
// TitleComponent,
// TooltipComponent,
// GridComponent,
// PolarComponent,
// AriaComponent,
// ParallelComponent,
// BarChart,
// LineChart,
// PieChart,
// MapChart,
// RadarChart,
// SVGRenderer,
// PictorialBarChart,
// RadarComponent,
// ToolboxComponent,
// DataZoomComponent,
// VisualMapComponent,
// TimelineComponent,
// CalendarComponent,
// GraphicComponent,
];
if (customEchart.length > 0) {
echartsComponent.use(customEchart);
} else {
echartsComponent.use(allEcharts);
}
export { echartsComponent };
- 使用
import { useECharts } from '@cpit-cpvf/libs'
vue
<template>
<div ref="chartRef" :style="{ height, width }"></div>
</template>
<script lang="ts">
import { defineComponent, PropType, ref, Ref, onMounted } from 'vue';
import { useECharts } from '@cpvf/libs';
import { echartsComponent } from '/@/views/demo/utils/echarts';
const getLineData = (() => {
const category: any[] = [];
let dottedBase = +new Date();
const lineData: any[] = [];
const barData: any[] = [];
for (let i = 0; i < 20; i++) {
const date = new Date((dottedBase += 1000 * 3600 * 24));
category.push([date.getFullYear(), date.getMonth() + 1, date.getDate()].join('-'));
const b = Math.random() * 200;
const d = Math.random() * 200;
barData.push(b);
lineData.push(d + b);
}
return { barData, category, lineData };
})();
const { barData, lineData, category } = getLineData;
const height = ref(100)
const width = ref(100)
// 绑定ref
const chartRef = ref<HTMLDivElement | null>(null);
// 为chartRef配置 echarts
const { setOptions, echarts } = useECharts(
chartRef as Ref<HTMLDivElement>,
echartsComponent,
);
// 为echarts挂载数据,以下为正常使用echarts配置
onMounted(() => {
setOptions({
backgroundColor: '#0f375f',
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow',
label: {
show: true,
backgroundColor: '#333',
},
},
},
legend: {
data: ['line', 'bar'],
textStyle: {
color: '#ccc',
},
},
xAxis: {
data: category,
axisLine: {
lineStyle: {
color: '#ccc',
},
},
},
yAxis: {
splitLine: { show: false },
axisLine: {
lineStyle: {
color: '#ccc',
},
},
},
series: [
{
name: 'line',
type: 'line',
smooth: true,
showAllSymbol: 'auto',
symbol: 'emptyCircle',
symbolSize: 15,
data: lineData,
},
{
name: 'bar',
type: 'bar',
barWidth: 10,
itemStyle: {
borderRadius: 5,
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: '#14c8d4' },
{ offset: 1, color: '#43eec6' },
]),
},
data: barData,
},
{
name: 'line',
type: 'bar',
barGap: '-100%',
barWidth: 10,
itemStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: 'rgba(20,200,212,0.5)' },
{ offset: 0.2, color: 'rgba(20,200,212,0.2)' },
{ offset: 1, color: 'rgba(20,200,212,0)' },
]),
},
z: -12,
data: lineData,
},
{
name: 'dotted',
type: 'pictorialBar',
symbol: 'rect',
itemStyle: {
color: '#0f375f',
},
symbolRepeat: true,
symbolSize: [12, 4],
symbolMargin: 1,
z: -10,
data: lineData,
},
],
});
});
</script>
使用示例
- 见:示例工程目录
src/views/demo/pages/charts
使用经验
见头部导航: 使用经验 - 图表(Echarts)