vue项目中使用ECharts,避免浏览器卡死完整版

在vue项目中常遇到echarts图表使用过多页面卡死的情况,以下只展示了一个折线图,可通过同样方式增加多个,通过以下方法每执行一次清除一次即可。

1、从npm获取

npm install echarts --save

2、在 main.js引入echarts

import * as echarts from 'echarts';

Vue.prototype.$echarts = echarts;

3、完整代码(图表中加了其他样式,可根据自己喜欢更改)

<template>
  <div>
    <div class="container">
      <div id="linePicture"></div>
    </div>
  </div>
</template>
<script>
//全局定义chart,离开页面时直接清除chart;
let chart = null;
export default {
  data() {
    return {};
  },
  // 页面初始化挂载dom
  mounted() {
    window.addEventListener("beforeunload", this.clearChart);
    this.linePictures("linePicture");
  },
  methods: {
    clearChart() {
      // 手动触发 destroy 相关的生命周期
      this.$destroy();
    },
    linePictures(id) {
      //心率/呼吸率
	  // 时间轴
      var xdata = [
        "00:38",
        "00:39",
        "00:40",
        "00:41",
        "00:42",
        "00:43",
        "00:44",
        "00:45",
        "00:46",
        "00:47",
        "00:48",
        "00:49",
        "00:50",
        "00:51",
        "00:52",
        "00:53",
        "00:54",
        "00:55",
        "00:56",
        "00:57",
        "00:58",
        "00:59",
        "01:00",
      ];
	  //线条一数据
      var adata = [
        0, 17, 17, 17, 18, 20, 22, 23, 0, 18, 19, 19, 19, 19, 19, 18, 18, 18, 0,
        0, 21, 19, 0,
      ];
	  //线条二数据
      var bdata = [
        0, 0, 54, 54, 54, 54, 54, 54, 53, 53, 53, 54, 54, 54, 54, 54, 53, 53, 0,
        0, 59, 59, 57,
      ];
	  //判断为0不展示,页面展示为0的线不渲染,如想将0也展示可删除;
      for (var i = 0; i < adata.length; i++) {
        if (adata[i] == 0) {
          adata[i] = "-";
        }
      }
	  //第二条线判断同上;
      for (var i = 0; i < bdata.length; i++) {
        if (bdata[i] == 0) {
          bdata[i] = "-";
        }
      }
	  //判断chart,清除上次数据,防止消耗内存;
      if (chart !== null) {
        chart.dispose();
        chart = null;
      }
      if (!chart) {
        chart = this.$echarts.init(document.getElementById(id), "classic", {
          renderer: "svg",
        });
        chart.setOption({
          title: {
            top: -5,
            left: 0,
            text: "{a|}" + " " + "心率与呼吸率动态",//展示的标题
            textStyle: {
              fontSize: 17,
              color: "#000",
              fontWeight: 500,
              rich: {
                a: {
                  color: "#FFF", //设置 动态数据字体的颜色
                  fontSize: "12", //设置 动态数据字体的大小
                  height: 23,
                  width: 23,
                  backgroundColor: {
                    image: require(`../../../assets/heart.png`),//头部标题图片
                  },
                },
              },
            },
          },
          tooltip: {
            trigger: "axis",
          },
          legend: {
            top: 4, //图例距离左的距离
            right: 7,
            y: "center", //图例上下居中
            orient: "vertical",
            itemGap: 32,
            height: "20",
            selectedMode: false, // 图例选择的模式,
            data: ["呼吸率", "心率"],
            textStyle: {
              fontSize: 13,
              color: "#989ca7",
            },
            // data:types
          },
          grid: {
            top: "29%",
            right: "2%",
            left: "1%",
            bottom: "5%",
            containLabel: true,
          },
          xAxis: {
            //横坐标
            name: "",
            scale: true,
            type: "category",
            boundaryGap: true,
            data: xdata,
            nameGap: 25, //坐标轴名称与轴线之间的距离        (用数字表示)
            nameRotate: 0, //坐标轴名字旋转的角度值
            inverse: false, //是否为反向坐标轴
            axisLine: {
              //坐标轴轴线设置
              show: true, //是否显示坐标轴轴线
            },
            axisLabel: {
              //坐标轴刻度文字的设置
              show: true, //是否显示
              fontSize: 11, //坐标轴刻度文字的大小         (用数字表示)
            },
          },
          yAxis: {
            name: "",
            type: "value",
            show: true, //是否显示 y 轴
            position: "bottom", //y轴的位置        (可选位置top  bottom)
            offset: 0, //y轴相对于默认位置的偏移,在相同的 position 上有多个 X 轴的时候有用
            nameGap: 25, //坐标轴名称与轴线之间的距离        (用数字表示)
            axisLine: {
              //坐标轴轴线设置
              show: true, //是否显示坐标轴轴线
              lineStyle: {
                //坐标轴的线
                width: 1.2, //线的粗细程度    (用数字表示)
                type: "solid", //线的类型        (可选solid  dotted  dashed)
                opacity: 1, //线的透明度        (用0~1的小数表示)
              },
            },
            axisTick: {
              //坐标轴刻度设置
              show: true, //是否显示坐标轴刻度
              alignWithLabel: true, //刻度线是否和标签对齐
              length: 5, //坐标轴刻度长度
              lineStyle: {
                //坐标轴刻度的样式
                width: 1.2, //坐标轴刻度的粗细程度    (用数字表示)
                type: "solid", //坐标轴刻度的类型        (可选solid  dotted  dashed)
              },
            },
            axisLabel: {
              //坐标轴刻度文字的设置
              show: true, //是否显示
              inside: false, //坐标轴刻度文字指向        (true表示向上   false表示向下)
              margin: 10, //坐标轴刻度文字与轴线之间的距离
              fontSize: 10, //坐标轴刻度文字的大小         (用数字表示)
              padding: [8, 0, 2, -5], //坐标轴刻度文字的边距        (上右下左)
            },
            splitLine: {
              //网格线
              show: true, //是否显示
              lineStyle: {
                //网格线样式
                color: "#b7b5b5", //网格线颜色
                width: 1.3, //网格线的加粗程度
                type: "dashed", //网格线类型
              },
            },
          },
          series: [
            {
              name: "呼吸率",
              type: "line",
              smooth: true,
              symbol: "none",
              data: adata,
            },
            {
              name: "心率",
              type: "line",
              smooth: true,
              symbol: "none",
              data: bdata,
            },
          ],
        });
      }
    },
  },
  //离开页面时需清除图表,避免占用内存,导致页面卡死;
  beforeDestroy() {
    if (!chart) {
      return;
    }
    chart.dispose();
    chart = null;
    // 清空 beforeunload 事件处理函数
    window.removeEventListener("beforeunload", this.clearChart);
  },
};
</script>
<style scoped>
.container {
  width: 50%;
  height: 400px;
  padding: 15px 30px;
  background: #fff;
  margin-bottom: 20px;
}

