首页 > 其他分享 >vue2 echarts 渲染数据

vue2 echarts 渲染数据

时间:2024-02-20 17:47:07浏览次数:27  
标签:map const 渲染 myChart item vue2 data echarts resize

 

<template>
  <div ref="friendsTrend" class="mt-16 friendsTrend" />
</template>

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

import { getLineChartStatisticsData } from '../api/wxworkFriends'

export default {
  name: 'FriendsLine',

  props: {
    date: {
      type: Array,
      default: () => []
    },
    userIdList: {
      type: Array,
      default: () => []
    }
  },

  data() {
    return {
      myChart: null
    }
  },

  mounted() {
    this.getData()
    window.addEventListener('resize', this.resize)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resize)
    this.myChart && this.myChart.dispose()
    this.myChart = null
  },
  methods: {
    // 缩放处理
    resize() {
      this.myChart && this.myChart.resize()
    },

    getData() {
      getLineChartStatisticsData({
        userIdList: this.userIdList.map((item) => item.id),
        startTime: `${ moment(this.date[0]).format('YYYY-MM-DD')  } 00:00:00`,
        endTime: `${ moment(this.date[1]).format('YYYY-MM-DD')  } 23:59:59`
      }).then((res) => {
        if (res.data) {
          this.appendData(res.data)
        }
      })
    },

    // 处理数据格式及渲染前处理,通过接口获取到数据后需要调用这个方法
    appendData(data) {
      // 这里需要处理横纵轴的数据展示,然后放到option里面
      const currentTotalNumberList = data.map((item) => item.currentTotalNumber)
      const todayIncreaseNumberList = data.map((item) => item.todayIncreaseNumber)
      const todayAddNumberList = data.map((item) => item.todayAddNumber)
      const todayDeleteNumberList = data.map((item) => item.todayDeleteNumber)
      const todayLossNumberList = data.map((item) => item.todayLossNumber)
      const xAxisList = data.map((item) => item.showTime)
      const option = {
        color: ['#165DFF', '#1FA087', '#D059D3', '#FF8B74', '#259DFF'],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            lineStyle: {
              color: '#86909C', // 显示竖线颜色
              type: 'dashed'
            }
          },
          backgroundColor: 'rgba(0,0,0,0)', // tooltip背景色
          borderColor: 'rgba(204,204,204,0)', // tooltip边框颜色
          borderWidth: 1,
          borderRadius: 4,
          width: 300,
          /* eslint-disable */
          formatter: function (param) {
            // 自定义tooltip内容
            let text = ''
            text += `<div style="background: rgba(204,204,204,0.1);border-radius:4px;padding:8px;backdrop-filter: blur(5px);box-shadow: 0px 0px 16px 0px rgba(29,48,92,0.15);">
                          <div style="color:#1D2129;font-size:12px;">${param[0].name}</div>`
            param.forEach((item, index) => {
              text += `<div style="background:#ffffff;border-radius:2px;margin:8px 0;padding:4px 8px;margint-bottom:30px">
                          <b style="display:inline-block;width:8px;height:8px;border-radius:6px;background-color:${param[index]?.color}"></b>
                          <span style="color:#4E5969;font-size: 12px; margin-right:50px;">${param[index]?.seriesName} </span>
                          <span style="float:right;color:#1D2129;font-size:12px;">${param[index]?.value}</span>
                        </div>`
            })
            text += `</div>`
            return text
          }
        },
        legend: {
          data: ['好友总数', '净增人数', '添加人数', '删除人数', '流失人数'],
          top: '0%',
          left: '97',
          textStyle: {
            fontSize: 14
          }
        },
        grid: {
          left: '3%',
          right: '3%',
          bottom: '1%',
          top: '14%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: xAxisList,
          axisLine: {
            // x轴
            lineStyle: {
              color: '#E5E6EB'
            }
          },
          axisTick: {
            // x轴刻度线
            lineStyle: {
              color: '#C9CDD4'
            }
          },
          axisLabel: {
            textStyle: {
              color: '#86909C'
            }
            // interval: function (index, value) {
            //   if (xAxisList.length <= 12) {
            //     return true
            //   }
            //   // 取余后的个数
            //   const count = (xAxisList.length - 1) % 11
            //   if (index === xAxisList.length - 1 || index === 0) {
            //     return true
            //   } else {
            //     return (index % Math.floor((xAxisList.length - 1 - count) / 11) === 0) && (index < (xAxisList.length - count))
            //   }
            // },
            // formatter: function (value, index) {
            //   // 取余后的个数
            //   const count = (xAxisList.length - 1) % 11
            //   if (xAxisList.length <= 12) {
            //     return value
            //   }
            //   if (index === xAxisList.length - 1 || index === 0) {
            //     return value // 最后一个日期必须有文字展示
            //   } else if ((index % Math.floor((xAxisList.length - 1 - count) / 11) === 0) && (index < (xAxisList.length - count))) {
            //     return value // 其余的日期平均分配11个点
            //   } else {
            //     return ' '
            //   }
            // }
          }
        },
        yAxis: {
          type: 'value',
          minInterval: 1,
          nameLocation: 'end',
          nameTextStyle: {
            color: '#86909C',
            fontSize: 12,
            marginRight: 23
            //  align:"left"
          },
          axisLine: {
            // y轴线
            show: false
          },
          axisTick: {
            // y轴刻度
            show: false
          },
          axisLabel: {
            // show :false,
            color: '#86909C'
          },
          splitLine: {
            lineStyle: {
              color: '#E4E4E4',
              type: 'dashed'
            }
          }
        },
        series: [
          {
            name: '好友总数',
            type: 'line',
            data: currentTotalNumberList,
            symbol: 'none'
          },
          {
            name: '净增人数',
            type: 'line',
            data: todayIncreaseNumberList,
            symbol: 'none'
          },
          {
            name: '添加人数',
            type: 'line',
            data: todayAddNumberList,
            symbol: 'none'
          },
          {
            name: '删除人数',
            type: 'line',
            data: todayDeleteNumberList,
            symbol: 'none'
          },
          {
            name: '流失人数',
            type: 'line',
            data: todayLossNumberList,
            symbol: 'none'
          }
        ]
      }
      this.drawInit(option)
    },
    drawInit(option) {
      this.$nextTick(() => {
        const dom = this.$refs.friendsTrend
        if (dom) {
          this.myChart = echarts.init(dom)
          this.myChart.clear() // 必写,清空数据,重新赋值
          this.myChart.resize()
          this.myChart.setOption(option)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.friendsTrend {
  height: 500px;
}
</style>
 

 

标签:map,const,渲染,myChart,item,vue2,data,echarts,resize
From: https://www.cnblogs.com/wjs0509/p/18023662

相关文章

  • echarts绘制世界地图的时候显示南海诸岛的局部放大图
    ehcarts在绘制中国地图的时候,判断geo的map为china的时候会自动加上南海诸岛的局部放大图。产品想在绘制世界地图的时候也加上南海诸岛,搞了半天没搞定,请教的同事搞定了。同事在此:简书ID:昊桐_260c。解决办法就是把南海诸岛的地图数据直接放在注册的JSON里面。{     ......
  • 使用Echarts绘图
    案例1参考代码如下<!-- 此示例下载自https://echarts.apache.org/examples/zh/editor.html?c=bar-histogram--><!DOCTYPEhtml><htmllang="en"style="height:100%"><head><metacharset="utf-8"></head&g......
  • nativeUI页面table列表显示,render渲染函数
    {key:'type',title:$t('cmdType'),width:150,align:'center',render(t){switch(t.type){case2:returnh('span',{......
  • echarts自适应问题,echarts中怎么改变字体单位实现自适应
    参考文档:https://blog.csdn.net/MFWSCQ/article/details/102522944最初想着怎么给echarts设置vw单位或者rem,echart中怎么把legend的单位设置为vw或者rem来使表格自适应,后面发现行不通。项目中使用px-to-vw包,将所有px转为对应的vw,所有可以根据相同比例进行缩放,做到自适应效果。但......
  • v-if后的echarts显示已有dom解决方法
    控制台报错:Thereisachartinstancealreadyinitializedonthedom. 核心思路:先判断dom是否存在,如存在就调用销毁方法,再初始化正常操作。echarts内:if(this.myChart!=null&&this.myChart!=""&&this.myChart!=undefined......
  • 解决vue2中el-table表格组件的空状态过小的问题
    问题当我们使用element的表格组件的时候,空状态会变的非常瘪,就会造成页面非常难看这样很不美观解决办法可以使用插槽设置空状态,并通过css来调整空状态的样式新建一个空表格<template><div><el-table:data="tableData"stripestyle="width:100%">......
  • vue3整合echarts
    Vue3是一个流行的前端框架,而ECharts是一个功能强大的图表库。将ECharts整合到Vue3项目中可以方便地展示各种图表。以下是将ECharts整合到Vue3项目中的基本步骤:安装ECharts:使用npm或yarn安装ECharts:bash复制代码npminstallecharts--save或......
  • Vue中使用Echarts
    第一步:安装echarts模块cnpminstallecharts-S第二步:在main.js中全局引入importechartsfrom'echarts'Vue.prototype.$echarts=echarts//全局引入后面用this.$echarts就能直接使用了使用方式:template中<template><el-cardclass="box-card"style=&quo......
  • 基于 GPU 渲染的高性能空间包围计算
    空间包围检测在计算机图形学、虚拟仿真、工业生产等有着广泛的应用。现代煤矿开采过程中,安全一直是最大的挑战之一。地质空间中存在诸多如瓦斯积聚、地质构造异常、水文条件不利等隐蔽致灾因素,一旦被触发,可能引发灾难性的后果。因此在安全生产过程中有效的管理和规避各隐蔽致灾因素......
  • react引用async异步函数数据渲染
    当需要在React组件中引用异步函数获取的数据时,可以使用useState钩子来存储数据,并在组件渲染时进行处理。下面是一个示例,展示了如何在React中引用异步函数的数据并进行渲染:importReact,{useState,useEffect}from'react';functionMyComponent(){const[data,......