<template>
  <div :class="className" :style="{ height: height, width: width }">
    <!-- <div ref="chart" :style="{ height: height, width: width }"></div> -->
  </div>
</template>

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

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '400px'
    }
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose();
    }
  },
  methods: {
    initChart() {
      if (this.chart) {
        this.chart.dispose();
      }
      this.chart = echarts.init(this.$refs.chart);

      const xValues = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
      const yValues1 = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20];
      const yValues2 = [3, 6, 9, 12, 15, 18, 21, 24, 27, 30];

      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {
          data: ['曲线 1', '曲线 2']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: xValues,
          axisLabel: {
            fontSize: 12
          }
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '曲线 1',
            type: 'line',
            data: xValues.map((x, index) => [x, yValues1[index]]),
            color: '#4CAF50'
          },
          {
            name: '曲线 2',
            type: 'line',
            data: xValues.map((x, index) => [x, yValues2[index]]),
            color: '#FF5722'
          }
        ]
      });
    }
  }
};
</script>

<style scoped>
</style>
