<template>
  <div class="forecast">
    <div class="chart" ref="chart" />
  </div>
</template>

<script>
const mock = [
  {name: '2019.10', value: 102},
  {name: '2019.11', value: 280},
  {name: '2019.12', value: 266},
  {name: '2020.01', value: 256},
  {name: '2020.02', value: 231},
  {name: '2020.03', value: 337},
  {name: '2020.04', value: 292},
  {name: '2020.05', value: 266},
  {name: '2020.06', value: 240},
  {name: '2020.07', value: 142},
]
import echarts from 'echarts'
export default {
  name: 'Forecast',
  data() {
    return {
      chart: null,
      option: {
        tooltip: {
          trigger: 'axis',
        },
        grid: {
          top: '16%',
          left: '5%',
          right: '5%',
          bottom: '10%',
          width: 'auto',
          height: 'auto',
          containLabel: true,
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          axisLine: {
            lineStyle: {
              color: 'rgba(91,213,255,0.1)'
            }
          },
          axisLabel: {
            textStyle: {
              color: '#5bd5ff',
            },
            fontSize: this.fontSize,
          },
          data: mock.map(item => item.name)
        },
        yAxis: {
          type: 'value',
          name: '单位：次',
          max: 500,
          min: 0,
          // interval: 100,
          axisLine: {
            show: false,
          },
          axisLabel: {
            textStyle: {
              color: '#fff',
            },
            fontSize: this.fontSize,
          },
          nameTextStyle: {
            color: '#fff',
            fontSize: this.fontSize,
          },
          splitLine: {
            lineStyle: {
              color: 'rgba(91,213,255,0.1)'
            }
          },
        },
        series: [],
        dataZoom: [{
          start: 0,
          end: 100,
          backgroundColor: '#105179',
          fillerColor: '#46b9ef',
          borderColor: 'transparent',
          bottom: '2%',
          height: 10,
          borderRadius: 10,
          handleSize: '200%',
          handleStyle: {
            color: '#fff',
          },
          textStyle: {
            color: '#fff',
          },
        }],
      }
    }
  },
  mounted() {
    // this.$nextTick(this.init)
    setTimeout(this.init, 0)
  },
  computed: {
    fontSize() {
      return Math.floor(screen.height * 1.48 / 100)
    },
  },
  methods: {
    init() {
      this.chart = this.$echarts.init(this.$refs.chart)
      this.setChartData()
    },
    setChartData() {
      const list = mock.map(item => item.value)
      this.option.series = [
        {
          itemStyle: {color: 'gold'}, areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {offset: 0, color: 'rgba(255,215,0,0.8)'},
              {offset: 1, color: 'rgba(255,215,0,0.1)'}
            ])},
          smooth: true, type: 'line', data: list
        },
      ]
      this.option.yAxis = Object.assign(this.option.yAxis, {max: Math.max(...list)})
      this.chart.setOption(this.option)
    },
  }
}
</script>

<style lang="stylus" scoped>
.forecast
  width 100%
  height 100%
  >.chart
    width 100%
    height 95%
</style>
