<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";
require('echarts/theme/macarons') // echarts theme
import resize from '@/utils/resize'

export default {
  mixins: [resize],
  mounted() {
    this.$nextTick(() => {
      this.chart = echarts.init(this.$refs.chart, 'macarons');
    })
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose()
      this.chart = null
    }
  },
  methods: {
    setOptions(chartData) {
      if (!this.chart) {
        return;
      }
      chartData.reverse()
      const xAxisData = chartData.map(({ time }) => time);
      const seriesData = chartData.map(({ avgWaterlevel }) => avgWaterlevel);
      const options = {
        xAxis: {
          name: '时间',
          data: xAxisData,
          type: 'category',
          axisLabel: {
            color: '#54606C'
          },
          axisLine: {
            lineStyle: {
              color: '#E4E4E4'
            }
          }
        },
        yAxis: [
          {
            name: '水位（m）',
            axisLine: {
              show: false
            },
            axisLabel: {
              color: '#54606C'
            },
            nameTextStyle: {
              color: '#8D99A4'
            },
            min: 'dataMin',
            max: 'dataMax',
          },
        ],
        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`;
          },
          padding: [5, 10]
        },
        series: [{
          name: '水位',
          type: 'line',
          smooth: false,
          data: seriesData,
          areaStyle: {
            normal: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: "rgba(127, 200, 229, 0.4)"
                },
                {
                  offset: 1,
                  color: "rgba(119, 211, 247, 0.1)"
                }
              ], false),
              shadowColor: "rgba(119, 211, 247, 0.1)",
              shadowBlur: 20
            }
          },
        }]
      };
      this.chart.setOption(options);
    }
  }
}
</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>
