<template>
  <div class="chart" :id="id"></div>
</template>

<script lang="ts">
import { Component, Prop, Vue, Watch } from 'vue-property-decorator'
import * as echarts from 'echarts';
type EChartsOption = echarts.EChartsOption;

@Component
export default class Pie02 extends Vue {
  id=this.randomString();

  mounted(){
    this.init()
  }

  randomString(){
    const str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    let result = '';
    for (let i = 10; i > 0; --i) 
      result += str[Math.floor(Math.random() * str.length)];
    return result;
  }

  init(){
    let chartDom:any = document.getElementById(this.id);
    let myChart = echarts.init(chartDom);
    let option: EChartsOption;

    option = {
      title: {
        text: '月订单来源',
        left: 'center'
      },
      tooltip: {
        trigger: 'item'
      },
      legend: {
        orient: 'vertical',
        right: 'right'
      },
      series: [
        {
          name: '当前数据',
          type: 'pie',
          radius: ['40%', '70%'],
          avoidLabelOverlap: false,
          itemStyle: {
            borderRadius: 10,
            borderColor: '#fff',
            borderWidth: 2
          },
          label: {
            formatter: '{b}:\n {d}' + '%',
            position:'inside'
          },
          emphasis: {
            label: {
              show: true,
              fontSize: 20,
              fontWeight: 'bold'
            }
          },
          labelLine: {
            show: false
          },
          data: [
            { value: 1048, name: '手工下单' },
            { value: 735, name: '聚水潭' },
            { value: 580, name: '其他' }
          ]
        }
      ]
    };

    option && myChart.setOption(option);
  }

}
</script>

<style lang="scss" scoped>
.chart{
  height: 100%;
  width: 100%;
}
</style>>