浅谈vue单页面中有多个echarts图表时的公用代码写法

 更新时间:2020年07月19日 09:29:13   作者:皮皮虾-coding  
这篇文章主要介绍了浅谈vue单页面中有多个echarts图表时的公用代码写法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

html中:

  <div class="charts1"/>
  <div class="charts2"/>
  <div class="charts3"/>
  <div class="charts4"/>
  <div class="charts5"/>
  <div class="charts6"/>
  <div class="charts7"/>

数据处理就不用提了。嗯,直接画图:

// 画 折线图
 drawLine() {
 // 数据处理的方法
  this.dealEchartsData()
  var myChartsArr = []
  for (var i = 1; i <= 7; i++) {
  this.myCharts = this.$echarts.init(document.getElementsByClassName('charts' + i)[0])
  myChartsArr.push(this.myCharts)
  var option = this.commonOption(this.myCharts, this.adnormalTypeSummery[i - 1], this.destArrAll[i - 1])
  // 为echarts对象加载数据 true 防止echarts数据叠加!!!
  this.myCharts.setOption(option, true)
  }
  window.onresize = function() { // 自适应
  for (var j = 0; j < myChartsArr.length; j++) {
   if (myChartsArr[j].resize()) {
   myChartsArr[j].resize()
   }
  }
  }
 },

公用部分:

 // option 主体
 commonOption(myCharts, titleText, destData) {
  var option = {
  title: {
   text: titleText
  },
  tooltip: {
   trigger: 'axis',
   confine: true
  },
  legend: {
   type: 'scroll',
   width: '90%',
   top: '13%'
  },
  grid: {
   left: '3%',
   right: '4%',
   bottom: '2%',
   containLabel: true
  },
  toolbox: {
   right: '20',
   feature: {
   saveAsImage: {}
   }
  },
  xAxis: {
   type: 'category',
   boundaryGap: false,
   data: this.monthName
  },
  yAxis: {
   type: 'value'
  },
  series: destData
  }
  return option
 }

离开该页面时候摧毁:

destroyed() {
 if (this.myCharts) {
  this.myCharts.clear()
  this.myCharts.dispose()
  window.onresize = null
 }

补充知识:Vue + Echarts 图表展示 及 动态渲染

准备工作

安装echarts依赖

npm install echarts --save-dev

引入

(main.js)
import echarts from 'echarts'
Vue.prototype.$echarts = echarts;

开始撸代码

<template>
 <div class="peopleWrap">
 <h3>
  <i class="el-icon-position"></i>
  出入人员总数{{peopleSumTotal}}
 </h3>
 <div id="peopleSum" style="width: 180px;height: 270px"></div>
 </div>
</template>
<script>
export default {
// 接受父组件传来的参数【父传子props】
 props: ["peopleSumTotal"], 
 data() {
 return {
  peopleSumTotalArr: []
 };
 },
 watch: {
 // 监听参数变化
 peopleSumTotal: {
  handler(newVal, oldVal) {
  if (newVal != 0) {
   console.log(newVal);
   this.peopleSum(newVal);
  }
  }
 }
 },
 methods: {
 peopleSum(newVal) {
 // 引入 echarts
  var echarts = require("echarts");
  let peopleSum = echarts.init(document.getElementById("peopleSum"));
  //echsrts点击事件
  peopleSum.on("click", function(param) {
  console.log(param);
  console.log(param.data.name);
  console.log(param.data.value);
  console.log(param.data.userDefined);
  //$emit的第一个为传的参的名字,第二个为传的值 【子传父 this.$emit】
  _this.$emit("peopleSumtoparent", param.data); 
  });

//接受动态数据时需要在 this.$nextTick(()=>{})展示
  this.$nextTick(() => {
  let obj = {};
  obj.value = newVal;
  obj.name = newVal;
  this.peopleSumTotalArr.push(obj);

  let option = {
   legend: {
   orient: "vertical",
   left: 10,
   data: [""]
   },
   series: [
   {
    type: "pie",
    radius: ["50%", "70%"],
    avoidLabelOverlap: false,
    itemStyle: {
    // 普通样式。
    normal: {
     // 点的颜色。
     color: "#6998f7"
    },
    // 高亮样式。
    emphasis: {
     // 高亮时点的颜色。
     color: "#6998f7"
    }
    },
    label: {
    normal: {
     show: true,
     position: "center",
     textStyle: {
     fontSize: "20"
     }
    }
    },
    labelLine: {
    normal: {
     show: false
    }
    },
    data: this.peopleSumTotalArr //动态图表展示
   }
   ]
  };
  console.log("option", option);
  peopleSum.setOption(option);
  });
 }
 },
 mounted() {}
};
</script>
<style lang="scss" scoped>
</style>

以上这篇浅谈vue单页面中有多个echarts图表时的公用代码写法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Vue过滤器filters的用法及时间戳转换问题

    Vue过滤器filters的用法及时间戳转换问题

    Vue的filters过滤器是比较常见的一个知识点,下面我将结合时间戳转换的例子带你快速了解filters的用法,感兴趣的朋友一起看看吧
    2021-09-09
  • 使用vue/cli出现defineConfig is not function错误解决办法

    使用vue/cli出现defineConfig is not function错误解决办法

    这篇文章主要给大家介绍了关于使用vue/cli出现defineConfig is not function错误的解决办法,当我们在做打包配置的时候,出现了这个错误,需要的朋友可以参考下
    2023-11-11
  • vue中data里面的数据相互使用方式

    vue中data里面的数据相互使用方式

    这篇文章主要介绍了vue中data里面的数据相互使用方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • vue中当图片地址无效的时候,显示默认图片的方法

    vue中当图片地址无效的时候,显示默认图片的方法

    今天小编就为大家分享一篇vue中当图片地址无效的时候,显示默认图片的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-09-09
  • vue的三种图片引入方式代码实例

    vue的三种图片引入方式代码实例

    这篇文章主要介绍了vue的三种图片引入方式代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • Vue项目自动转换 px 为 rem的实现方法

    Vue项目自动转换 px 为 rem的实现方法

    这篇文章主要介绍了Vue项目自动转换 px 为 rem的实现方法,本文是通过一系列的配置后,转换成热门,具体内容详情大家跟随小编一起通过本文学习吧
    2018-10-10
  • Ant Design Vue Pagination分页组件的封装与使用

    Ant Design Vue Pagination分页组件的封装与使用

    这篇文章主要介绍了Ant Design Vue Pagination分页组件的封装与使用方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • vuex中store的action和mutations用法

    vuex中store的action和mutations用法

    这篇文章主要介绍了vuex中store的action和mutations用法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04
  • vue使用vant中的checkbox实现全选功能

    vue使用vant中的checkbox实现全选功能

    这篇文章主要为大家详细介绍了vue使用vant中的checkbox实现全选功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-11-11
  • 基于better-scroll 实现歌词联动功能的代码

    基于better-scroll 实现歌词联动功能的代码

    BetterScroll 是一款重点解决移动端(已支持 PC)各种滚动场景需求的插件,BetterScroll 是使用纯 JavaScript 实现的,这意味着它是无依赖的。本文基于better-scroll 实现歌词联动功能,感兴趣的朋友跟随小编一起看看吧
    2020-05-05

最新评论