<template>
  <div :id="id" :style="{ width: width, height: height }"></div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  props: {
    id: {
      type: String
    },
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },

  data() {
    return {
      defaultOption: {
        title: {
          text: null,
          left: 'center'
        },
        tooltip: {
          show: true,
          trigger: 'axis'
        },
        legend: {},
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: []
        },
        yAxis: {
          type: 'value'
        },
        series: []
      }
    };
  },
  mounted() {
    this.initChart();
  },
  methods: {
    initChart() {
      this.defaultOption = { ...this.defaultOption, ...this.chartData };
      console.log(this.defaultOption);
      const chartDom = document.getElementById(this.id);
      const myChart = echarts.init(chartDom);
      myChart.setOption(this.defaultOption);
    }

    /*    setOptions(chartData) {
      console.log(chartData);
      let option = {};
      const series = [];
      chartData.series.forEach((item) => {
        series.push({
          name: item.name,
          type: 'line',
          data: item.data
        });
      });
      option = {
        title: {
          text: null,
          left: 'center'
        },
        tooltip: {
          show: true,
          trigger: 'axis'
        },
        legend: {
          data: series.map((item) => item.name)
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: chartData.xAxisData
        },
        yAxis: {
          type: 'value'
        },
        series: series
      };
      option && this.chart.setOption(option);
    }*/
  }
};
</script>
