ECharts饼图下钻

背景

项目上需要对Echarts饼图进行功能定制,实现点击颜色块,下钻显示下一层级占比

说明

饼图实现点击下钻/面包屑返回的功能

实现

  • 数据结构
js 复制代码
[
  {
    name: 'a',
    value: 1,
    children: [...]
  },
  ...
]
  • 点击下钻
js 复制代码
// 为图表绑定点击事件(需要在destroy前进行事件解绑)
this.myChart.on('click', (e) => {
  if (e.data?.children) {
    this.source = e.data.children // 将子级用于当前图表显示
    this.breadcrumbList.push(e.data.name) // 面包屑添加子级名称
  }
})
  • 面包屑返回
html 复制代码
<!-- 面包屑-显示下钻层级及点击返回 数据结构: [xxx, xxx, ...] -->
<row class="breadcrumb" v-if="drillAble">
  <span
    class="clickable flex"
    v-for="(item, index) in breadcrumbList"
    :key="index"
    @click="handleCrumbClick(index)"
  >
    <OverflowTip style="max-width: 200px" :content="item">
      {{ item }}
    </OverflowTip>
    <span class="px-5" v-if="index < breadcrumbList.length - 1"> / </span>
  </span>
</row>
js 复制代码
handleCrumbClick(index) { // index为点击面包屑的层级
  if (index === this.breadcrumbList.length - 1) return // 点击当前层级 不做任何操作
  this.breadcrumbList.splice(index + 1) // 删除下级面包屑
  if (index === 0) {
    this.source = this.originData // 根级别 直接赋值
  } else {
    const parentName = this.breadcrumbList[index]
    const parentData = this.findParentData(this.originData, parentName) // 递归查找
    this.source = parentData?.children || []
  }
},
findParentData(data, name) {
  for (const item of data) {
    if (item.name === name) {
      return item; // 找到对应的父级节点
    }
    if (item.children) {
      const result = this.findParentData(item.children, name);
      if (result) {
        return result; // 在子级中递归查找
      }
    }
  }
  return null; // 如果没有找到
},
相关推荐
小程故事多_8026 分钟前
破局 LLM 黑盒困局,Phoenix 凭全链路可观测,重构大模型应用工程化落地规则
java·前端·人工智能·重构·aigc
紫微AI2 小时前
WebMCP:开启 Agentic Web 新时代——Chrome 新 API 的特性与前瞻
前端·chrome
恋猫de小郭8 小时前
AGENTS.md 真的对 AI Coding 有用吗?或许在此之前你没用对?
前端·人工智能·ai编程
sunny_10 小时前
构建工具的第三次革命:从 Rollup 到 Rust Bundler,我是如何设计 robuild 的
前端·rust·前端工程化
rfidunion11 小时前
springboot+VUE+部署(12。Nginx和前端配置遇到的问题)
前端·vue.js·spring boot
vx-Biye_Design11 小时前
servlet家政公司管理系统-计算机毕业设计源码01438
java·vue.js·spring·servlet·tomcat·maven·mybatis
珹洺11 小时前
Java-servlet(五)手把手教你利用Servlet配置HTML请求与相应
java·运维·服务器·前端·servlet·html·maven
FYKJ_201011 小时前
springboot大学校园论坛管理系统--附源码42669
java·javascript·spring boot·python·spark·django·php
QQ243919711 小时前
语言在线考试与学习交流网页平台信息管理系统源码-SpringBoot后端+Vue前端+MySQL【可直接运行】
前端·spring boot·sql·学习·java-ee
范特西.i12 小时前
QT聊天项目(6)
前端