微信小程序使用echarts

编辑: admin 分类: 电脑知识 发布时间: 2023-06-14 来源:互联网
第一种使用方法wxml
  1. <view style="height: 500rpx;">
  2. <ec-canvas id="mychart-dom-line" canvas-id="mychart-line" ec="{{ ec }}"></ec-canvas>
  3. </view>
  4. <button bindtap="pie" type="primary">饼图</button>
jshttps://github.com/ecomfe/echarts-for-weixin找到里面的ec-canvas文件夹
  1. import * as echarts from '../ec-canvas/echarts';
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. ec: {
  8. onInit:Object
  9. }
  10. },
  11. drawWorkOrderCharts() {
  12. var oneComponent = this.selectComponent('#mychart-dom-line2')
  13. oneComponent.init((canvas, width, height,dpr) => {
  14. const chart = echarts.init(canvas, null, {
  15. width: width,
  16. height: height,
  17. devicePixelRatio: dpr
  18. });
  19. canvas.setChart(chart);//canvas画布上创建一个图表
  20. chart.setOption({
  21. tooltip: {
  22. trigger: 'axis',
  23. axisPointer: { // 坐标轴指示器,坐标轴触发有效
  24. type: 'cross', // 默认为直线,可选为:'line' | 'shadow' | 'cross'
  25. axis: "x",
  26. },
  27. },
  28. grid: {
  29. top: 15,
  30. left: 15,
  31. bottom: 15,
  32. containLabel: true
  33. },
  34. xAxis: {
  35. type: 'category',
  36. data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
  37. },
  38. yAxis: {},
  39. series: [{
  40. name: '工单总数',
  41. type: 'line',
  42. data: [120, 130, 140, 133, 122, 167, 176],
  43. }, {
  44. name: '完成总数',
  45. type: 'line',
  46. data: [110, 120, 140, 80, 112, 160, 170],
  47. }]
  48. });
  49. this.setData({
  50. ec: {
  51. onInit: () => chart
  52. }
  53. })
  54. });
  55. },
  56. /**
  57. * 生命周期函数--监听页面加载
  58. */
  59. onLoad: function (options) {
  60. this.drawWorkOrderCharts()//也可以直接调用,但是会有提示。
  61. },
  62. })
