<template>
  <div ref="chart" class="chart" :style="{height: '400px', width: '100%'}" />
</template>

<script>
import * as echarts from "echarts";
require('echarts/theme/macarons') // echarts theme
import resize from '@/utils/resize'

export default {
  mixins: [resize],
  props: {
    sections: Array
  },
  mounted() {
    this.$nextTick(() => {
      this.chart = echarts.init(this.$refs.chart, 'macarons');
      if (this.sections.length > 0) {
        this.setOptions(this.sections);
      }
    })
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose()
      this.chart = null
    }
  },
  watch: {
    sections(value) {
      this.setOptions(value)
    }
  },
  methods: {
    setOptions(sections) {
      const xAxisData = sections.map(({ x }) => x);
      const seriesData = sections.map(({ x, y }) => ([x, y]));
      this.chart.setOption({
        xAxis: {
          name: '起点距',
          boundaryGap: false,
          type: 'value',
          min: Math.min(...xAxisData),
          max: Math.max(...xAxisData),
          axisLine: {
            lineStyle: {
              color: '#FF8500'
            }
          },
          axisTick: {
            lineStyle: {
              color: '#8D99A4'
            }
          },
          axisLabel: {
            color: '#54606C'
          },
          nameTextStyle: {
            color: '#54606C'
          }
        },
        yAxis: [
          {
            name: '高程（m）',
            axisLine: {
              show: true,
              lineStyle: {
                color: '#FF8500'
              }
            },
            axisTick: {
              show: true,
              lineStyle: {
                color: '#8D99A4'
              }
            },
            axisLabel: {
              color: '#54606C'
            },
            nameTextStyle: {
              color: '#54606C'
            },
            min: function (value) {
              return Math.round(value.min - (value.max - value.min) * 0.1);
            },
            max: function (value) {
              return Math.round(value.max + (value.max - value.min) * 0.1);
            },
          },
          {
            axisLine: {
              show: true,
              lineStyle: {
                color: '#FF8500'
              }
            },
            axisTick: {
              show: false,
            },
            axisLabel: {
              show: false,
            },
          },
        ],
        grid: {
          left: 50,
          right: 50,
          bottom: 40,
          top: 40,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        series: [{
          name: '高程',
          itemStyle: {
            normal: {
              color: '#FF8500',
              lineStyle: {
                color: '#FF8500',
                width: 2
              }
            }
          },
          areaStyle: {
            color: '#FF8500',
            opacity: 0.5
          },
          smooth: false,
          symbol: 'none',
          type: 'line',
          data: seriesData,
          animations: false,
        }]
      })
    }
  }
}
</script>

<style scoped>

</style>
