index.vue 2.46 KB
<template>
  <div>
    <div id="channel-main" class="chart-line" ref="line" v-show="lineStatus"></div>
    <div class="chart-line-empty" v-show="!lineStatus">
      <img src="@/assets/img/empty.png" />
      <div class="chart-empty-d">暂无数据</div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  name: 'line',

  data() {
    return {
      lineStatus: true
    }
  },
  props: {
    colorList: {
      type: Array
    }
  },
  methods: {
    init(chartList, codeName) {
      if (chartList.length == 0) {
        this.lineStatus = false
        return
      }
      let time = chartList[0].trendItemList.map(item => {
        return item.dayTime
      })
      let seriesList = chartList.map((item, index) => {
        let newlyQyCount = []
        item.trendItemList.forEach(i => {
          newlyQyCount.push(i.newlyQyCount)
        })
        return {
          name: codeName[index],
          type: 'line',
          data: newlyQyCount,
          itemStyle: {
            normal: {
              color: this.colorList[index],
              lineStyle: {
                color: this.colorList[index]
              }
            }
          }
        }
      })
      echarts.init(document.getElementById('channel-main')).clear()
      let chartDom = document.getElementById('channel-main')
      let myChart = echarts.init(chartDom)
      let option = {
        legend: {
          left: 'auto',
          icon: 'stack',
          itemHeight: 2,
          itemWidth: 12,
          color: this.colorList
        },
        tooltip: {
          trigger: 'axis'
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: time
        },
        yAxis: {
          type: 'value',
          minInterval: 1
        },
        series: seriesList
      }
      myChart.setOption(option)
      let dataList = []
      seriesList.forEach(item => {
        dataList = dataList.concat(item.data)
      })
      if (dataList.every(item => item == 0)) {
        this.lineStatus = false
      } else {
        this.lineStatus = true
      }
    }
  }
}
</script>

<style scoped lang="scss">
.chart-line {
  height: 300px;
}
.chart-line-empty {
  height: 300px;
  text-align: center;
  color: #666;
  img {
    margin-top: 100px;
    width: 64px;
    height: 32px;
    margin-bottom: 12px;
  }
}
</style>