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

  </div>
</template>

<script>
import ApiWorkBus from "@/api/work/apiWorkBus";

export default {
  data() {
    return {
      option: {
        legend: {
          data: [
            '平均出车时长',
            '出车次数',
          ]
        },
        grid: {
          left: '2%', //默认10%
          right: '3%', //默认10%
          bottom: '13%', //默认60
          top: '15%',
          containLabel: true
          //grid区域是否包含坐标轴的刻度标签
        },
        xAxis: {
          type: 'category',
          data: []
        },
        yAxis: [
          {
            name: '出车次数',
            type: 'value',
            position: 'left',
            axisLine: {
              show: true,
            },
            nameTextStyle: {
              padding: [10, 0, 10, 0]
            },
          },
          {
            name: '出车时长(h)',
            type: 'value',
            axisLine: {
              show: true,
            },
            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)',
        },
        dataZoom: this.$constant.ECHARTS_DATAZOOM,
        series: [
          {
            name: '平均出车时长',
            data: [],
            type: 'bar',
            barWidth: '15%',
            backgroundStyle: {
              color: 'rgba(180, 180, 180, 0.2)'
            },
            itemStyle: {
              color: '#80D4FF'
            }
          },
          {
            name: '出车次数',
            data: [],
            type: 'bar',
            yAxisIndex: 1,
            barWidth: '15%',
            backgroundStyle: {
              color: 'rgba(180, 180, 180, 0.2)'
            },
            itemStyle: {
              color: '#FFDF80'
            }
          },
        ]
      },
    }
  },
  props: {
    height: Number,
    queryData: Object
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      let chart = this.$echarts.init(this.$refs.myChart)
      this.chart = chart;
      chart.setOption(this.option)
      this.getData()
    },
    getData() {
      return ApiWorkBus.getBusInfo(this.queryData).then(res=>{
        let data = this.$util.dataUtil.covertDataToEcharts(res, ['averageDuration','numberTrips'])
        this.$util.dataUtil.refreshEchartsData(this.chart, this.option, data)
      })
    }
  }
}
</script>

<style lang="less" scoped>

</style>