<template>
  <div class="inerank-container">
    <div :style="{height:'100%',width:'100%'}" ref="myLineRank"></div>
  </div>
</template>
<script>
export default {
  props:{
    data:{
      type:Array,
      default:[]
    }
  },
  data() {
    return {

    };
  },
  mounted() {
    this.init();
  },
  watch:{
    data:{
      handler: function (val, oldVal) {
        if (val) {
          console.log('inerank..........')
          this.init()
        }
      }
    }
  },
  methods: {
    init() {
      let data=[]
      this.data.forEach(x=>{
        if(x.ratio>0){
          data.push({
            value:x.ratio,
            name:x.plat
          });
        }
      })
      let myChart = this.$echarts.init(this.$refs.myLineRank);
      let option = {
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        color: ["#468af5", "#5ec2fb", "#44dac6", "#0174f5"],
        grid: {
          left: "0",
          right: "30px",
          top: "0"
        },
        series: [
          {
            name: "订单来源",
            type: "pie",
            radius: [20, 65],
            center: ["50%", "50%"],
            data: data.sort(function(a, b) {
              return a.value - b.value;
            }),
            roseType: "radius",
            label: {
              normal: {
                textStyle: {
                  color: "#79778a",
                  fontFamily: "pingfang",
                  fontSize: 12,
                  verticalAlign: "bottom"
                },
                formatter: "{b}\n{d}%\n\n",
                borderWidth: 20,
                borderRadius: 4,
                padding: [10, -50]
              }
            },
            labelLine: {
              normal: {
                lineStyle: {
                  color: "#223f77"
                },
                smooth: 0.1,
                length: 5,
                length2: 58
              }
            },
            itemStyle: {
              normal: {
                color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: "#4e5afd" },
                  { offset: 1, color: "#44c4ed" }
                ]),
                shadowBlur: 10,
                shadowColor: "#4e5afd"
              }
            },
            animationType: "scale",
            animationEasing: "elasticOut",
            animationDelay: function(idx) {
              return Math.random() * 200;
            }
          }
        ]
      };
      console.log(option);
      myChart.setOption(option);
    }
  }
};
</script>
<style>
.inerank-container {
  height: 100%;
  width: 100%;
}
</style>
