<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts';

export default {
  name:'echartsGradient',
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '390px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el)
      this.chart.setOption({
        title: {
          text: '',
          show: true,
          textStyle: {
            color: '#333'
          }
        },
        animation: false,
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#6a7985'
            }
          }
        },
        legend: {
          selectedMode: false,
          data: [
            {
              name:'用水',
              icon: 'circle',
              itemStyle: {
                color: '#F4A72F'
              },
            },
            {
              name:'用电',
              icon: 'circle',
              itemStyle: {
                color: '#1890FF',
              }
            }
          ]
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            splitLine: {
              show: true,
              lineStyle: {
                type: 'dashed',
              }
            },
            axisTick: {
              show: false
            },
            axisLine: {
              show: false,
              noZero:false
            },
            type: 'category',
            boundaryGap: false,
            axisLabel: {color: "#333"},
            data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
          }
        ],
        yAxis: [{
          name: '用水单位: 方',
          nameTextStyle: {color: '#333'},
          type: "value",
          min:0,
          max:1000,
          splitLine: {
            show: true,
            lineStyle: {
              type: 'dashed',
            }
          },
          splitArea:{
            show: false
          },
          axisLabel: {verticalAlign: "center", color: "#333"},
          splitNumber: 5,
        }, {
          name: '用电单位: 度',
          nameTextStyle: {color: '#333',padding:[0,0,0,0]},
          type: "value",
          min:0,
          max:1000,
          splitLine: {
            show: true,
            lineStyle: {
              type: 'dashed'
            }
          },
          splitArea:{
            show: false
          },
          axisLabel: {verticalAlign: "center", color: "#333"},
          splitNumber: 5,
        }],
        series: [
          {
            name: '用水',
            type: 'line',
            smooth: true,
            symbol: 'none',
            symbolSize: 5,
            showSymbol: false,
            sampling: 'average',
            lineStyle: {
              width: 1,
              color: '#F4A72F',
              type: 'solid',
            },
            areaStyle: {
              opacity: 0.5,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: 'rgba(244,167,47,0.5)'
                },
                {
                  offset: 1,
                  color: 'rgba(244,167,47,0.1)'
                }
              ])
            },
            data: [0, 232, 101, 264, 90, 340, 250,140, 232, 101, 264, 90]
          },
          {
            name: '用电',
            type: 'line',
            smooth: true,      
            symbol: 'circle',
            symbolSize: 5,
            sampling: 'average',
            lineStyle: {
              width: 1,
              color: '#1890FF',
              type: 'solid',
            },
            showSymbol: false,
            areaStyle: {
              opacity: 0.8,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: 'rgba(24, 144, 255, 0.5)'
                },
                {
                  offset: 1,
                  color: 'rgba(24, 144, 255, 0.1)'
                }
              ])
            },
            data: [420, 282, 111, 234, 220, 0, 310,120, 282, 111, 234, 220]
          }
        ]
      })
    }
  }
}
</script>
