当前位置:首页 > 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

<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');

常见图表类型实现

折线图配置示例

{
  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实现echarts项目

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

相关文章

vue 表格实现

vue 表格实现

Vue 表格实现方法 在 Vue 中实现表格功能可以通过多种方式完成,以下介绍几种常见的方法: 使用原生 HTML 表格 最简单的表格可以通过 Vue 模板中的原生 HTML 表格元素实现: &l…

vue实现密码

vue实现密码

Vue 密码输入组件实现 基础密码输入框实现 使用 Vue 的 v-model 指令绑定数据,并通过 type="password" 设置输入类型为密码: <template>…

vue前端实现注册

vue前端实现注册

使用 Vue 实现用户注册功能 环境准备 确保已安装 Vue CLI 或通过 CDN 引入 Vue。若使用 Vue CLI,需安装 axios 或其他 HTTP 库用于后端通信。 注册表单组件 创…

在线脑图 vue 实现

在线脑图 vue 实现

在线脑图 Vue 实现方案 使用开源库实现 推荐使用 mind-elixir 或 jsmind 这类开源库,它们专为脑图设计且支持 Vue 集成。 安装依赖: npm install mind…

vue实现冰墩墩

vue实现冰墩墩

使用 Vue 实现冰墩墩动画 通过 Vue 的动画特性和 SVG 技术,可以动态渲染冰墩墩的形象。以下是具体实现方法: 准备 SVG 素材 冰墩墩的形象通常由多个 SVG 路径组成,包括身体轮廓…

vue实现弹窗可切换

vue实现弹窗可切换

实现弹窗可切换的 Vue 方案 动态组件切换 通过 Vue 的 <component :is="currentComponent"> 动态加载不同弹窗组件,结合 v-if 控制显示状态。…