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

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '458px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ expectedData, actualData,linedata } = {}) {
      this.chart.setOption({
        title:{text: "2023年浏览统计",
          left: "center",
          top: 0,
          textStyle: {
            color: "#161616",
            fontWeight: 'bold',
            fontSize: '15px',
            }
          },
        color:['#04BFA1', '#4875E8','#FFB132'],
        xAxis: {
          data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月','8月', '9月', '10月', '11月', '12月'],
          axisLabel:{
            color:'#AAAAAA'
          },
          axisLine: {
          	lineStyle: {
          		color: '#DADADA',
          		width: 1
          	}
          },
          splitLine: {
                show: false,
              },
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 40,
          top: 50,
          show:false,
          containLabel: true
        },
        toolbox: {
          show:false,
        },
        tooltip: {
          trigger: 'axis',
          backgroundColor: "#FFF",
          color: "#161616",
          textStyle: {
                color: "#161616",
                // fontWeight:'bold'
              },
          axisPointer: {
            type: 'cross',
          },
          padding: [5, 10]
        },
        yAxis: {
          splitArea: {
            show: false
          },
          axisLabel:{
            color:'#AAAAAA'
          },
          axisLine: {
            show:true,
          	lineStyle: {
          		color: '#DADADA',
          		width: 1
          	}
          },
          splitLine:{  //决定是否显示坐标中网格
            show:false
          }
        },
        legend: {
          x:'center',
          y:'bottom',
          textStyle: {
            color: "#161616",
            padding:[0,3]//文字与图形之间的左右间距
          },
          itemWidth: 38,//图例标记的图形宽度。
          itemHeight: 12,
          itemGap: 20,
          data: ['总浏览人数', '总浏览次数','累计浏览终端数']
        },
        series: [{
          name: '总浏览人数',
          itemStyle: {},
          type: 'bar',
          barGap:"70%",
          barCategoryGap:'30%',
          data: expectedData,
          // animationDuration: 2800,
          // animationEasing: 'cubicInOut'
        },
        {
          name: '总浏览次数',
          type: 'bar',
          barGap:"70%",
          barCategoryGap:'30%',
          data: actualData,
          // animationDuration: 2800,
          // animationEasing: 'quadraticOut'
        },
        {
          name: '累计浏览终端数',
          type: 'line',
          data: linedata,
          // animationDuration: 2800,
          // animationEasing: 'quadraticOut'
        }
        ]
      })
    }
  }
}
</script>
