echarts 饼图

1. 饼图移入修改相关样式以及鼠标事件

1.1 代码

<template>
  <div class="pie-container">
    <div ref="pieChart" class="chart" />
  </div>
</template>

<script>
export default {
  data() {
    return {
      echartInstance: null,
      activeNumIndex: 0,
      numList: [
        { label: '苹果', value: 10 },
        { label: '香蕉', value: 20 },
        { label: '蜜桃', value: 30 },
        { label: '雪梨', value: 40 }
      ]
    }
  },
  mounted() {
    this.initEchart()
  },
  methods: {
    initEchart() {
      this.echartInstance = this.$echarts.init(this.$refs.pieChart)
      window.onresize = () => {
        this.echartInstance.resize()
      }

      const options = this.initOptions()
      this.echartInstance.setOption(options)

      this.echartInstance.on('mousemove', param => {
        const { name } = param

        const index = this.numList.findIndex(n => n.label === name)

        if (this.activeNumIndex === index) return

        // 点击环时, 取消上一次的点击
        this.echartInstance.dispatchAction({
          type: 'downplay',
          seriesIndex: 0,
          dataIndex: this.activeNumIndex
        })
        this.handleDataStyle(index, false, true)

        this.activeNumIndex = index
      })

      this.echartInstance.getZr().on('mouseout', param => {
        const target = param.event.target
        // 取消激活
        if (target && target.tagName === 'CANVAS') {
          this.echartInstance.dispatchAction({
            type: 'downplay',
            seriesIndex: 0,
            dataIndex: this.activeNumIndex
          })

          this.activeNumIndex = -1
          this.handleDataStyle(this.activeNumIndex, true)
        }
      })

      // 监听legend的点击事件
      this.echartInstance.on('legendselectchanged', param => {
        const { name, selected } = param
        if (!selected[name]) {
          this.resetEcharts()
        } else {
          const index = this.numList.findIndex(n => n.label === name)
          this.activeNumIndex = index
          this.handleDataStyle(this.activeNumIndex)
        }
      })

      this.echartInstance.getZr().on('click', param => {
        const { target } = param
        // 点击的外部区域
        if (!target) {
          this.resetEcharts()
        }
      })
    },
    initOptions() {
      return {
        color: ['#FCB466', '#FF7384', '#6BB8E1', '#647CF9'],
        title: {
          subtext: '单位:个',
          top: 10,
          left: '60%',
          subtextStyle: {
            color: '#8C8C8C'
          }
        },
        tooltip: {
          trigger: 'item',
          padding: [6, 8],
          textStyle: {
            color: '#262626',
            lineHeight: 20
          },
          backgroundColor: '#fff',
          formatter: (param) => {
            const { data, marker, dataIndex } = param
            return `<div class="pie-tooltip">
              <p>
                ${marker}
                <span>${data.name}</span>
                <span>${data.value}个</span>
              </p>
            </div>`
          }
        },
        legend: {
          orient: 'vertical',
          selectedMode: false, // 控制是否可以通过点击图例改变系列的显示状态
          top: 40,
          left: '60%',
          icon: 'rect',
          itemGap: 14,
          itemWidth: 10,
          itemHeight: 10,
          textStyle: {
            lineHeight: 17,
            color: '#595959',
            padding: [0, 0, -2, 0]
          },
          formatter: (name) => {
            const i = this.numList.findIndex(n => n.label === name)
            return [`${name}`, this.numList[i].value].join('   ')
          }
        },
        series: [
          {
            type: 'pie',
            center: ['30%', '44%'],
            radius: ['60%', '78%'],
            itemStyle: {
              borderColor: '#fff',
              borderWidth: 2
            },
            label: {
              show: true,
              position: 'center',
              color: '#262626',
              formatter: [
                `{a|100}个`,
                '水果数'
              ].join('\n'),
              rich: {
                a: {
                  fontSize: 18,
                  color: '#262626',
                  lineHeight: 27
                }
              }
            },
            data: [
              { value: this.numList[0].value, name: this.numList[0].label },
              { value: this.numList[1].value, name: this.numList[1].label },
              { value: this.numList[2].value, name: this.numList[2].label },
              { value: this.numList[3].value, name: this.numList[3].label }
            ]
          }
        ]
      }
    },
    /**
     * 处理echarts 样式
     * @param {number} i 点击下标
     * @param {boolean} isInit 是否样式初始化
     * @param {boolean} isPie 是否点击的环
     */
    handleDataStyle(i, isInit = false, isPie = false) {
      const options = this.echartInstance.getOption()
      options.series[0].data.forEach((item, index) => {
        item.itemStyle = {
          opacity: (index === i || isInit) ? 1 : 0.5
        }
      })

      const labelValue = isInit ? 100 : this.numList[i].value
      options.series[0].label.formatter = [
        `{a|${labelValue}}个`,
        '水果数'
      ].join('\n')

      this.echartInstance.setOption(options)

      if (!isInit && !isPie) {
        setTimeout(() => {
          this.echartInstance.dispatchAction({
            type: 'showTip',
            seriesIndex: 0,
            dataIndex: i
          })
        }, 10)
      }
    }
  }
}
</script>

<style scoped lang="scss">
  .pie-container {
    > .chart {
      width: 400px;
      height: 190px;
    }
  }
</style>

1.2 效果图

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,657评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,889评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,057评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,509评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,562评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,443评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,251评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,129评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,561评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,779评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,902评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,621评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,220评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,838评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,971评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,025评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,843评论 2 354

推荐阅读更多精彩内容

  • 开头 饼图,很常见的一种图表,使用任何一个图表库都能轻松的渲染出来,但是,我司的交互想法千奇百怪,布局捉摸不透,本...
    街角小林2阅读 249评论 0 0
  • 配置属性 基本属性 id:组件id,默认不指定name:系列名称type:series图表类型,常用值:bar(柱...
    李槿槿阅读 13,621评论 0 1
  • 一、饼图实例 1、代码如下: (1) legend: 图例组件。图例组件展现了不同系列的标记(symbol),颜色...
    神奇作手阅读 9,321评论 0 1
  • ECharts 提供了丰富的自定义配置选项,并且能够从全局、系列、数据三个层级去设置数据图形的样式。下面我们来看如...
    飞扬code阅读 671评论 0 1
  • 先看一下效果图: 实现思路: 除了本来要绘制的饼图以外,再绘制两个透明的饼图(一个用来将标志线与饼图分隔开,另一个...
    听见雨山阅读 8,628评论 0 2