<template>
  <div class="lineBarCharts" ref="lineBarCharts" />
</template>

<script>
import * as echarts from 'echarts'
export default {
  name: '',
  props: {
    title: {
      type: String,
      default: ''
    },
    xAxisData: {
      type: Array,
      default: []
    },
    seriesData: {
      type: Array,
      default: []
    }
  },
  data () {
    return {
      uEcharts: null,
      uOption: null,

    }
  },
  watch: {
    seriesData: {
      handler(newValue, oldValue){
        console.log(newValue, oldValue)
        if(newValue){
          this.setSeriesData(newValue)
        }
      },
      // immediate: true
    }
  },
  created () {
  },
  mounted () {
    this.$nextTick(() => {
      this.initChart()
      this.setSeriesData()
    })
  },
  methods: {
    initChart(){
      this.uEcharts = echarts.init(this.$refs.lineBarCharts)

      let color = ['#42CEAF', '#1890ff', '#16B4FE', '#747AFE'];
      this.uOption = {
        // title: {
        //   text: this.title + '图',
        //   left: 'center'
        // },
        color: color,
        tooltip: {
          trigger: 'axis'
        },
        grid: {
          left: '5%',
          right: '5%',
          bottom: '10%'
        },
        legend: {
          type: 'scroll',
          width: '60%',
          // orient: 'vertical',
          // right: 90,
          // left: 20,
          top: 0,
          // bottom: 20,
          // selectedMode: 'single',
          padding: [
              5,  // 上
              150, // 右
              0,  // 下
              20, // 左
          ],
        },
        toolbox: {
          show: true,
          feature: {
            // dataView: { readOnly: false }, // 展示数据--不过没用
            // magicType: { type: ['bar', 'line'] },
            // restore: {}, // 还原
            saveAsImage: {}
          }
        },
        // dataZoom: [
        //   {
        //     startValue: '2023-02-01'
        //   },
        //   {
        //     type: 'inside'
        //   }
        // ],
        xAxis: {
          type: 'category',
          // boundaryGap: true, // 解决数据溢出X轴的问题
          data: this.xAxisData
        },
        yAxis: [
          {
            type: 'value',
            axisLabel: {
              // 可以动态设置是数量还是金额
              formatter: '{value} '
            }
          },
          {
            type: 'value',
            axisLabel: {
              // 可以动态设置是数量还是金额
              formatter: '{value} '
            }
          },
        ],
        series: [
          // {
          //   name: '合同数量',
          //   type: 'line',
          //   yAxisIndex: 0,
          //   data: [50, 31, 53, 51, 42, 48, 59]
          // },
          // {
          //   name: '合同金额',
          //   type: 'bar',
          //   yAxisIndex: 1,
          //   barWidth: '30%',
          //   data: [500, 310, 530, 510, 420, 480, 590]
          // },
        ]
      };

      this.uOption && this.uEcharts.setOption(this.uOption);
      window.addEventListener('resize', ()=>{
        this.uEcharts.resize()
      })
    },
    setSeriesData(data){
      // console.log(this.uEcharts)
      var seriesData = data ? data : this.seriesData
      // console.log(seriesData)
      // var option = this.uOption
      this.uOption.series = seriesData.map((res, index) => {
        return {
          name: res.name,
          type: res.type,
          yAxisIndex: index,
          data: res.data,
        }
      })
      console.log(this.uOption)
      this.uEcharts.setOption(this.uOption, true)
    }
  }
}
</script>

<style lang="less" scoped>
.lineBarCharts{
  width: 100%;
  height: 100%;
}
</style>
