echarts 饼图效果,显示其对应数值及百分比

需求:echarts 饼图效果,显示其对应数值及百分比

代码

<Chart1 :ecStyle="ecStyle" :ecOption="optionRight2"/>

<script>
	import Chart1 from "@/components/Chart";
	data(){
		return(){
			optionRight2:{
			  //表头菜单显示
			  //legend: {
				//left:'10%',
				//top:'5%',
				//bottom:'3%',
				//textStyle: {
					//color: '#fff'
				//}
			  //},
	          tooltip: {
	            trigger: 'item'
	          },
	          
	          series: [
	            {
	              name: '数量',
	              type: 'pie',
	              radius: ['35%', '60%'],
	              //center: ['50%', '58%'], 图表定位
	              avoidLabelOverlap: false,  //设置为true时,当数值为0时不重叠
	              itemStyle: {
	                borderRadius: 10,
	                borderColor: '#07076a',
	                borderWidth: 1
	              },
	              labelLine: {
	                show: true,
	              },
	              data: [
	                { value: 57, name: '商户',itemStyle:{color:'#fc8452'}},
	                { value: 117, name: '自由职业',itemStyle:{color:'#3ba272'}},
	                { value: 369, name: '大学生',itemStyle:{color:'#73c0de'}},
	                { value: 448, name: '农民工',itemStyle:{color:'#ee6666'}},
	                { value: 28, name: '医生',itemStyle:{color:'#fac858'}},
	                { value: 135, name: '工程师',itemStyle:{color:'#91cc75'}},
	                { value: 335, name: '其它',itemStyle:{color:'#5470c6'}},
	              ],
	              label:{
	                formatter:function(data){
	                  return `${data.name} ${data.value} 
	(${data.percent.toFixed(1)}%)`
	                }
	              }
	            }
	          ]
	        }
		}
	}
}

</script>

chart1组件

//chart1组件

<template>
    <div :style="ecStyle" ref="chartRef"></div>
</template>

<script>
export default {
    name: 'Chart',
    props: {
        ecStyle: {
            type: Object,
            default: function () {
                return {
                    width: '100%',
                    height: '100%'
                }
            }
        },
        ecOption: {
            type: Object,
            default: function () {
                return {}
            }
        }
    },
    data() {
        return {
            chart: ''
        }
    },
    mounted() {
        this.setDrawChart()
    },
    methods: {
        setDrawChart() {
            this.chart = this.$echarts.init(this.$refs.chartRef)
            this.chart.clear()
            this.chart.setOption(this.ecOption)
            window.addEventListener('resize', this.chart.resize)
			
			//饼图点击的方法
			this.chart.on('click',function(param){
				//当前 name 代表饼图所定义的 name
				if(param.name == ''){}	
			})
			
        }
    },
    watch: {
        /**
         * 观察图表变化
         * @date 2020/8/26 13:54
         * @returns {Object}
         **/
        ecOption: {
            handler(newVal, oldVal) {
                if (this.chart) {
                    if (newVal) {
                        this.chart.setOption(newVal, true)
                    } else {
                        this.chart.setOption(oldVal)
                    }
                } else {
                    this.init()
                }
            },
            // 对象内部属性的监听
            deep: true
        }
    },
    destroyed() {
        /**
         * 销毁监听事件
         * @date 2020/8/26 13:55
         **/
        window.removeEventListener('resize', this.chart.resize)
    }
}
</script>

效果

在这里插入图片描述

  • 13
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您想要在 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` 元素的文本内容。 希望这个回答能够帮到您!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值