echarts图表响应式的实现

npm install echarts --save

main.js

import * as echarts from 'echarts'
Vue.prototype.$echarts = echarts

echarts图表封装(折线图)

<template>
  <div :id="id" ref="data" style="width: 100%; height: 300px"></div>
</template>

<script>
// import * as echarts from 'echarts'

export default {
  name: 'LineChart',
  props: ['id', 'data'], // 接受从父组件传回的值
  data() {
    return {
      // screenWidth: document.body.clientWidth,
    }
  },
  // 实时监听父组件传过来的值
  // 然后执行drawBar方法 重新绘图
  watch: {
    data: {
      handler(value) {
        this.drawBar(value)
      },
      deep: true, // 深度监听
    },
  },
  mounted() {
    setTimeout(() => {
      this.drawBar(this.data)
    }, 2000)
  },

  methods: {
    drawBar(
      {
        nameArray = [], // x轴的数据
        series = [], // series的数据
      } = {} // 作为一个整体的参数
    ) {
      //
      this.$nextTick(() => {
        const lineBox = this.$echarts.init(document.getElementById(this.id))
        const option = {
          grid: {
            containLabel: true,
          },
          color: ['#1890FF'],
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow',
            },
          },
          legend: {
            data: name,
          },
          xAxis: {
            type: 'category',
            data: nameArray,
            axisTick: {
              show: false,
            },
          },
          yAxis: {
            type: 'value',
            splitNumber: 3,
            splitLine: {
              lineStyle: {
                color: '#E8E8E8',
                type: 'dashed',
              },
            },
          },
          axisLabel: {
            align: 'center',
          },
          series: series,
        }
        window.addEventListener('resize', function () {
          lineBox.resize()
        })
        lineBox.setOption(option, true)
      })
    },
  },
}
</script>

<style scoped>
</style>
 <VueEcharts :id="'echarts1'" :data="objectData1" />
 objectData1: {
        nameArray: [],
        series: [
          {
            data: [],
            name: '接收',
            type: 'line',
            smooth: true,
            symbol: 'none',
            color: '#1890FF',
            lineStyle: {
              width: '3',
            },
          },
          {
            data: [],
            name: '发送',
            type: 'line',
            smooth: true,
            symbol: 'none',
            color: '#FFB0E3',
            lineStyle: {
              width: '3',
            },
          },
        ],
      },
this.objectData3.series[1].data = res.data.map((item) => item.send).reverse()
          this.objectData1.nameArray = res.data.map((item) => this.dayjs(item.stateTime).format('HH:mm')).reverse()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值