<template>
  <div class="myChart" ref="myChart" :style="{height: height+'px'}"></div>
</template>

<script>
import apiDashboard from "@/api/dashboard/apiDashboard";

export default {
  data() {
    return {
      option: {
        color: ['#3AA7E6','#F8797E'],
        legend: {
          data: [
            '已使用时间',
            '标准年限',
          ]
        },
        grid: {
          left: '2%', //默认10%
          right: '2%', //默认10%
          bottom: '0%', //默认60
          top: '15%',
          containLabel: true
          //grid区域是否包含坐标轴的刻度标签
        },
        xAxis: {
          type: 'category',
          data: []
        },
        yAxis: [
          {
            name: '已使用时间',
            type: 'value',
            nameTextStyle: {
              padding: [10, 0, 10, 15]
            },
          },
          {
            type: 'value',
            name: '标准年限',
            nameTextStyle: {
              padding: [10, 20, 10, 0]
            },
          },
        ],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          },
          textStyle: {
            color: '#fff',
            align: 'left',
            fontSize: 14
          },
          axisLine: {//x坐标轴轴线
            show: true,
            lineStyle: {//x坐标轴轴线样式
              color: '#000',//'#ccc' | 'rgb(128, 128, 128)' | 'rgba(128, 128, 128, 0.5)'，设置标签颜色
            }
          },
          backgroundColor: 'rgba(0,0,0,0.8)',
        },
        series: [
          {
            name: '已使用时间',
            data: [],
            type: 'bar',
            stack: 'A',
            barWidth: '30%',
            backgroundStyle: {
              color: 'rgba(180, 180, 180, 0.2)'
            }
          },
          {
            name: '标准年限',
            data: [],
            stack: 'B',
            yAxisIndex:1,
            type: 'line',
          }
        ]
      }
    }
  },
  props: {
    height: Number
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      let chart = this.$echarts.init(this.$refs.myChart);
      this.chart = chart;
      chart.setOption(this.option);
      this.getData();
    },
    getData() {
      apiDashboard.getMoneyLifeData({}).then(res=>{
        let data = this.$util.dataUtil.covertDataToEcharts(res, ['serviceLife','standardLife'])
        this.$util.dataUtil.refreshEchartsData(this.chart, this.option, data)
      })
    }
  }
}
</script>

<style lang="less" scoped>
</style>