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

打印 上一主题 下一主题

主题 549|帖子 549|积分 1647

利用的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左右大小
页面中的用法

  1. <template>
  2.     <view class="charts-box">
  3.       <l-echart ref="chart" @finished="init" class="charts-box"></l-echart>
  4.     </view>
  5. </template>
  6. <script>
  7. import LEchart from "@/package-pc/pages/components/lime-echart/components/l-echart/l-echart.vue";
  8. import * as echarts from "@/package-pc/pages/components/lime-echart/static/echarts.min.js";
  9. import option from "@/package-pc/pages/monthreport/option";
  10. export default {
  11.   components: {
  12.     LEchart,
  13.   },
  14.   data() {
  15.     return {
  16.       option: option,
  17.     };
  18.   },
  19.   // 使用组件的finished事件里调用
  20.   methods: {
  21.     async init() {
  22.       const chart = await this.$refs.chart.init(echarts);
  23.       chart.setOption(this.option);
  24.     },
  25.   },
  26. };
  27. </script>
  28. <style scoped>
  29. /* 请根据实际需求修改父元素尺寸,组件自动识别宽高 */
  30. .charts-box {
  31.   width: 100%;
  32.   height: 600px;
  33. }
  34. </style>
复制代码
第一次尝试,修改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方法
  1. import * as echarts from "@/package-pc/pages/components/lime-echart/static/echarts.min.js";
  2. export default {
  3.   name: "lime-echart",
  4.   props: {
  5.     ...
  6.     option: {
  7.       type: Object,
  8.     },
  9.   },
  10.   watch: {
  11.     option: {
  12.       handler() {
  13.         this.setOption(this.option);
  14.       },
  15.       deep: true,
  16.     },
  17.   },
  18.   mounted() {
  19.     this.$nextTick(() => {
  20.       this.$emit("finished");
  21.       this.init();
  22.     });
  23.   },
  24.   methods:{
  25.   ...
  26.    async init(...args) {
  27.       // #ifndef APP-NVUE
  28.       // if (arguments && arguments.length < 1) {
  29.       //   console.error(
  30.       //     "缺少参数:init(echarts, theme?:string, opts?: object, callback?: function)"
  31.       //   );
  32.       //   return;
  33.       // }
  34.       // #endif
  35.       ...
  36.       this.chart = echarts.init(
  37.         config.canvas,
  38.         theme,
  39.         Object.assign({}, config, opts)
  40.       );
  41.       this.chart.setOption(this.option ?? {});
  42.       uni.onWindowResize(() => {
  43.         this.resize();
  44.       });
  45.       ...
  46.     },
  47.   }
复制代码
修改后的页面用法

直接传参option给组件,哀求接口后修改option即可
  1. <template>
  2.     <view class="charts-box">
  3.       <l-echart :option="option1" class="charts-box"></l-echart>
  4.     </view>
  5. </template>
  6. <script>
  7. import LEchart from "@/package-pc/pages/components/lime-echart/components/l-echart/l-echart.vue";
  8. import option from "@/package-pc/pages/monthreport/option";
  9. export default {
  10.   components: {
  11.     LEchart,
  12.   },
  13.   data() {
  14.     return {
  15.       option: option,
  16.     };
  17.   },
  18.   // 修改option即可
  19.   methods: {
  20.     async setText() {
  21.       this.option.title.text = "test"
  22.     },
  23.   },
  24. };
  25. </script>
  26. <style scoped>
  27. /* 请根据实际需求修改父元素尺寸,组件自动识别宽高 */
  28. .charts-box {
  29.   width: 100%;
  30.   height: 600px;
  31. }
  32. </style>
复制代码
第二次尝试,修改l-echart源码,简化组件用法(推荐用法):

做的工作实在就是把echarts放在组件里面利用了,页面中就不用导入了,同时组件内部做了init初始化图表,页面中setOption就行了
  1. import * as echarts from "@/package-pc/pages/components/lime-echart/static/echarts.min.js";
  2. export default {
  3.   name: "lime-echart",
  4.   mounted() {
  5.     this.$nextTick(async () => {
  6.       await this.init();
  7.       this.$emit("finished");
  8.     });
  9.   },
  10.   methods:{
  11.   ...
  12.    async init(...args) {
  13.       // #ifndef APP-NVUE
  14.       // if (arguments && arguments.length < 1) {
  15.       //   console.error(
  16.       //     "缺少参数:init(echarts, theme?:string, opts?: object, callback?: function)"
  17.       //   );
  18.       //   return;
  19.       // }
  20.       // #endif
  21.       ...
  22.       this.chart = echarts.init(
  23.         config.canvas,
  24.         theme,
  25.         Object.assign({}, config, opts)
  26.       );
  27.       uni.onWindowResize(() => {
  28.         this.resize();
  29.       });
  30.       ...
  31.     },
  32.   }
复制代码
修改后的页面用法

  1. <template>
  2.     <view class="charts-box">
  3.        <l-echart
  4.             ref="chart"
  5.             :option="option"
  6.             @finished="init"
  7.             class="charts-box"></l-echart>
  8.     </view>
  9. </template>
  10. <script>
  11. import LEchart from "@/package-pc/pages/components/lime-echart/components/l-echart/l-echart.vue";
  12. import option from "@/package-pc/pages/monthreport/option";
  13. export default {
  14.   components: {
  15.     LEchart,
  16.   },
  17.   data() {
  18.     return {
  19.       option: option,
  20.     };
  21.   },
  22.   // finished回调中设置option,接口请求图表数据也放在这里
  23.   methods: {
  24.     init() {
  25.       this.$refs.chart.setOption(this.option);
  26.     },
  27.   },
  28. };
  29. </script>
  30. <style scoped>
  31. /* 请根据实际需求修改父元素尺寸,组件自动识别宽高 */
  32. .charts-box {
  33.   width: 100%;
  34.   height: 600px;
  35. }
  36. </style>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

美食家大橙子

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表