<template>
  <div class="realtime-container">
    <div class="title">垂线点对应的流量图</div>
    <div ref="chart" class="chart" :style="{height: '140px', width: '100%'}" />
  </div>
</template>

<script>
import * as echarts from "echarts";
import resize from '@/utils/resize'

export default {
  mixins: [resize],
  props: {
    positions: Array,
    measureLine: Array,
  },
  mounted() {
    this.$nextTick(() => {
      this.chart = echarts.init(this.$refs.chart, 'macarons');
    })
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose()
      this.chart = null
    }
  },
  methods: {
    setOptions() {
      if (!this.chart || !this.positions) {
        return;
      }
      const xAxisData = this.positions.map(({x}) => x);
      const seriesData = this.positions.map((_,index) => this.measureLine.find(({pn}) => pn - 1 === index)?.wspeed || 0);
      const options = {
        xAxis: {
          name: '停泊点',
          data: xAxisData,
          type: 'category',
          axisLabel: {
            color: '#54606C'
          },
          axisLine: {
            lineStyle: {
              color: '#E4E4E4'
            }
          }
        },
        yAxis: [
          {
            name: '流量（m³/h）',
            axisLine: {
              show: false
            },
            axisLabel: {
              color: '#54606C'
            },
            nameTextStyle: {
              color: '#8D99A4'
            }
          },
        ],
        grid: {
          left: 20,
          right: 0,
          bottom: 0,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'line'
          },
          formatter: function(prams) {
            return `停泊点：${prams[0].name}<br/>流量：${prams[0].data}m³/h`;
          },
          padding: [5, 10]
        },
        series: [{
          name: '流量',
          type: 'bar',
          barWidth: 14,
          data: seriesData,
          itemStyle: {
            color: '#7FC8E5'
          }
        }]
      };
      this.chart.setOption(options);
    },
  },
  watch: {
    measureLine() {
      this.setOptions()
    }
  }
}
</script>

<style scoped>
.realtime-container {
  background: #fff;
  height: 190px;
  border-radius: 4px;
  padding: 10px 20px;
}
.title {
  font-size: 18px;
  line-height: 26px;
  font-weight: 600;
  color: #1D2738;
}
</style>