#linePicture {
  width: 100%;
  height: 380px;
}
</style>

效果展示:

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Vue项目使用echarts图表,你需要先安装echarts库。可以通过npm或yarn来安装echarts: ```bash npm install echarts --save # 或者 yarn add echarts ``` 然后在Vue组件引入echarts使用它来绘制图表。以下是一个简单的例子: ```vue <template> <div class="chart-container"> <div ref="echarts" class="echarts"></div> </div> </template> <script> import echarts from 'echarts' export default { name: 'EchartsDemo', data() { return { chartData: [ { name: '一月', value: 100 }, { name: '二月', value: 200 }, { name: '三月', value: 300 }, { name: '四月', value: 400 }, { name: '五月', value: 500 }, { name: '六月', value: 600 } ] } }, mounted() { this.drawChart() }, methods: { drawChart() { const chartDom = this.$refs.echarts const chart = echarts.init(chartDom) const option = { xAxis: { type: 'category', data: this.chartData.map(item => item.name) }, yAxis: { type: 'value' }, series: [{ data: this.chartData.map(item => item.value), type: 'bar' }] } chart.setOption(option) } } } </script> <style scoped> .echarts { width: 100%; height: 400px; } </style> ``` 在这个例子,我们在组件的`mounted`生命周期钩子调用了`drawChart`方法来绘制图表。方法首先通过`this.$refs.echarts`获取到一个DOM元素,然后使用`echarts.init`方法初始化echarts实例。接着,我们通过设置`option`对象来定义图表的配置,最后调用`chart.setOption`方法来渲染图表。 上面的例子演示了如何使用echarts来绘制一个简单的柱状图。你可以根据需要调整`option`对象的配置来实现其他类型的图表,例如折线图、饼图等等。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值