登录  /  注册

学习微信小程序如何使用echarts图表

coldplay.xixi
发布: 2020-08-21 17:05:57
原创
2755人浏览过

学习微信小程序如何使用echarts图表

前言

数据统计是我们经常使用的功能,我们一般在 pc 端使用的比较多,大多数用在管理系统中统计数据的分析,最近在做微信小程序的时候也遇到了相同的需求,把数据统计在小程序端以图表的形式展示,这里记录下自己的配置使用过程。

准备

首先百度的 echarts 没有提供小程序版本,这里找了个封装过可以用在微信端的仓库小程序版 echarts,通过这个链接下载最新的包。解压之后有个ec-canvas文件夹就是封装的组件,放到小程序的组件文件夹目录下,以供使 用。

<span style="display: block; background: url(https://my-wechat.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span>├── ec-canvas<br/>│   ├── ec-canvas.js<br/>│   ├── ec-canvas.json<br/>│   ├── ec-canvas.wxml<br/>│   ├── ec-canvas.wxss<br/>│   ├── echarts.min.js<br/>│   └── wx-canvas.js<br/><span class="copy-code-btn">复制代码</span>
登录后复制

使用

  1. 在需要使用的页面配置文件中引入该图表组件
<span style="display: block; background: url(https://my-wechat.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><span class="hljs-string" style="color: #98c379; line-height: 26px;">"usingComponents"</span>: {<br/>    <span class="hljs-attr" style="color: #d19a66; line-height: 26px;">"ec-canvas"</span>: <span class="hljs-string" style="color: #98c379; line-height: 26px;">"../../ec-canvas/ec-canvas"</span><br/>  }<br/><span class="copy-code-btn">复制代码</span>
登录后复制
  1. index.wxml 中,我们创建了一个 组件:
<view class="container">
  <ec-canvas id="mychart-dom-bar" canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>
</view>复制代码
登录后复制
  1. 其中 ec 是一个我们在 index.js 中定义的对象,它使得图表能够在页面加载后被初始化并设置。index.js 的结构如下:
<span style="display: block; background: url(https://my-wechat.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span>Page({<br/>  <span class="hljs-attr" style="color: #d19a66; line-height: 26px;">data</span>: {<br/>    <span class="hljs-attr" style="color: #d19a66; line-height: 26px;">ec</span>: {<br/>      <span class="hljs-attr" style="color: #d19a66; line-height: 26px;">onInit</span>: initChart<br/>    }<br/>  },<br/>  onLoad(){<br/>      <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">// 在需要的地方获取dom</span><br/>      <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">this</span>.echartsComponnet1 = <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">this</span>.selectComponent(<span class="hljs-string" style="color: #98c379; line-height: 26px;">&#39;#mychart-dom-bar1&#39;</span>)<br/>      <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">this</span>.init_echarts1({ <span class="hljs-attr" style="color: #d19a66; line-height: 26px;">value</span>: res.data.rotateSpeed || <span class="hljs-number" style="color: #d19a66; line-height: 26px;">0</span>, <span class="hljs-attr" style="color: #d19a66; line-height: 26px;">name</span>: <span class="hljs-string" style="color: #98c379; line-height: 26px;">&#39;x1000&#39;</span> })<br/>  }<br/>  <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">// 初始化</span><br/>    init_echarts1 (data) {<br/>      <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">this</span>.echartsComponnet1.init(<span class="hljs-function" style="line-height: 26px;">(<span class="hljs-params" style="line-height: 26px;">canvas, width, height</span>) =></span> {<br/>        <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">// 初始化图表</span><br/>        <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">const</span> chart = echarts.init(canvas, <span class="hljs-literal" style="color: #56b6c2; line-height: 26px;">null</span>, {<br/>          <span class="hljs-attr" style="color: #d19a66; line-height: 26px;">width</span>: width,<br/>          <span class="hljs-attr" style="color: #d19a66; line-height: 26px;">height</span>: height<br/>        })<br/>        <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">this</span>.chart = chart<br/>        <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">// setGaugeChartOption1获取到基础配置</span><br/>        chart.setOption(setGaugeChartOption1(data))<br/>        <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">// 注意这里一定要返回 chart 实例,否则会影响事件处理等</span><br/>        <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">return</span> chart<br/>      })<br/>    },<br/>});<br/><span class="copy-code-btn">复制代码</span>
登录后复制
相关学习推荐:微信小程序教程

以上就是学习微信小程序如何使用echarts图表的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:juejin网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号