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

<script>
import * as echarts from 'echarts'
export default {
  name: 'lineChart', //折线统计图
  mounted() {
    this.$nextTick(() => {
      this.initialChart()
    })
  },
  methods: {
    initialChart() {
      let option = {
        backgroundColor: 'transparent',
        grid: {
          left: '5%',
          right: '5%',
          top: '20%',
          bottom: '0%',
          containLabel: true
        },
        tooltip: {
          show: true,
          trigger: 'axis',
          borderWidth: 0,
          textStyle: {
            color: '#fff'
          },
          formatter: function (params) {
            //params[0].name表示x轴数据
            let str = params[0].name + '<br/>'
            //params是数组格式
            for (let item of params) {
              //设置浮层图形的样式跟随图中展示的颜色
              str +=
                "<span style='display:inline-block;width:10px;height:10px;border-radius:10px;background-color:" +
                item.borderColor +
                ";'></span>" +
                '\t' +
                item.seriesName +
                ' : ' +
                item.value +
                '<br/>'
            }
            return str
          },
          axisPointer: {
            lineStyle: {
              type: 'solid',
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: 'rgba(126,199,255,0)' // 0% 处的颜色
                  },
                  {
                    offset: 0.5,
                    color: 'rgba(126,199,255,1)' // 50% 处的颜色
                  },
                  {
                    offset: 1,
                    color: 'rgba(126,199,255,0)' // 100% 处的颜色
                  }
                ],
                global: false // 缺省为 false
              }
            }
          },
          backgroundColor: 'rgba(50,50,50,0.7)'
        },
        legend: {
          show: true,
          x: 'center',
          icon: 'stack',
          itemWidth: 5,
          itemHeight: 5,
          textStyle: { color: '#82BAE2' },
          data: [
            { name: '收入', itemStyle: { color: '#0092f6' } },
            { name: '支出', itemStyle: { color: '#CBB54C' } },
            { name: '利润', itemStyle: { color: '#04BD88' } }
          ]
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: true,
            axisLabel: {
              color: 'rgba(130, 186, 226, 1)'
            },
            axisLine: {
              show: false,
              lineStyle: {
                color: '#397cbc'
              }
            },
            axisTick: {
              show: false
            },
            splitLine: {
              show: false,
              lineStyle: {
                color: '#195384'
              }
            },
            data: ['1月', '2月', '3月', '4月', '5月', '6月']
          }
        ],
        yAxis: [
          {
            type: 'value',
            axisLabel: {
              textStyle: {
                color: 'rgba(130, 186, 226, 1)'
              }
            },
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: '#11366e'
              }
            }
          }
        ],
        series: [
          {
            name: '收入',
            type: 'line',
            symbol: 'circle',
            symbolSize: 6,
            itemStyle: {
              color: 'rgba(13, 26, 43, 1)',
              borderColor: '#0092f6',
              borderWidth: 1
            },
            lineStyle: {
              color: '#0092f6',
              width: 1
            },
            data: [1, 3, 1, 2, 3, 2]
          },
          {
            name: '支出',
            type: 'line',
            symbol: 'circle',
            symbolSize: 6,
            itemStyle: {
              color: 'rgba(13, 26, 43, 1)',
              borderColor: '#CBB54C',
              borderWidth: 1
            },
            lineStyle: {
              color: '#CBB54C',
              width: 1
            },
            data: [2, 1, 3, 2, 2, 3]
          },
          {
            name: '利润',
            type: 'line',
            symbol: 'circle',
            symbolSize: 6,
            itemStyle: {
              color: 'rgba(13, 26, 43, 1)',
              borderColor: '#04BD88',
              borderWidth: 1
            },
            lineStyle: {
              color: '#04BD88',
              width: 1
            },
            data: [3, 2, 1, 2, 3, 1]
          }
        ]
      }
      this.creatChart(option, this.$refs.chart)
    },
    creatChart(option, ref) {
      let chart = echarts.init(ref) //this.$refs.chart
      chart.resize()
      chart.setOption(option, {
        notMerge: true
      })
      //图表大小自适应
      window.addEventListener('resize', () => {
        chart.resize()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.chart {
  width: 100%;
  height: 100%;
}
</style>