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; // 如果没有找到
},
相关推荐
m0_736034853 小时前
1.28笔记
前端·chrome·笔记
IT陈图图3 小时前
构建 Flutter × OpenHarmony 跨端带文本输入对话框示例
开发语言·javascript·flutter
奔跑的web.8 小时前
TypeScript 装饰器入门核心用法
前端·javascript·vue.js·typescript
阿蒙Amon8 小时前
TypeScript学习-第1章:入门
javascript·学习·typescript
winfredzhang8 小时前
实战复盘:如何用 HTML+JS+AI 打造一款“影迹”智能影视管理系统
javascript·html·json·加载·搜索·保存·电影接口
集成显卡8 小时前
Lucide Icons:一套现代、轻量且可定制的 SVG 图标库
前端·ui·图标库·lucide
pas1369 小时前
37-mini-vue 解析插值
前端·javascript·vue.js
十里-10 小时前
vue.js 2前端开发的项目通过electron打包成exe
前端·vue.js·electron
雨季66611 小时前
构建 OpenHarmony 简易文字行数统计器:用字符串分割实现纯文本结构感知
开发语言·前端·javascript·flutter·ui·dart
雨季66611 小时前
Flutter 三端应用实战:OpenHarmony 简易倒序文本查看器开发指南
开发语言·javascript·flutter·ui