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

<script>
import G2 from "@antv/g2";
import { setTimeout } from 'timers';

export default {
  data() {
    return {
      chart: null
    };
  },
  props: {
    charData: {
      type: Array,
      default() {
        return []
      }
    },
    id: String,
    width: {
      type: Number,
      default: null
    }
  },
  mounted() {
    setTimeout(() => {
      this.drawChart();
    }, 30);
  },
  beforeUpdate() {
    this.drawChart();
  },
  watch: {
    charData() {
      this.drawChart();
    }
  },
  methods: {
    drawChart() {
      // 更新的时候要移除先
      this.chart && this.chart.destroy();
      this.chart = new G2.Chart({
        container: this.id,
        width: this.width
      });
      this.chart.source(this.charData, {
        新增会员: {
          formatter: function formatter(val) {
            return val + '%';
          }
        }
      });
      this.chart.scale('sales',{
        tickInterval: 20
      });
      this.chart.interval().position('keyword*新增会员').size(40);
      this.chart.render();
    }
  }
};
</script>

<style lang="stylus" scoped>
</style>
