第 3 章:单页面多个 ECharts 图表共存(柱状图 + 折线图)

第 3 章:单页面多个 ECharts 图表共存(柱状图 + 折线图)

本章目标:在同一个页面创建 2 张独立图表,复用useEcharts。

核心要点:多次调用 useEcharts (),每次调用都会生成独立的 ref 和图表实例,互不干扰。

完整页面代码 chartDemo.vue

复制代码
<template>
  <div class="chart-container">
    <!-- 第一个图表:柱状图 -->
    <el-button type="primary" @click="changeBarData">切换柱状图数据</el-button>
    <div
      ref="barChartRef"
      style="
        width: 100%;
        height: 320px;
        border: 1px solid #eee;
        margin-top: 15px;
      "
    ></div>

    <!-- 第二个图表:折线图 -->
    <el-button type="success" @click="changeLineData" style="margin-top:20px;">切换折线图数据</el-button>
    <div
      ref="lineChartRef"
      style="
        width: 100%;
        height: 320px;
        border: 1px solid #eee;
        margin-top: 15px;
      "
    ></div>
  </div>
</template>

<script setup>
import { onMounted } from 'vue'
import { useEcharts } from '@/composables/echartHelper'

// 🔥多次调用useEcharts,解构重命名,生成两套独立实例
const { chartRef: barChartRef, setChartOption: setBarOption } = useEcharts()
const { chartRef: lineChartRef, setChartOption: setLineOption } = useEcharts()

// ========== 柱状图配置 ==========
const barOptionA = {
  title: {
    text: '销售柱状图',
    left: 'center'
  },
  xAxis: {
    type: 'category',
    data: ['周一', '周二', '周三', '周四', '周五']
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      type: 'bar',
      data: [10, 22, 18, 25, 16]
    }
  ]
}
const barOptionB = {
  title: {
    text: '销售柱状图',
    left: 'center'
  },
  xAxis: {
    type: 'category',
    data: ['周一', '周二', '周三', '周四', '周五']
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      type: 'bar',
      data: [28, 12, 24, 15, 21]
    }
  ]
}
let currentBarOption = barOptionA

// ========== 折线图配置 ==========
const lineOptionA = {
  title: {
    text: '访问量折线图',
    left: 'center'
  },
  xAxis: {
    type: 'category',
    data: ['周一', '周二', '周三', '周四', '周五']
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      type: 'line',
      smooth: true,
      data: [8, 14, 10, 19, 16]
    }
  ]
}
const lineOptionB = {
  title: {
    text: '访问量折线图',
    left: 'center'
  },
  xAxis: {
    type: 'category',
    data: ['周一', '周二', '周三', '周四', '周五']
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      type: 'line',
      smooth: true,
      data: [22, 15, 28, 13, 20]
    }
  ]
}
let currentLineOption = lineOptionA

// 页面挂载完成,初始化两张图表
onMounted(() => {
  setBarOption(currentBarOption)
  setLineOption(currentLineOption)
})

// 切换柱状图
const changeBarData = () => {
  currentBarOption = currentBarOption === barOptionA ? barOptionB : barOptionA
  setBarOption(currentBarOption)
}

// 切换折线图
const changeLineData = () => {
  currentLineOption = currentLineOption === lineOptionA ? lineOptionB : lineOptionA
  setLineOption(currentLineOption)
}
</script>

第 3 章 测试清单

  1. 替换chartDemo.vue全部代码,保存文件
  2. 刷新页面
  3. 逐项测试:
    1. 页面加载同时展示柱状图、折线图两张图表
    2. 点击【切换柱状图数据】,仅柱状图变化,折线图不动
    3. 点击【切换折线图数据】,仅折线图变化,柱状图不动
    4. 拉伸浏览器窗口,两张图表都自适应缩放
    5. 在若依菜单来回切换页面,控制台无报错,实例正常销毁
相关推荐
雪芽蓝域zzs19 天前
第26章:ECharts 自定义主题 + 页面一键切换主题(深色 / 浅色两套)
vue.js·echars
雪芽蓝域zzs19 天前
第22章:ECharts 图表联动(多图表交互联动)
vue.js·echars
雪芽蓝域zzs19 天前
第18章:ECharts 折线图 + 标记点、标记线,自定义 tooltip
vue.js·echars
雪芽蓝域zzs19 天前
第24章:ECharts 地图组件(基础行政区地图,若依 Vue3)
vue.js·echars
雪芽蓝域zzs19 天前
第25章:地图下钻交互(点击省份,切换到对应省份城市地图)
vue.js·echars
雪芽蓝域zzs19 天前
第19章:ECharts 组合图(折线 + 柱状混合图,双 Y 轴)
vue.js·echars
雪芽蓝域zzs19 天前
第17章:ECharts 饼图自定义样式(扇区颜色、轮廓、标签配置 + 空状态兼容)
vue.js·echars
雪芽蓝域zzs19 天前
第15章:ECharts 图表事件交互(若依 Vue3)
vue.js·echars
雪芽蓝域zzs19 天前
第20章:ECharts 图表主题切换 + 自定义颜色渐变
vue.js·echars
雪芽蓝域zzs20 天前
第 7 章:双 Y 轴组合图(柱状 + 折线,看板高频场景)
vue.js·echars