echarts 饼图默认高亮,选中任意选项离开后再次高亮,中心区显示选中的数据

 

主要方法:

 // 高亮
    setHighLight(id) {
      let myChart = this.$echarts.init(document.getElementById(id))
      //设置默认选中高亮部分
      // 高亮指定的数据图形。
      // 通过seriesName或者seriesIndex指定系列。如果要再指定某个数据可以再指定dataIndex或者name。
      let lastIndex = 0//最后一次选中的dataIndex
      //默认显示的内容dataIndex是第一个0
      myChart.dispatchAction({
        type: 'highlight',
        seriesIndex: 0,
        dataIndex: 0
      })

      myChart.on('mouseover', (v) => { //清空第一次选中
        // console.log('鼠标移上去的index:')
        // console.log(v.dataIndex)

        lastIndex = v.dataIndex

        //鼠标移上去之后,如果选中的是0就高亮,其他的互斥
        if (lastIndex===0) {
          myChart.dispatchAction({
            type: 'highlight',
            seriesIndex: 0,
            dataIndex: 0
          })
          myChart.dispatchAction({
            type: 'downplay',
            seriesIndex: 0,
            dataIndex: 1
          })
        }
        if (lastIndex===1){
          myChart.dispatchAction({
            type: 'highlight',
            seriesIndex: 0,
            dataIndex: 1
          })
          myChart.dispatchAction({
            type: 'downplay',
            seriesIndex: 0,
            dataIndex: 0
          })
        }

      })
      //鼠标移除后,如果选中的是0就继续高亮,其他的互斥
      myChart.on('mouseout', (v) => { //鼠标离开圆环默认选第一次
        // console.log('鼠标移走后的index:')
        // console.log(v.dataIndex)
        lastIndex = v.dataIndex//
        //如果选中的是其他index就高亮
        if (lastIndex===0){
          myChart.dispatchAction({
            type: 'highlight',
            seriesIndex: 0,
            dataIndex: 0
          })
          myChart.dispatchAction({
            type: 'downplay',
            seriesIndex: 0,
            dataIndex:1
          })
        }

        if (lastIndex===1) {
          myChart.dispatchAction({
            type: 'highlight',
            seriesIndex: 0,
            dataIndex: 1
          })
          myChart.dispatchAction({
            type: 'downplay',
            seriesIndex: 0,
            dataIndex: 0
          })
        }
      })
    },
drawChartTop1() {
      // console.log('重绘了')
      // 基于准备好的dom,初始化echarts实例
      let myChart = this.$echarts.init(document.getElementById('top1'))
      // 指定图表的配置项和数据
      let option = {
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b}: {c} ({d}%)'
        },
        series: [
          {
            name: '语音转写情况',
            type: 'pie',
            radius: ['50%', '70%'],
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: 'center'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: '20',
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: false
            },
            data: [
              { value: 510, name: '指令转写' },
              { value: 3305, name: '总转写次数' }

            ]
          }
        ]
      }

      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option)

      this.setHighLight('top2')

    },
mounted() {
    // this.drawChart1()
    // this.drawChart2()
    // this.drawChart3()
    // this.drawChart4()
    this.drawChartTop1()
    this.drawChartTop2()
    this.drawChartTop3()
    this.drawChartBottom1()
    this.drawChartBottom2()
    let erd = elementResizeDetectorMaker()
    let that = this
    erd.listenTo(document.getElementById('top1'), (element) => {
      that.$nextTick(() => {
        //使echarts尺寸重置,因为下面写的是$echarts.init所以此处也要带有$符号
        that.$echarts.init(document.getElementById('top1')).resize()
        setTimeout(() => {
          this.setHighLight('top1')
        }, 500)
      })
    })
}
import elementResizeDetectorMaker from 'element-resize-detector'

效果图:

默认打开:

操作后:

 

参考:

https://blog.csdn.net/qq_36947128/article/details/105714590

https://segmentfault.com/q/1010000039026832/a-1020000039027488

https://echarts.apache.org/zh/api.html#action.highlight

https://segmentfault.com/q/1010000039026832?_ea=95492260

https://echarts.apache.org/zh/api.html#events.%E9%BC%A0%E6%A0%87%E4%BA%8B%E4%BB%B6

https://echarts.apache.org/zh/tutorial.html#ECharts%20%E4%B8%AD%E7%9A%84%E4%BA%8B%E4%BB%B6%E5%92%8C%E8%A1%8C%E4%B8%BA

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
如果您想要在 ECharts 饼图选中一个区块时显示数值,可以使用 ECharts 提供的 `graphic` 组件来实现。具体来说,您可以在 `graphic` 中添加一个 `text` 元素,然后在选中区块时通过 `setOption` 方法来更新该元素的文本内容。下面是一个示例: ```javascript // 获取饼图实例 var chart = echarts.init(document.getElementById('main')); // 配置饼图数据 var option = { series: [{ type: 'pie', data: [ {value: 335, name: '直接访问'}, {value: 310, name: '邮件营销'}, {value: 234, name: '联盟广告'}, {value: 135, name: '视频广告'}, {value: 1548, name: '搜索引擎'} ], label: { show: false // 隐藏饼图中的标签 } }], graphic: { elements: [{ type: 'text', id: 'valueText', style: { text: '', font: '14px Microsoft Yahei', // 设置文本字体和大小 fill: '#333' // 设置文本颜色 }, left: 'center', top: 'center' }] } }; // 渲染饼图 chart.setOption(option); // 监听饼图的点击事件 chart.on('click', function(params) { if (params.componentType === 'series') { // 计算选中区块的数值 var value = option.series[0].data[params.dataIndex].value; // 更新文本内容 chart.setOption({ graphic: { elements: [{ id: 'valueText', style: { text: value } }] } }); } }); ``` 这个例子中,我们首先获取了 ECharts 饼图的实例,并配置了一些数据。然后,在 `graphic` 中添加了一个 `text` 元素,用于显示选中区块的数值。在饼图的点击事件中,我们计算了选中区块的数值,并通过 `setOption` 方法更新了 `text` 元素的文本内容。 希望这个回答能够帮到您!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南北极之间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值