<template>
  <div :id="id" :class="className" :style="{ height:height,width:width }" />
</template>
<script>
  import echarts from 'echarts'
  import resize from './mixins/resize'
  import "echarts-wordcloud/dist/echarts-wordcloud";
  import "echarts-wordcloud/dist/echarts-wordcloud.min";
  import '../../assets/js/echarts-wordcloud.min'

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: "chart"
      },
      id: {
        type: String,
        default: "chart"
      },
      width: {
        type: String,
        default: "100%"
      },
      height: {
        type: String,
        default: "400px"
      },
      data: {
        type: Array,
        default: []
      },
      title: {
        type: String,
        default: ""
      }
    },
    data() {
      return {
        chart: null
      };
    },
    watch: {
      //观察option的变化
      data: {
        handler(newVal, oldVal) {
          if (newVal) {
            this.initChart();
          }
        },
        deep: true //对象内部属性的监听，关键。
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart();
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return;
      }
      this.chart.dispose();
      this.chart = null;
    },
    methods: {
      initChart() {
        this.chart = echarts.init(document.getElementById(this.id));
        let option = {
          title: {
            text: this.title,
            x: "center"
          },
          // backgroundColor: "#fff",
          // tooltip: {
          //   pointFormat: "{series.name}: <b>{point.percentage:.1f}%</b>"
          // },
          series: [
            {
              type: "wordCloud",
              shape: "circle",
              //用来调整词之间的距离
              gridSize: 6,
              //用来调整字的大小范围
              // Text size range which the value in data will be mapped to.
              // Default to have minimum 12px and maximum 60px size.
              sizeRange: [14, 20],
              // Text rotation range and step in degree. Text will be rotated randomly in range [-90,                                                                             90] by rotationStep 45
              //用来调整词的旋转方向，，[0,0]--代表着没有角度，也就是词为水平方向，需要设置角度参考注释内容
              rotationRange: [0, 90],
              rotationStep: 90,
              //随机生成字体颜色
              // maskImage: maskImage,
              textStyle: {
                normal: {
                  color: function() {
                    var colors = ['#f39800', '#007de7', '#63d5f8', "#b7e2fe"];
                    return colors[parseInt(Math.random() * 4)];
                    // return 'rgb(' + [
                    //   Math.round(Math.random() * 256),
                    //   Math.round(Math.random() * 256),
                    //   Math.round(Math.random() * 256)
                    // ].join(',') + ')';
                  }
                }
              },
              //位置相关设置
              // Folllowing left/top/width/height/right/bottom are used for positioning the word cloud
              // Default to be put in the center and has 75% x 80% size.
              left: "center",
              top: "center",
              right: null,
              bottom: null,
              width: "200%",
              height: "200%",
              //数据
              data: this.data
            }
          ]
        };
        this.chart.setOption(option);
      }
    }
  };
</script>
