<template>
  <div>
    <div :id="id"></div>
    <div v-if="!charData.length" class="chart--nodata">
      <div>
        <img class="image-120" src="@/assets/no-data.svg" />
        <p class="font-14 color-606266 no-data-text">暂无数据</p>
      </div>
    </div>
  </div>
  <!-- <div :id="id"></div> -->
  <!-- <div  v-if="!charData.length" class="chart--nodata">
    <div>
      <img class="image-120" src="@/assets/no-data.svg" />
      <p class="font-14 color-606266 no-data-text">暂无数据</p>
    </div>
  </div> -->
</template>

<script>
// import G2 from '@antv/g2';
import * as G2 from '@antv/g2';
export default {
  components: {},
  data() {
    return {
      chart: null
    };
  },
  props: {
    charData: {
      type: Array,
      default() {
        return []; //{ bizDate: '2018/8/1', 新增人数: 4623 }
      }
    },
    id: String,
    width: {
      type: Number,
      default: 1192
    }
  },
  mounted() {
    setTimeout(() => {
      this.drawChart(); // 第一步是创建的时候更新图表，但是这个不适用于异步请求接口获取相关数据，采用下面的监听的方式
    }, 30);
  },
  beforeUpdate() {
    this.drawChart();
  },
  watch: {
    charData() {
      this.drawChart();
    }
  },
  methods: {
    async drawChart() {
      this.chart && this.chart.destroy();
      if (!this.charData.length) return;
      this.chart = new G2.Chart({
        container: this.id,
        height: 359,
        forceFit: true,
        width: this.width,
        padding: [20, 40, 80, 80]
      });
      //装载数据

      // this.chart.data(this.charData); // 载入数据源
      this.chart.data(this.charData, {
        bizDate: {
          alias: '日期' //别名
        },
        新增人数: {
          alias: '新增人数'
        }
      });
      //坐标轴配置(法一配置)
      this.chart.axis('bizDate', {
        line: {
          style: {
            stroke: '#606266',
            lineDash: [3, 3]
          }
        },
        tickLine: {
          style: {
            fill: '#E4E7ED',
            lineDash: [3, 3]
          }
        },
        label: {
          style: {
            fill: '#606266'
          }
        }
      });
      this.chart.axis('新增人数', {
        grid: {
          line: {
            type: 'line',
            style: {
              stroke: '#E4E7ED',
              lineDash: [3, 3]
            }
          }
        },
        label: {
          style: {
            fill: '#606266'
          },
          formatter: val => {
            // 格式化参数值
            return val;
          }
        }
      });
      // 图例
      this.chart.legend({
        custom: true,
        position: 'top-left',
        items: [{ name: '新增人数', value: '新增人数', marker: { symbol: 'line', style: { stroke: '#4B74E8', lineWidth: 12, lineHeight: 8 } } }]
      });
      if (this.charData.length > 1) {
        this.chart
          .line()
          .position('bizDate*新增人数')
          .color('#4B74E8');
      } else {
        this.chart
          .point()
          .position('bizDate*新增人数')
          .color('#4B74E8');
      }

      this.chart.removeInteraction('legend-filter'); // 自定义图例，移除默认的分类图例筛选交互
      //图表绘制的最后一步，用于将图表渲染至画布
      this.chart.render();
    }
  }
};
</script>
<style lang="scss" scoped>
.chart--nodata {
  height: 256px;
  width: 100%;
  display: flex;
  justify-content: center;
  .image-120 {
    width: 120px;
    height: 120px;
    padding-top: 60px;
  }
  .no-data-text {
    text-align: center;
    padding-top: 18px;
  }
}
</style>
