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

<script>
  import 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: '350px'
      },
      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({
        czjs,
        czxz,
        hjjs,
        hjxz,
        time
      } = {}) {
        // console.log(x,y[0].data,45)
        this.chart.setOption({
          xAxis: {
            type: 'category',
            data: ['进口冷链食品相关', '集中隔离场所工作', '现场疫情处置工作', '定点医疗机构', '普通医疗机构 （工作人员）', '普通医疗机构 （患者、陪护）', '医废收运与处置',
              '快递、外卖类'
            ],
            splitLine:{
              show:true
            },
             axisLine: {
                  lineStyle: {
                    color: '#9BA2B0', //x轴的颜色
                     width:'1'
                  },
              },
             axisLabel: {
                interval: 0, // 设置数据间隔
                formatter:function(value){
                    var str = "";
                        var num = 8; //每行显示字数
                        var valLength = value.length; //该项x轴字数
                        var rowNum = Math.ceil(valLength / num); // 行数
                        if(rowNum > 1)
                        {
                            for(var i = 0; i < rowNum; i++)
                            {
                                var temp = "";
                                var start = i * num;
                                var end = start + num;

                                temp = value.substring(start, end) + "\n";
                                str += temp;
                            }

                            return str;
                        }
                        else
                        {
                            return value;
                        }
                },
            },
            boundaryGap: false,
            axisTick: {
              show: false,
              alignWithLabel: true
            }
          },
          grid: {
            left: 25,
            right: 50,
            bottom: 10,
            top: 30,
            containLabel: true
          },
          tooltip: {
           type: 'line',
            lineStyle: {
                color: '#dddddd',
                width: 1,
                type: 'dashed'
            },
          },
          yAxis: {
            axisTick: {
              show: false
            },
            axisLine: {
                 lineStyle: {
                   color: '#9BA2B0', //x轴的颜色
                    width:'1'
                 },
             },
          },
          legend: {
            data: ''
          },
          series: [{
            data: [100, 145, 160, 150, 163, 180, 170, 200, 230],
            type: 'line',
            areaStyle: {
              color: {
                //线性渐变
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [{
                  offset: 0,
                  color: 'rgba(1, 219, 127, 1)', // 0% 处的颜色
                }, {
                  offset: 0.6,
                  color: 'rgba(1, 219, 127,0)', // 100% 处的颜色
                }],
                global: false, // 缺省为 false
              },
            },
          }]
        })
      }
    }
  }
</script>
