<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)
        }
      },
      deep: true,
      // immediate: true
    }
  },
  created () {
  },
  mounted () {
    this.$nextTick(() => {
      this.initChart()
      this.setSeriesData()
    })
  },
  methods: {
    initChart(){
      this.uEcharts = echarts.init(this.$refs.lineBarCharts)

      let color = ['#42CEAF', '#FF882A', '#16B4FE', '#747AFE','#f9c129', '#13c2c2', '#79ce42', '#f55837'];
      // fc8452  
      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} '
          }
        },
        series: [
          // {
          //   name: 'Highest',
          //   type: 'line',
          //   data: [10, 11, 13, 11, 12, 12, 9],
          //   markPoint: {
          //     data: [
          //       { type: 'max', name: 'Max' },
          //       { type: 'min', name: 'Min' }
          //     ]
          //   },
          //   // markLine: {
          //   //   data: [{ type: 'average', name: 'Avg' }]
          //   // }
          // },
        ]
      };

      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.xAxis.data = this.xAxisData
      this.uOption.series = seriesData.map(res => {
        return {
          name: res.name,
          type: 'bar',
          smooth: true,
          data: res.data,
        }
      })
      console.log(this.uOption.series)
      this.uEcharts.setOption(this.uOption, true)
    }
  }
}
</script>

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