uniapp引入插件市场echarts图表(l-echart)实现小程序端图表,并修改源码简化使用

注意事项
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左右大小

页面中的用法
<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>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
第一次尝试,修改l-echart源码,简化组件用法(不推荐用法):
这样写有一个重大问题,uniapp不支持props传递的对象里面属性有function,而echarts这样的属性很多,所以不推荐这样修改源码,这里只是记录一下我尝试封装的思路过程
1.组件中直接引入echarts.min.js
2.props增加option传参
3.watch中监听option传参
4.mounted中直接执行init方法初始化图表
5.init方法中调用setOption方法
6.加入uni.onWindowResize方法监听宽高变化,然后调用原本就实现的resize方法

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(() => {
      this.$emit("finished");
      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();
      });
      ...
    },
  }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
修改后的页面用法
直接传参option给组件,请求接口后修改option即可

<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>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
第二次尝试,修改l-echart源码,简化组件用法(推荐用法):
做的工作其实就是把echarts放在组件里面使用了,页面中就不用导入了,同时组件内部做了init初始化图表,页面中setOption就行了

import * as echarts from "@/package-pc/pages/components/lime-echart/static/echarts.min.js";
export default {
  name: "lime-echart",
  mounted() {
    this.$nextTick(async () => {
      await this.init();
      this.$emit("finished");
    });
  },
  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)
      );
      uni.onWindowResize(() => {
        this.resize();
      });
      ...
    },
  }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
修改后的页面用法
<template>
    <view class="charts-box">
       <l-echart
            ref="chart"
            :option="option"
            @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 option from "@/package-pc/pages/monthreport/option";
export default {
  components: {
    LEchart,
  },
  data() {
    return {
      option: option,
    };
  },
  // finished回调中设置option,接口请求图表数据也放在这里
  methods: {
    init() {
      this.$refs.chart.setOption(this.option);
    },
  },
};
</script>

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值