pieChart.vue 4.4 KB
<template>
  <div>
     <basic ref="basic" :chartId="chartId" :chartData="chartData"></basic>
  </div>
</template>
<script>
import mixin from './mixin';
export default {
  mixins: [mixin],
  data() {
    return {

    }
  },
  methods: {
    /**
     * 预处理option参数
     * */
    preChartData(cdata) {
       return cdata;
    },
    /**
     * 处理option参数
     * */
    dealChartData(optionFormat) {
      let that = this;
      let { labelShow=true,fontSize=24,labelName=true,_color, seriesText, legendObj, radius,center,isRose,isGraphic,animation=false,_double=false} = optionFormat.otherConf
      let itemDatas = []
      let totalNum = 0
      optionFormat.xAxis = null;
      optionFormat.animation = animation;
      optionFormat.yAxis = null;
      optionFormat.color = _color || optionFormat.color;
      optionFormat.legend = Object.assign({},{
        show:true,
        orient: "horizontal",
        itemWidth: 12,
        itemHeight: 12,
        itemGap: 12,
        bottom: 0,
        selected: {},
        textStyle: {
          color: '#555'
        },
        data: []
      },legendObj);
      if(isGraphic)optionFormat.graphic = [{
        type:'text',
        left: 'center',
        top: 'middle',
        style:{
           textAlign:'center',
           font: `${localStorage.getItem('lang')=='en_US'?20:fontSize}px  sans-serif`,
           text:that.$t('iPage.distribution')
        }
      }];
      optionFormat.tooltip = {
        show: true,
        // trigger: 'axis',
        backgroundColor: '#fff',
        padding: [0, 0, 0, 0],
        textStyle: {
          color: '#333333'
        },
        axisPointer: {
          type: "line",
          animation: true,
          lineStyle: {
              color: '#444'
          }
        },
        extraCssText: 'box-shadow: 0px 0px 10px -4px rgba(3, 3, 3, .4)'
      }
      let option = Object.assign({}, optionFormat);
      /*option.series[0].data = [
        { value: 1048, name: 'Search Engine' },
        { value: 735, name: 'Direct' },
        { value: 580, name: 'Email' },
        { value: 484, name: 'Union Ads' },
        { value: 300, name: 'Video Ads' }
      ];*/
      option.series.forEach((item, index) => {
        item.data.forEach(dataItem => {
          totalNum += dataItem.value;
          option.legend.data.push(dataItem.name)
        })
        item.type = 'pie'
        if(index==0)item.zlevel=2;
        item.radius = radius || ['0%', '50%']
        if(index==1&&_double)item.radius = ['60%','80%'];
        item.center = center|| ['50%', '50%']
        item.roseType = isRose?'radius':false;
        item.itemStyle = {
          emphasis: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            borderRadius:10,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
        if(isRose){
           item.itemStyle.borderRadius = 5;
           item.itemStyle.borderWidth = 0;
           item.itemStyle.borderColor = '#fff';
        }
        item.label = {
          normal: {
            show: !_double&&labelShow,
            position: 'outside',
            formatter: (labelName?'{b} ':'')+'{d}%'
          },
          emphasis: {
            show: option.series.length>1||!labelShow?false:true,
          }
        }
        item.labelLine = {
          normal: {
            show: labelShow,
          }
        }
      });
      option.tooltip.formatter = (params, ticket, callback) => {
          let htmls = ''
          let dataVal = ''
          let ratio = ''
          if(params.value || params.value === 0) {
              dataVal = params.value;
              ratio = (params.value / totalNum * 100).toFixed(2) + '%';
          } else {
              dataVal = '--';
              ratio = '--';
          }
          htmls += '<div>'
              + '<p style="font-size:14px; text-align: left; padding:4px 15px; color:"#444444"">' + params.name +'</p>'
              + `<p style="font-size:14px; text-align: left; padding:4px 15px; color:"#444444"">${this.$t('iPage.trafficcount')}: <span style="color:#5093fa">` + dataVal + `</span>${this.$t("format.perTime")}</p>`
              + `<p style="font-size:14px; text-align: left; padding:4px 15px; color:"#444444"">${this.$t("asisTab.proportion")}: <span style="color:#5093fa">` + params.percent + '%</span></p>'
          return htmls;
      }
      return option;

    }
  },
  created() {

  }
}

</script>
<style scoped>
.chart {
  position: relative;
  height: 500px;
}

</style>