<template>
  <div id='chart_2{{sid}}' ></div>
</template>

<script>
  import co from 'co'
  import { HttpResetClass } from 'vue-client'

  let load = new HttpResetClass()
  let getData = function * (self) {
    load.load('POST', self.sql,
      {data:self.condition},
      {resolveMsg: null, rejectMsg: null})
      .then((res) => {
        let model = []
        for (var i = 0; i < res.data.length; i++) {
          let item = {}
          item.name = res.data[i].name
          item.value = res.data[i].value
          model.push(item)
        }
        self.set(model)
      })
  }
  export default {
    data() {
      return {
        sid: new  Date().getSeconds()
      }
    },
    props: {
      sql: String,
      condition: {
        type: Object,
        default: '1=1'
      }
    },
    ready(){
      this.getdata()
    },
    watch:{
      'condition'(val){
        this.getdata()
      }
    },
    methods: {
      getdata(){
        co(getData(this))
      },
      set(model){
        // 基于准备好的dom，初始化echarts实例
        let myChart = this.$echarts.init(document.getElementById('chart_2'+this.sid))

        var scaleData = model
        var rich = {
          white: {
            //color: '#000',
            align: 'center',
            padding: [3, 0]
          }
        };
        var placeHolderStyle = {
          normal: {
            label: {
              show: false
            },
            labelLine: {
              show: false
            },
            color: 'rgba(0, 0, 0, 0)',
            borderColor: 'rgba(0, 0, 0, 0)',
            borderWidth: 0
          }
        };
        var data = [];
        var color = ['#00ffff', '#00cfff', '#006ced', '#ffe000', '#ffa800', '#ff5b00', '#ff3000']
        for (var i = 0; i < scaleData.length; i++) {
          data.push({
            value: scaleData[i].value,
            name: scaleData[i].name,
            itemStyle: {
              normal: {
                borderWidth: 5,
                shadowBlur: 20,
                borderColor: color[i],
                shadowColor: color[i]
              }
            }
          }, {
            value: 2,
            name: '',
            itemStyle: placeHolderStyle
          });
        }
        var seriesObj = [{
          name: '',
          type: 'pie',
          clockWise: false,
          radius: [95, 100],
          hoverAnimation: false,
          itemStyle: {
            normal: {
              label: {
                show: true,
                position: 'outside',
               // color: '#000',
                formatter: function(params) {
                  var percent = 0;
                  var total = 0;
                  for (var i = 0; i < scaleData.length; i++) {
                    total += scaleData[i].value;
                  }
                  percent = ((params.value / total) * 100).toFixed(0);
                  if (params.name !== '') {
                    return params.name + '\n{white|' + '占比' + percent + '%}';
                  } else {
                    return '';
                  }
                },
                rich: rich
              },
              labelLine: {
                length: 10,
                length2: 20,
                show: true,
                color: '#00ffff'
              }
            }
          },
          data: data
        }];
        var option = {
          backgroundColor: '#eee',
          tooltip: {
            show: false
          },
          legend: {
            show: false
          },
          toolbox: {
            show: false
          },
          series: seriesObj
        }
        // 绘制图表
        myChart.setOption(option);
      }
    }
  }
</script>


<style scoped>

</style>
