当前位置:   article > 正文

微信小程序图表插件wxcharts.js使用小记,Echarts.js,推荐uCharts.js

wxcharts.js

一、wxcharts.js

参考地址:https://github.com/xiaolin3303/wx-charts/issues/58

参数含义参考:https://www.cnblogs.com/zxf100/p/9956415.html

1.1.wxml

  1. <!--pages/wxcharts/wxcharts.wxml-->
  2. <canvas canvas-id="pieCanvas" disable-scroll="true" class="canvas"></canvas>
  3. <canvas canvas-id="ringCanvas" disable-scroll="true" class="canvas canvas2"></canvas>
  4. <canvas canvas-id="lineCanvas" disable-scroll="true" class="canvas"></canvas>
  5. <canvas canvas-id="columnCanvas" disable-scroll="true" class="canvas"></canvas>
  6. <canvas canvas-id="areaCanvas" disable-scroll="true" class="canvas"></canvas>
  7. <canvas canvas-id="radarCanvas" disable-scroll="true" class="canvas canvas2"></canvas>

1.2.wxss

  1. /* pages/wxcharts/wxcharts.wxss */
  2. .canvas {
  3. width: 750rpx;
  4. height: 500rpx;
  5. }
  6. .canvas2{
  7. height: 400rpx;
  8. }

