LineChart.vue 3.07 KB
<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
    },
    lastData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },

  watch: {
    lastData: {
      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.lastData)
    },
    setOptions({ receiptDetails, shipmentDetails } = {}) {
      this.chart.setOption({
        title: {
        text: '历史每日收发货量',
        textStyle: {
          color: 'rgba(0, 0, 0, 0.7)',
          fontSize: '15',
          fontWeight:'bold'
        }
       
        
    },
        xAxis: {
          data: [this.changeDate(this.reduceDays(6)),this.changeDate(this.reduceDays(5)),this.changeDate(this.reduceDays(4)), this.changeDate(this.reduceDays(3)), this.changeDate(this.reduceDays(2)), this.changeDate(this.reduceDays(1)), this.changeDate(new Date())],
          boundaryGap: true,
          axisTick: {
            show: true
          },
        
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          }
        },
        legend: {
          data: ['收货量', '发货量']
        },
        series: [{
          name: '收货量', itemStyle: {
            normal: {
              color: '#FF005A',
              lineStyle: {
                color: '#FF005A',
                width: 2
              }
            }
          },
          smooth: true,
          type: 'line',
          data: receiptDetails,
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        },
        {
          name: '发货量',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#3888fa',
              lineStyle: {
                color: '#3888fa',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: shipmentDetails,
          animationDuration: 2800,
          animationEasing: 'quadraticOut'
        }]
      })
    }
  }
}
</script>