<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2020-07-02 16:48:49
 * @LastEditors: 无尘
 * @LastEditTime: 2020-07-03 15:07:36
-->
<!--
  <day-login-chart :charData="charData" id="day-login-chart"></day-login-chart>
  import dayLoginChart from '@/components/login-data/day-login-chart.vue';
-->
<template>
  <div :id="id"></div>
</template>

<script>
import * as G2 from '@antv/g2';

export default {
  data() {
    return {
      chart: null
    };
  },
  props: {
    charData: {
      type: Array,
      default() {
        return [];
      }
    },
    id: String,
    width: {
      type: Number,
      default: 800
    }
  },
  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,
        autoFit: true,
        height: 250,
        padding: [50, 30, 60, 60]
      });
      this.chart.source(this.charData);
      this.chart.scale({
        people: {
          min: 0,
          nice: true
        },
        waiting: {
          range: [0, 1],
          nice: true
        }
      });
      this.chart.axis('people', {
        grid: null,
        title: {
          fontSize: 12, // 文本大小
          textAlign: 'center', // 文本对齐方式
          fill: '#303133', // 文本颜色
          position: 'end',
          rotate: 0
        }
      });
      this.chart.tooltip({
        // crosshairs: {
        //   type: 'line'
        // }
        showCrosshairs: true,
        shared: true
      });
      this.chart
        .interval()
        .position('time*waiting')
        .color('#597EF7');
      this.chart
        .line()
        .position('time*people')
        .color('#5ad8a6');
      this.chart
        .point()
        .position('time*people')
        .color('#5ad8a6')
        .size(2)
        .shape('circle')
        .style({
          stroke: '#fff',
          lineWidth: 1
        });
      this.chart.legend({
        position: 'top', // 设置图例的显示位置
        itemGap: 20, // 图例项之间的间距
        custom: true,
        items: [{ value: 'waiting', name: 'waiting', marker: { symbol: 'square', style: { fill: '#597ef7', r: 5 } } }, { value: 'people', name: 'people', marker: { symbol: 'hyphen', style: { stroke: '#5ad8a6', r: 5, lineWidth: 3 } } }]
      });
      this.chart.render();
    }
  }
};
</script>

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