<template>
  <div class="grid-chart" :id="id">

  </div>
</template>

<script>

import {copyObj, uuid} from '@/utils';
import * as echarts from 'echarts';

const basicSerie = {
  type: 'line',
};
const opts = {
  grid: {
    bottom: 20,
  },
  tooltip: {
    show: true,
    trigger: 'axis',
  },
  legend: {
    data: [],
  },
  series: [],
};
export default {
  props: {
    optCfg: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      id: uuid(),
      refCharts: null,
    };
  },
  mounted() {
    this.$nextTick(this.initChart);
  },
  created() {
  },
  methods: {
    initChart() {
      this.refCharts && this.refCharts.clear();
      const chartDom = document.getElementById(this.id);
      this.refCharts = echarts.init(chartDom);
      let option = copyObj(opts);
      const {series, xAxis, ...rest} = this.optCfg
      Object.assign(option, rest);
      const legend = [];
      series.forEach((item, index) => {
        const serie = copyObj(basicSerie);
        Object.assign(serie, item);
        const {name} = item;
        option.series.push(serie);
        if (name) {
          legend.push(name);
        }
      });
      option.legend.data = legend;
      option.xAxis = xAxis;
      option && this.refCharts && this.refCharts.setOption(option);
      this.refCharts.resize();
    },
  },
};
</script>
<style src="./index.scss" lang="scss"/>
