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

<script>
// import echarts from 'echarts'
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import {formatMoney} from "@/filters";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    xAxisArray:  Array
  },
  data() {
    return {
      printData:{},
      printNameList:[],
      chart: null,
      xAxisArrayReal: this.xAxisArray,
    }
  },
  mounted() {
    // this.$nextTick(() => {
    //   this.initChart()
    // })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'vintage');
      this.setOptions(this.printData)
    },
    setOptions(printData) {
      const _that = _that;
      var seriesArray = [];
      for (let i = 0; i < printData.seriesData.length; i++) {
        var datas = printData.seriesData[i];
        var parms = {
          name:"",
          smooth: false,
          type: 'line',
          data: [],
        }
        parms.name = printData.legendStrArray[i];
        parms.data = printData.seriesData[i].data;
        seriesArray.push(parms)
      }

      this.chart.setOption({
        xAxis: {
          data:printData.xAxisArray,
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 60,
          bottom: 20,
          top: 40,
          containLabel: false
        },
        title: {
          text: '订单印量',
          subtext: '',
          left: 'center'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10],
          confine: true,
          formatter: function (params) {
            var str = "";
            str +="<div class='tooltip-con'>"
            for (let i = 0; i < params.length; i++) {
              var itemData = params[i];
              str +="<div class='tooltip-con-li'>"
              str += itemData.marker
              str += "<span style='margin-right: 5px'>"+itemData.seriesName+"</span>";
              str += "<span>"+formatMoney(itemData.value,0)+"</span>";
              str +="</div>"
            }
            str +="<div style='clear: both;'></div>"
            str +="</div>"
            return str
          }
        },
        yAxis: {
          axisTick: {
            show: false
          },
          axisLabel:{
            interval:0,
            rotate:-30
          }
        },
        legend: {
          type: 'scroll',
          orient: 'vertical',
          right: 5,
          top: 25,
          bottom: 20,
          data: printData.legendStrArray
        },
        series: seriesArray
      })
    }
  }
}
</script>
<style lang="scss">
.tooltip-con-li{
  display: inline-block;
  width: 190px;
  margin-right: 8px;
  float: left;
}
.tooltip-con{
  width: 410px;
  font-size: 13px;
  height: auto;
  background: #fff !important;
}
</style>
