uniapp引入插件市场echarts图表(l-echart)实现小程序端图表,并修改源码实现watch监听option

使用的uniapp插件:l-echart

https://ext.dcloud.net.cn/plugin?id=4899

注意事项

1.因为小程序有主包分包大小限制,并且uni_modules中的包也会算在主包体积中,而我项目中的图表是在分包中使用的,所以我移动uni_modules中的l-echart图表组件到分包目录组件文件夹中

2.精简echarts.min.js体积,因为需求中只需要柱图和饼图,所以我去https://echarts.apache.org/zh/builder.html下载指定的 echarts 组件压缩包,然后替换l-echart中的echarts.min.js文件,只需要500kb左右大小

页面中使用的用法
dart 复制代码
<template>
    <view class="charts-box">
      <l-echart ref="chart" @finished="init" class="charts-box"></l-echart>
    </view>
</template>

<script>
import LEchart from "@/package-pc/pages/components/lime-echart/components/l-echart/l-echart.vue";
import * as echarts from "@/package-pc/pages/components/lime-echart/static/echarts.min.js";
import option from "@/package-pc/pages/monthreport/option";
export default {
  components: {
    LEchart,
  },
  data() {
    return {
      option: option,
    };
  },
  // 使用组件的finished事件里调用
  methods: {
    async init() {
      const chart = await this.$refs.chart.init(echarts);
      chart.setOption(this.option);
    },
  },
};
</script>

<style scoped>
/* 请根据实际需求修改父元素尺寸,组件自动识别宽高 */
.charts-box {
  width: 100%;
  height: 600px;
}
</style>
修改l-echart源码,简化组件用法:

1.组件中直接引入echarts.min.js

2.props增加option传参

3.watch中监听option传参

4.mounted中直接执行init方法初始化图表

5.init方法中调用setOption方法

6.加入uni.onWindowResize方法监听宽高变化,然后调用原本就实现的resize方法

js 复制代码
import * as echarts from "@/package-pc/pages/components/lime-echart/static/echarts.min.js";
export default {
  name: "lime-echart",
  props: {
    ...
    option: {
      type: Object,
    },
  },
  watch: {
    option: {
      handler() {
        this.setOption(this.option);
      },
      deep: true,
    },
  },
  mounted() {
    this.$nextTick(async () => {
      this.$emit("finished");
      await this.init();
    });
  },
  methods:{
  ...
   async init(...args) {
      // #ifndef APP-NVUE
      // if (arguments && arguments.length < 1) {
      //   console.error(
      //     "缺少参数:init(echarts, theme?:string, opts?: object, callback?: function)"
      //   );
      //   return;
      // }
      // #endif
      ...
      this.chart = echarts.init(
        config.canvas,
        theme,
        Object.assign({}, config, opts)
      );
      this.chart.setOption(this.option ?? {});
      uni.onWindowResize(() => {
        this.resize();
      });
      ...
    },
  }
修改后的页面用法

直接传参option给组件,请求接口后修改option即可

dart 复制代码
<template>
    <view class="charts-box">
      <l-echart :option="option1" class="charts-box"></l-echart>
    </view>
</template>

<script>
import LEchart from "@/package-pc/pages/components/lime-echart/components/l-echart/l-echart.vue";
import option from "@/package-pc/pages/monthreport/option";
export default {
  components: {
    LEchart,
  },
  data() {
    return {
      option: option,
    };
  },
  // 修改option即可
  methods: {
    async setText() {
      this.option.title.text = "test"
    },
  },
};
</script>

<style scoped>
/* 请根据实际需求修改父元素尺寸,组件自动识别宽高 */
.charts-box {
  width: 100%;
  height: 600px;
}
</style>
相关推荐
万少3 小时前
HarmonyOS 开发必会 5 种 Builder 详解
前端·harmonyos
橙序员小站5 小时前
Agent Skill 是什么?一文讲透 Agent Skill 的设计与实现
前端·后端
炫饭第一名8 小时前
速通Canvas指北🦮——基础入门篇
前端·javascript·程序员
王晓枫8 小时前
flutter接入三方库运行报错:Error running pod install
前端·flutter
符方昊8 小时前
React 19 对比 React 16 新特性解析
前端·react.js
ssshooter8 小时前
又被 Safari 差异坑了:textContent 拿到的值居然没换行?
前端
曲折8 小时前
Cesium-气象要素PNG色斑图叠加
前端·cesium
Forever7_8 小时前
Electron 淘汰!新的桌面端框架 更强大、更轻量化
前端·vue.js
Angelial9 小时前
Vue3 嵌套路由 KeepAlive:动态缓存与反向配置方案
前端·vue.js
jiayu9 小时前
Angular学习笔记24:Angular 响应式表单 FormArray 与 FormGroup 相互嵌套
前端