第二种使用方法
  1. import * as echarts from '~/ec-canvas/echarts';//这里我设置的绝对路径
  2. var option = [];//图表配置项 声明
  3. // 初始化图表函数 开始
  4. let chart = null;
  5. function initChart(canvas, width, height, dpr) {
  6. chart = echarts.init(canvas, null, {
  7. width: width,
  8. height: height,
  9. devicePixelRatio: dpr
  10. })
  11. canvas.setChart(chart)
  12. return chart;
  13. }
  14. Page({
  15. data: {
  16. topLabel: '检验详情',
  17. ec: {
  18. onInit: initChart
  19. },
  20. Pie:null,
  21. x:null,
  22. y:null,
  23. },
  24. drawWorkOrderCharts() { //图表
  25. setTimeout(() => {
  26. let arr2 = [23, 24, 25, 26, 27, 28, 29]; // X轴假数据
  27. let datas2 = [773, 768, 865, 937, 148, 487, 410]; // Y轴假数据
  28. let Pie = [
  29. { value: 23, name: '773' },
  30. { value: 24, name: '768' },
  31. { value: 25, name: '865' },
  32. { value: 26, name: '937' },
  33. { value: 27, name: '148' },
  34. { value: 28, name: '487' },
  35. { value: 29, name: '410' }
  36. ]; // 饼图假数据
  37. chart.setOption({
  38. // backgroundColor:'#578EFF',
  39. tooltip: {
  40. trigger: "axis",
  41. axisPointer: {
  42. type: "shadow"
  43. }
  44. },
  45. grid: {
  46. left: 40,
  47. right: 10,
  48. bottom: 30,
  49. top: 20
  50. },
  51. xAxis: [{
  52. type: 'category',
  53. data: ['收缩压(mmHg)', '舒张压 (mmHg)', '脉搏 (bpm)'],
  54. axisLabel: {
  55. fontSize: 9
  56. }
  57. }],
  58. yAxis: [{
  59. type: "value"
  60. }],
  61. series: [{
  62. barWidth: "20%",
  63. data: [{
  64. value: 120,
  65. itemStyle: {
  66. color: '#578EFF',
  67. }
  68. },
  69. {
  70. value: 200,
  71. itemStyle: {
  72. color: '#51AEFF',
  73. }
  74. },
  75. {
  76. value: 150,
  77. itemStyle: {
  78. color: '#78A4FF',
  79. }
  80. }
  81. ],
  82. type: 'bar'
  83. }]
  84. })
  85. this.setData({
  86. x: arr2,
  87. y: datas2,
  88. Pie: Pie,
  89. })
  90. }, 1000);
  91. },
  92. pie() {//饼图
  93. option = {
  94. tooltip: {
  95. trigger: 'item'
  96. },
  97. legend: {
  98. top: '5%',
  99. left: 'center'
  100. },
  101. series: [
  102. {
  103. name: '收益明细',
  104. type: 'pie',
  105. radius: ['40%', '70%'],
  106. avoidLabelOverlap: false,
  107. itemStyle: {
  108. borderRadius: 10,
  109. borderColor: '#fff',
  110. borderWidth: 2
  111. },
  112. label: {
  113. show: false,
  114. position: 'center'
  115. },
  116. emphasis: {
  117. label: {
  118. show: true,
  119. fontSize: '40',
  120. fontWeight: 'bold'
  121. }
  122. },
  123. labelLine: {
  124. show: false
  125. },
  126. data: this.data.Pie,
  127. }
  128. ]
  129. };
  130. chart.setOption(option)
  131. },
  132. reques() {//请求方式数据的方式
  133. wx.request({
  134. url: '填入自己的接口,把下面的假数据更改即可', //请求数据信息的接口地址
  135. data: {
  136. },
  137. header: {
  138. 'content-type': 'application/json' // 默认值
  139. },
  140. success: (res) => {
  141. let arr2 = [23, 24, 25, 26, 27, 28, 29]; // X轴假数据
  142. let datas2 = [773, 768, 865, 937, 148, 487, 410]; // Y轴假数据
  143. let Pie = [
  144. { value: 23, name: '773' },
  145. { value: 24, name: '768' },
  146. { value: 25, name: '865' },
  147. { value: 26, name: '937' },
  148. { value: 27, name: '148' },
  149. { value: 28, name: '487' },
  150. { value: 29, name: '410' }
  151. ]; // 饼图假数据
  152. // 显示Echarts图表类型信息,可以去Echarts官网复制粘贴
  153. // 默认输出柱状图的配置项
  154. option = {
  155. xAxis: {
  156. type: 'category',
  157. data: arr2
  158. },
  159. yAxis: {
  160. type: 'value'
  161. },
  162. series: [{
  163. label: { //数据显示
  164. show: true,
  165. color: 'inherit',
  166. position: 'top',
  167. fontSize: 10,
  168. },
  169. data: datas2,
  170. type: 'bar',
  171. showBackground: true,
  172. backgroundStyle: {
  173. color: 'rgba(180, 180, 180, 0.2)'
  174. }
  175. },
  176. ]
  177. }
  178. // 输出到页面
  179. chart.setOption(option);
  180. // 数据获取 结束
  181. this.setData({
  182. x: arr2,
  183. y: datas2,
  184. Pie: Pie,
  185. })
  186. }
  187. })
  188. },
  189. onLoad: function (options) {
  190. this.drawWorkOrderCharts()
  191. },
  192. onReady: function () { },
  193. })
【感谢龙石为本站提供api接口平台 http://www.longshidata.com/pages/apigateway.html】