1.3.js(须自己下载wxcharts.js,可前往https://github.com/xiaolin3303/wx-charts/tree/master/dist,下载)

  1. // pages/wxcharts/wxcharts.js
  2. //首先引入wxcharts.js插件
  3. var wxCharts = require("../wxcharts.js");
  4. //定义记录初始屏幕宽度比例,便于初始化
  5. var windowW=0;
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. },
  12. /**
  13. * 生命周期函数--监听页面加载
  14. */
  15. onLoad: function (options) {
  16. // 屏幕宽度
  17. this.setData({
  18. imageWidth: wx.getSystemInfoSync().windowWidth
  19. }) ;
  20. console.log(this.data.imageWidth) ;
  21. //计算屏幕宽度比列
  22. windowW = this.data.imageWidth/375;
  23. console.log(windowW);
  24. },
  25. /**
  26. * 生命周期函数--监听页面显示
  27. */
  28. onShow: function () {
  29. // pieCanvas
  30. new wxCharts({
  31. animation: true, //是否有动画
  32. canvasId: 'pieCanvas',
  33. type: 'pie',
  34. series: [{
  35. name: '成交量1',
  36. data: 15,
  37. }, {
  38. name: '成交量2',
  39. data: 35,
  40. }, {
  41. name: '成交量3',
  42. data: 78,
  43. }],
  44. width: (375 * windowW),
  45. height: (250 * windowW),
  46. dataLabel: true,
  47. });
  48. // ringCanvas
  49. new wxCharts({
  50. animation: true,
  51. canvasId: 'ringCanvas',
  52. type: 'ring',
  53. extra: {
  54. ringWidth: 25,
  55. pie: {
  56. offsetAngle: -45
  57. }
  58. },
  59. title: {
  60. name: '70%',
  61. color: '#7cb5ec',
  62. fontSize: 25
  63. },
  64. subtitle: {
  65. name: '收益率',
  66. color: '#666666',
  67. fontSize: 15
  68. },
  69. series: [{
  70. name: '成交量1',
  71. data: 15,
  72. stroke: false
  73. }, {
  74. name: '成交量2',
  75. data: 35,
  76. stroke: false
  77. }, {
  78. name: '成交量3',
  79. data: 78,
  80. stroke: false
  81. }, {
  82. name: '成交量4',
  83. data: 63,
  84. stroke: false
  85. }],
  86. disablePieStroke: true,
  87. width: (375 * windowW),
  88. height: (200 * windowW),
  89. dataLabel: false,
  90. legend: false,
  91. padding: 0
  92. });
  93. //lineCanvas
  94. new wxCharts({
  95. canvasId: 'lineCanvas',
  96. type: 'line',
  97. categories: ['2016-1', '2017-1', '2018-1', '2019-1', '2020-1', '2021-1', '2022-1', '2023-1', '2024-1', '2025-1', '2026-1'],
  98. animation: true,
  99. background: '#f5f5f5',
  100. series: [{
  101. name: '成交量1',
  102. data: [16, 12, 15, 11, 13, 17, 18, 16, 15, 14],
  103. format: function (val, name) {
  104. return val.toFixed(2) + '万';
  105. }
  106. }, {
  107. name: '成交量2',
  108. data: [2, 0, 0, 3, null, 4, 0, 0, 2, 0],
  109. format: function (val, name) {
  110. return val.toFixed(2) + '万';
  111. }
  112. }],
  113. xAxis: {
  114. disableGrid: true
  115. },
  116. yAxis: {
  117. title: '成交金额 (万元)',
  118. format: function (val) {
  119. return val.toFixed(2);
  120. },
  121. min: 0
  122. },
  123. width: (375 * windowW),
  124. height: (200 * windowW),
  125. dataLabel: false,
  126. dataPointShape: true,
  127. extra: {
  128. lineStyle: 'curve'
  129. }
  130. });
  131. //columnCanvas
  132. new wxCharts({
  133. canvasId: 'columnCanvas',
  134. type: 'column',
  135. animation: true,
  136. categories: [2001,2002,2003,2004,2005],
  137. series: [{
  138. name: '成交量',
  139. data: [15.00,20.00,45.00,37.00],
  140. format: function (val, name) {
  141. return val.toFixed(2) + '万';
  142. }
  143. }, {
  144. name: '成交量',
  145. data: [6.00, 9.00, 20.00, 45.00],
  146. format: function (val, name) {
  147. return val.toFixed(2) + '万';
  148. }
  149. }],
  150. yAxis: {
  151. format: function (val) {
  152. return val + '万';
  153. },
  154. title: 'hello',
  155. min: 0
  156. },
  157. xAxis: {
  158. disableGrid: false,
  159. type: 'calibration'
  160. },
  161. extra: {
  162. column: {
  163. width: 15
  164. }
  165. },
  166. width: (375 * windowW),
  167. height: (200 * windowW),
  168. });
  169. //areaCanvas
  170. new wxCharts({
  171. canvasId: 'areaCanvas',
  172. type: 'area',
  173. categories: ['1', '2', '3', '4', '5', '6'],
  174. animation: true,
  175. series: [{
  176. name: '成交量1',
  177. data: [32, 45, 0, 56, 33, 34],
  178. format: function (val) {
  179. return val.toFixed(2) + '万';
  180. }
  181. }, {
  182. name: '成交量2',
  183. data: [15, 20, 45, 37, 4, 80],
  184. format: function (val) {
  185. return val.toFixed(2) + '万';
  186. },
  187. }],
  188. yAxis: {
  189. title: '成交金额 (万元)',
  190. format: function (val) {
  191. return val.toFixed(2);
  192. },
  193. min: 0,
  194. fontColor: '#8085e9',
  195. gridColor: '#8085e9',
  196. titleFontColor: '#f7a35c'
  197. },
  198. xAxis: {
  199. fontColor: '#7cb5ec',
  200. gridColor: '#7cb5ec'
  201. },
  202. extra: {
  203. legendTextColor: '#cb2431'
  204. },
  205. width: (375 * windowW),
  206. height: (200 * windowW),
  207. });
  208. //radarCanvas
  209. new wxCharts({
  210. canvasId: 'radarCanvas',
  211. type: 'radar',
  212. categories: ['1', '2', '3', '4', '5', '6'],
  213. series: [{
  214. name: '成交量1',
  215. data: [90, 110, 125, 95, 87, 122]
  216. }],
  217. width: (375 * windowW),
  218. height: (200 * windowW),
  219. extra: {
  220. radar: {
  221. max: 50
  222. }
  223. }
  224. });
  225. },
  226. })

1.4.效果

1.5.说明

(1)推荐地址:https://github.com/xiaolin3303/wx-charts

(2)案例参考地址:https://github.com/xiaolin3303/wx-charts/issues/58

(3)参数含义参考:https://www.cnblogs.com/zxf100/p/9956415.html

(如果不需要在图表中展示数值,只需 dataLabel: false, 即可)

二、ECharts

有需要的可以去看一下,点击跳转我转载的一篇文章 ECharts;

三、uCharts.js

    1.在码云上看到了一个在2019.5.20号成立的团队开发的一个图表插件,很不错,可以试试这个,包括小程序,百度,今日头条,支付宝等等都有,使用方法也比较详细,提供有范例,也比较炫酷,下载范例直接可以运行。

    2.码云地址:https://gitee.com/uCharts/uCharts

    3.支持的小程序

    4.使用范例:

    

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/203722
推荐阅读