pieChart.vue 3.03 KB
<template>
	<view class="piechart">
		<div :id='bindId' class="chart"></div>
	</view>
</template>

<script>
	export default {
	  data () {
		return {
		}
	  },
	  mounted() {
	  },
	  watch:{
		  bindId:function(newValue,oldValue){
		  },
		  chartData:{
			handler(newValue,oldValue){
				this.drawPie();
			},
			deep:true
		  },
	  },
	  props:{
			bindId:{
				type:String,
				default:''
			},
		   chartData: {
                type: Object,
                default: function(){
					return {}
				}
            }
	  },
	  methods: {
	  	 drawPie() {
            let myChart = {};
            let chartDom = document.getElementById(this.bindId);
			myChart = this.echarts.init(chartDom);
            myChart.showLoading('default', {
                text: '',
                color: '#409eff'
            });
			
            var itemDatas = [];
			var option=this.chartData;
            if(!option.series) return;
            for(let i = 0, len = option.series.length;i < len; i++) {
                itemDatas = option.series[i].data;
            }
            let legendData = [];
            for(let i = 0, len = itemDatas.length;i < len; i++) {
                legendData.push({
                    name: itemDatas[i].name,
                    icon: 'rect'
                });
            }
            var opt = {
				title:{
					show:true,
					 text:this.chartData.title,
					 left: uni.upx2px(18.11),
					 top: uni.upx2px(10.86),
					 textStyle: {
						 fontWeight: "normal",
						 fontSize: uni.upx2px(25.4),
						 color: '#4A4A4A'
					 }
				},
                legend: {
                    orient: 'horizontal',
                    right: 'center',
                    itemWidth: uni.upx2px(19.92),
                    itemHeight: uni.upx2px(19.92),
                    bottom: uni.upx2px(18.11),
                    left: 'center',
                    data: legendData
                },
                series: [{
                    type: 'pie',
                    color: ['#6e6fc1', '#33bafe', '#fec62b', '#ff9c01', '#38d4be', '#79ce4c'],
                    center: ['50%', '50%'],
                    radius: ['20%', '60%'],
                    label: {
                        normal: {
                            show: true,
                            position: 'outside',
                            formatter: '{b} {d}%'
                        },
                        emphasis: {
                            show: true
                        }
                    },
                    labelLine: {
                        normal: {
                            show: true
                        }
                    },
                    data: itemDatas
                }]
            };
            // myChart.clear();
            setTimeout(() => {
                myChart.hideLoading();
            }, 500);
            myChart.setOption(opt);
        }
	  }
	}
</script>

<style>
	.chart{
		width:706.52upx;
		height:438.4upx;
		margin: 0 auto;
		background: #FFFFFF;
		margin-bottom: 18.2upx;
		border-radius:7.24upx;
		overflow: hidden;
	}
</style>