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

<script>
import * as echarts from 'echarts'
export default {
  name: '',
  props: {
    xAxisData: {
      type: Array,
      default: []
    },
    seriesData: {
      type: Array,
      default: []
    },
    selectedMode: {
      type: String,
      default: 'multiple' // 'multiple'
    },
  },
  data () {
    return {
      uEcharts: null,
      uOption: null,
    }
  },
  watch: {
    seriesData: {
      handler(newValue, oldValue){
        console.log(newValue, oldValue)
        if(newValue){
          this.setSeriesData(newValue)
        }
      },
      deep: true,
      // immediate: true
    },
    selectedMode: {
      handler(newValue, oldValue){
        console.log(newValue, oldValue)
        if(newValue){
          this.uOption.legend.selectedMode = newValue
          this.uEcharts.setOption(this.uOption, 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: '归一化展示',
          // left: 'center',
          left: 'left'
        },
        // color: color,
        tooltip: {
          trigger: 'axis'
        },
        grid: {
          left: 60,
          right: 20,
          // bottom: '70px',
          bottom: '30px'
        },
        legend: {
          selectedMode: 'multiple', // 单个显示
          type: 'scroll',
          width: '60%',
          // orient: 'vertical',
          right: 0,
          // left: 20,
          // top: 60,
          // bottom: 20,
          // selectedMode: 'multiple',
          padding: [
              5,  // 上
              150, // 右
              0,  // 下
              20, // 左
          ],
        },
        toolbox: {
          show: true,
          feature: {
            // dataView: { readOnly: false }, // 展示数据--不过没用
            magicType: { type: ['line', 'bar'] },
            // restore: {}, // 还原
            saveAsImage: {}
          }
        },
        // dataZoom: [
        //   {
        //     type: 'inside',
        //     start: 0,
        //     end: 100,
        //   },
        //   {
        //     start: 0,
        //     end: 100
        //   }
        // ],
        xAxis: {
          type: 'category',
          // boundaryGap: true, // 解决数据溢出X轴的问题
          data: this.xAxisData
        },
        yAxis: {
          type: 'value',
          name: '单位（元）',
          nameTextStyle: {
            color: "#aaa",
            nameLocation: "start",
          },
          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: 'line',
          smooth: true,
          data: res.data,
        }
      })
      // console.log(this.uOption.series)
      this.uEcharts.hideLoading()
      this.uEcharts.setOption(this.uOption, true)
    }
  }
}
</script>

<style lang="less" scoped>
.a-spin{
  height: 100%;
  .ant-spin-container{
    height: 100%;
  }
}
.lineBarCharts{
  width: 100%;
  height: 100%;
}
</style>
