当前位置: 首页 > news >正文

Vue 3项目中引用ECharts并设计多种图表组件的实现方案

以下是一个完整的Vue 3项目中引用ECharts并设计多种图表组件的实现方案:

安装依赖

npm install echarts vue-echarts

基础封装组件

components/ECharts.vue中创建基础封装:

<template><div ref="chartRef" :style="{ width, height }"></div>
</template><script setup>
import { ref, onMounted, onBeforeUnmount, watch } from 'vue';
import * as echarts from 'echarts';const props = defineProps({options: Object,width: { type: String, default: '100%' },height: { type: String, default: '400px' }
});const chartRef = ref(null);
let chartInstance = null;const initChart = () => {if (chartRef.value) {chartInstance = echarts.init(chartRef.value);props.options && chartInstance.setOption(props.options);}
};const resizeChart = () => {chartInstance?.resize();
};onMounted(() => {initChart();window.addEventListener('resize', resizeChart);
});onBeforeUnmount(() => {window.removeEventListener('resize', resizeChart);chartInstance?.dispose();
});watch(() => props.options, (newVal) => {chartInstance?.setOption(newVal);
}, { deep: true });
</script>

柱状图组件

components/BarChart.vue中:

<template><ECharts :options="chartOptions" :width="width" :height="height" />
</template><script setup>
import ECharts from './ECharts.vue';const props = defineProps({data: Array,xAxis: Array,width: { type: String, default: '100%' },height: { type: String, default: '400px' }
});const chartOptions = {tooltip: {trigger: 'axis'},xAxis: {type: 'category',data: props.xAxis},yAxis: {type: 'value'},series: [{data: props.data,type: 'bar',showBackground: true,backgroundStyle: {color: 'rgba(180, 180, 180, 0.2)'}}]
};
</script>

折线图组件

components/LineChart.vue中:

<template><ECharts :options="chartOptions" :width="width" :height="height" />
</template><script setup>
import ECharts from './ECharts.vue';const props = defineProps({series: Array,xAxis: Array,width: { type: String, default: '100%' },height: { type: String, default: '400px' }
});const chartOptions = {tooltip: {trigger: 'axis'},legend: {data: props.series.map(item => item.name)},xAxis: {type: 'category',data: props.xAxis},yAxis: {type: 'value'},series: props.series.map(item => ({...item,type: 'line',smooth: true}))
};
</script>

饼图组件

components/PieChart.vue中:

<template><ECharts :options="chartOptions" :width="width" :height="height" />
</template><script setup>
import ECharts from './ECharts.vue';const props = defineProps({data: Array,width: { type: String, default: '100%' },height: { type: String, default: '400px' }
});const chartOptions = {tooltip: {trigger: 'item'},series: [{name: '占比',type: 'pie',radius: ['40%', '70%'],avoidLabelOverlap: false,itemStyle: {borderRadius: 10,borderColor: '#fff',borderWidth: 2},label: {show: false,position: 'center'},emphasis: {label: {show: true,fontSize: '18',fontWeight: 'bold'}},data: props.data}]
};
</script>

使用示例

在页面中使用这些组件:

<template><div class="chart-container"><BarChart :data="barData" :xAxis="barXAxis" height="300px" /><LineChart :series="lineSeries" :xAxis="lineXAxis" height="300px" /><PieChart :data="pieData" height="300px" /></div>
</template><script setup>
import BarChart from '@/components/BarChart.vue';
import LineChart from '@/components/LineChart.vue';
import PieChart from '@/components/PieChart.vue';const barData = [120, 200, 150, 80, 70, 110, 130];
const barXAxis = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];const lineSeries = [{ name: 'Email', data: [120, 132, 101, 134, 90, 230, 210] },{ name: 'Union Ads', data: [220, 182, 191, 234, 290, 330, 310] }
];
const lineXAxis = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];const pieData = [{ value: 1048, name: 'Search Engine' },{ value: 735, name: 'Direct' },{ value: 580, name: 'Email' }
];
</script><style scoped>
.chart-container {display: grid;gap: 20px;padding: 20px;
}
</style>

高级功能扩展

在基础组件中添加主题和自定义功能:

<script setup>
// 在ECharts.vue中扩展
import { ref, onMounted, watch } from 'vue';
import * as echarts from 'echarts';const props = defineProps({// ...原有propstheme: { type: String, default: '' },initOptions: Object,loading: Boolean,loadingOptions: Object
});// 初始化时添加主题
const initChart = () => {if (chartRef.value) {chartInstance = echarts.init(chartRef.value,props.theme,props.initOptions);props.options && chartInstance.setOption(props.options);props.loading && chartInstance.showLoading(props.loadingOptions);}
};// 添加loading状态监听
watch(() => props.loading, (val) => {if (chartInstance) {val ? chartInstance.showLoading(props.loadingOptions) :chartInstance.hideLoading();}
});
</script>

