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

<script>
import * as echarts from "echarts";
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(history) {
      const seriesData = history.map((h, index) => {
        const positions = JSON.parse(h.positions) || [];
        return {
          name: `${index + 1}.${h.berthingName}`,
          type: 'line',
          smooth: false,
          areaStyle: {normal: {}},
          data: positions.map(({x, y}) => [x, y]),
        }
      });
      const legend = {
          data: history.map((h, index) => `${index + 1}.${h.berthingName}`),
      }
      this.chart.clear();
      this.chart.setOption({
        legend,
        xAxis: {
          name: '起点距',
          boundaryGap: false,
          type: 'value',
          min: 'dataMin',
          max: 'dataMax',
          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: 'dataMin',
            max: 'dataMax',
          },
          {
            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: seriesData
      })
    }
  }
}
</script>

<style scoped>

</style>
