当前位置:首页 > VUE

vue实现echarts项目

2026-01-17 01:30:13VUE

vue3 中集成 ECharts

安装 ECharts 依赖包

npm install echarts --save

在组件中引入并初始化

import * as echarts from 'echarts';
import { onMounted, ref } from 'vue';

const chartRef = ref(null);

onMounted(() => {
  const myChart = echarts.init(chartRef.value);
  myChart.setOption({
    title: { text: 'Vue3 ECharts 示例' },
    tooltip: {},
    xAxis: { data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子'] },
    yAxis: {},
    series: [{ name: '销量', type: 'bar', data: [5, 20, 36, 10, 10, 20] }]
  });
});

模板部分

<div ref="chartRef" style="width: 600px; height: 400px;"></div>

响应式处理

监听窗口变化自动调整

import { onUnmounted } from 'vue';

const resizeHandler = () => myChart.resize();
window.addEventListener('resize', resizeHandler);

onUnmounted(() => {
  window.removeEventListener('resize', resizeHandler);
  myChart.dispose();
});

封装可复用组件

创建 EChartsWrapper.vue

vue实现echarts项目

<template>
  <div ref="chartEl" :style="{ width, height }"></div>
</template>

<script setup>
import * as echarts from 'echarts';
import { ref, watch, onMounted, onUnmounted } from 'vue';

const props = defineProps({
  option: Object,
  width: { type: String, default: '100%' },
  height: { type: String, default: '400px' }
});

const chartEl = ref(null);
let chartInstance = null;

const initChart = () => {
  chartInstance = echarts.init(chartEl.value);
  chartInstance.setOption(props.option);
};

onMounted(initChart);
onUnmounted(() => chartInstance?.dispose());

watch(() => props.option, (newVal) => {
  chartInstance?.setOption(newVal);
}, { deep: true });
</script>

主题定制

使用官方主题

import * as echarts from 'echarts';
import 'echarts/theme/dark'; // 引入暗黑主题

const myChart = echarts.init(chartRef.value, 'dark');

自定义主题

  1. 在主题编辑器中设计:https://echarts.apache.org/theme-builder.html
  2. 下载主题文件并导入
    import theme from './customTheme.json';
    echarts.registerTheme('myTheme', theme);
    const myChart = echarts.init(chartRef.value, 'myTheme');

常见图表类型实现

折线图配置示例

vue实现echarts项目

{
  xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] },
  yAxis: { type: 'value' },
  series: [{ data: [820, 932, 901, 934, 1290, 1330, 1320], type: 'line' }]
}

饼图配置示例

{
  series: [{
    type: 'pie',
    data: [
      { value: 1048, name: 'Search Engine' },
      { value: 735, name: 'Direct' },
      { value: 580, name: 'Email' }
    ]
  }]
}

性能优化建议

大数据量使用增量渲染

series: [{
  type: 'line',
  progressive: 1000, // 增量渲染阈值
  progressiveThreshold: 1000
}]

按需引入模块

import * as echarts from 'echarts/core';
import { BarChart, LineChart } from 'echarts/charts';
import { TitleComponent, TooltipComponent } from 'echarts/components';
import { CanvasRenderer } from 'echarts/renderers';

echarts.use([BarChart, LineChart, TitleComponent, TooltipComponent, CanvasRenderer]);

标签: 项目vue
分享给朋友:

相关文章

vue状态管理怎么实现

vue状态管理怎么实现

Vue 状态管理实现方法 使用 Vuex(官方推荐) Vuex 是 Vue 的官方状态管理库,适合中大型应用。 安装 Vuex: npm install vuex --save 创建 Store 示…

vue组件实现

vue组件实现

Vue 组件实现基础 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。组件的实现包括定义、注册、数据传递和生命周期管理等。 定义组件 组件可以通过单文件组件(.…

vue  select实现

vue select实现

Vue Select 实现方法 在 Vue 中实现下拉选择功能可以使用原生 <select> 标签或第三方库如 vue-select。以下是两种方法的详细说明: 原生 HTML Sele…

vue 实现递归

vue 实现递归

Vue 实现递归组件的方法 在 Vue 中实现递归组件通常用于处理树形结构数据,例如菜单、评论列表或文件目录。以下是实现递归组件的关键步骤: 定义递归组件 递归组件需要设置 name 属性,以便在模…

vue实现ide

vue实现ide

Vue 实现 IDE 的基本思路 使用 Vue 构建一个简易的集成开发环境(IDE)需要整合代码编辑器、文件管理、终端模拟等功能。以下是关键实现步骤和推荐工具。 核心功能模块 代码编辑器 使用…

vue实现tabbar

vue实现tabbar

Vue 实现 TabBar 的方法 使用 Vue Router 实现基础 TabBar 安装 Vue Router 依赖: npm install vue-router 创建路由配置文件(route…