注意事项

  1. 所有图表组件都需要响应式容器,确保父容器有明确尺寸
  2. 大数据量时建议开启dataZoom或使用large模式
  3. 动态更新数据时,建议使用notMerge: false参数保持平滑过渡
  4. 多图表页面建议使用resize-observer-polyfill处理容器尺寸变化

以上方案提供了Vue 3中ECharts的完整实现路径,从基础封装到具体图表组件的开发,可根据实际需求进一步扩展功能。


文章转载自:

http://VORgtUpk.jfbbq.cn
http://mvhfHH3z.jfbbq.cn
http://wvrwYg56.jfbbq.cn
http://hvWAyMLO.jfbbq.cn
http://0T8Xx3DF.jfbbq.cn
http://xs9xTtzj.jfbbq.cn
http://55OMIWnn.jfbbq.cn
http://ILZcJNZG.jfbbq.cn
http://zFOfjH7a.jfbbq.cn
http://p3xDTTes.jfbbq.cn
http://ZEXWwR8B.jfbbq.cn
http://ROpjTxxg.jfbbq.cn
http://KdnePuwY.jfbbq.cn
http://j0cVnlSm.jfbbq.cn
http://HmmosDmh.jfbbq.cn
http://602jvgHi.jfbbq.cn
http://J1MTE5af.jfbbq.cn
http://R1TamDOl.jfbbq.cn
http://JOWEQRzA.jfbbq.cn
http://VYGHMqwG.jfbbq.cn
http://W8NuZliA.jfbbq.cn
http://cCVcIasd.jfbbq.cn
http://sXdnh0Au.jfbbq.cn
http://mEjaYM06.jfbbq.cn
http://wVgU5VMm.jfbbq.cn
http://0MCXOGRf.jfbbq.cn
http://j1GUAETR.jfbbq.cn
http://6YS70K9p.jfbbq.cn
http://Bpcdsd3w.jfbbq.cn
http://651IjmFY.jfbbq.cn
http://www.dtcms.com/a/369862.html

相关文章:

  • Day37 MQTT协议 多客户端服务器模型
  • 【UAV】基于PX4+Ubuntu24.04.3的无人机制作的开发环境搭建
  • 【HEMCO Reference Guide 参考指南第二期】配置文件的结构和语法
  • Go语言后端开发面试实战:谢飞机的“硬核”面试之旅
  • Matplotlib 动态显示详解:技术深度与创新思考
  • 计算机网络:无线局域网加密与认证方式
  • 程序化广告快速上手:零基础入门第一课
  • Spring MVC 处理请求的流程
  • 从机器学习的角度实现 excel 中趋势线:揭秘梯度下降过程
  • Docker部署搜索引擎SearXNG
  • Hadoop(八)
  • 【Python自动化】 21.2 Pandas 读取 Excel 时的 dtype 参数完全指南
  • 【面板数据】各省制造业出口技术复杂度数据集(2010-2023年)
  • 使用 YAML 自动化 Azure DevOps 管道
  • 【数据库相关】TxSQL新增数据库节点步骤
  • 理想汽车智驾方案介绍 4 World model + 强化学习重建自动驾驶交互环境
  • 大语言模型预训练数据采集与清洗技术实践:从语料到知识库的全流程优化
  • 腾讯混元翻译模型Hunyuan-MT-7B开源,先前拿了30个冠军
  • MiniDrive:面向自动驾驶的更高效的视觉语言模型
  • 2025年渗透测试面试题总结-54(题目+回答)
  • 《Kubernetes 构建 MySQL MGR 集群实战教程》
  • 创建阿里云ECS实例操作(免费试用版)
  • 【数学建模】质量消光系数在烟幕遮蔽效能建模中的核心作用
  • 小孔成像原理
  • 操作系统基本概念.1
  • Jupyter Notebook与cpolar:构建跨地域数据科学协作平台
  • 山西移动九联UNT413HS-海思MV320-2+8G-原机全量备份包
  • AI热点周报(8.31~9.6): Qwen3‑Max‑Preview上线、GLM-4.5提供一键迁移、Gemini for Home,AI风向何在?
  • 【C++】C++11的可变参数模板、emplace接口、类的新功能
  • [特殊字符] 从零到一:打造你的VSCode圈复杂度分析插件