小程序里面如何使用echarts


 

 

https://gitcode.net/mirrors/ecomfe/echarts-for-weixin?utm_source=csdn_github_accelerator (具体官网链接)

 

https://echarts.apache.org/zh/builder.html(在线定制链接)

 

 

微信小程序使用echarts,实现左右双Y轴,动态获取数据,生成折线图

本来使用的是wxcharts,但发现实现不了左右双y轴的效果,就换成echarts

要实现这样的效果,需要以下几步:

(1)去github下载插件,放进自己的项目里

只需要将名称是ec-canvas的文件夹放进自己项目里。

像这样:

 

 (2)分别写小程序的四个文件

1 echart.json
2 {
3  "usingComponents": {
4   "ec-canvas": "../../ec-canvas/ec-canvas"
5  }
6 }

 

 

1 <!--echart.wxml-->
2 <view class="container">
3    <ec-canvas id="myechart" canvas-id="graph" ec="{{ ec }}"></ec-canvas>
4 </view>

1 echart.wxss
2 .container{
3  margin: 0;
4  padding: 0
5 }

 

 

 

echart.js

这里分步写:

第一步:导入 echarts 插件

import * as echarts from '../../ec-canvas/echarts' ;
 

第二步:写在Page外的方法

  1 function echart(chart, leftData, rightData) {//leftData是坐标系左边y轴,rightData是右边y轴
  2  var option = {
  3   //网格
  4   grid: {
  5    bottom: 80,
  6    show: true,
  7    // containLabel: true
  8   },
  9   //图例
 10   legend: {
 11    data: [{
 12      name: 'leftData',
 13      textStyle: { //设置颜色
 14       color: '#6076FF',
 15       fontSize: '14',
 16      }
 17     },
 18     {
 19      name: 'rightData',
 20      textStyle: {
 21       color: '#FFC560',
 22       fontSize: '14',
 23      }
 24     }
 25    ],
 26    x: 'left',
 27    bottom: 15,
 28    left: 30
 29   },
 30   //x轴
 31   xAxis: {
 32    type: 'category',
 33    boundaryGap: false,
 34    disableGrid: true, //绘制X网格
 35    data: ['', '', '', '', '', '', '', '', ''],
 36    splitLine: {
 37     show: true,
 38     // 改变轴线颜色
 39     lineStyle: {
 40      // 使用深浅的间隔色
 41      color: ['#DDDDDD']
 42     }
 43    },
 44    //去掉刻度
 45    axisTick: {
 46     show: false
 47    },
 48    //去掉x轴线
 49    axisLine: {
 50     show: false
 51    },
 52   },
 53   //y轴
 54   yAxis: [{
 55     name: 'mph',
 56     type: 'value',
 57     min: 0,
 58     // max: 40,
 59     //y标轴名称的文字样式
 60     nameTextStyle: {
 61      color: '#FFC560'
 62     },
 63     //网格线
 64     splitLine: {
 65      show: true,
 66      lineStyle: {
 67       color: ['#DDDDDD']
 68      }
 69     },
 70     //去掉刻度
 71     axisTick: {
 72      show: false
 73     },
 74     //去掉y轴线
 75     axisLine: {
 76      show: false
 77     },
 78    },
 79    {
 80     name: 'g',
 81     type: 'value',
 82     // max: 4,
 83     min: 0,
 84     nameTextStyle: {
 85      color: '#6076FF'
 86     },
 87     //去掉刻度
 88     axisTick: {
 89      show: false
 90     },
 91     //去掉y轴线
 92     axisLine: {
 93      show: false
 94     },
 95 
 96    }
 97   ],
 98 
 99   series: [{
100     name: 'leftData',
101     type: 'line',
102     animation: true, //动画效果
103     symbol: 'none',
104     //折线区域
105     areaStyle: {
106      //渐变颜色
107      color: {
108       type: 'linear',
109       x: 0,
110       y: 0,
111       x2: 0,
112       y2: 1,
113       colorStops: [{
114        offset: 0,
115        color: '#6076FF' // 0% 处的颜色
116       }, {
117        offset: 1,
118        color: 'rgba(96,118,255,0.1)' // 100% 处的颜色
119       }],
120       global: false, // 缺省为 false
121      },
122     },
123     //折线宽度
124     lineStyle: {
125      width: 2
126     },
127     color: '#6076FF',
128     data: leftData // 后台传过来的动态数据
129     //设置固定的数据
130     // data: [
131     //  23, 30, 20, 23, 30, 26, 20, 25, 25
132     // ] 
133    },
134    {
135     name: 'rightData',
136     type: 'line',
137     yAxisIndex: 1,
138     animation: true,
139     symbol: 'none',
140     areaStyle: {
141      color: {
142       type: 'linear',
143       x: 0,
144       y: 0,
145       x2: 0,
146       y2: 1,
147       colorStops: [{
148        offset: 0,
149        color: '#FFC560' // 0% 处的颜色
150       }, {
151        offset: 1,
152        color: 'rgba(255, 197, 96,0.3)' // 100% 处的颜色
153       }],
154       global: false, // 缺省为 false
155      },
156     },
157     lineStyle: {
158      width: 2
159     },
160     color: '#FFC560',
161     data: rightData,//后台传过来的动态数据
162     //设置固定的数据
163     // data: [
164     //  2, 1, 0.5, 0.9, 2, 1.0, 0.6, 2, 0.5
165     // ]
166    }]
167  }
168 }

 

 第三步:写在Page里的方法,(包括onLoad(),初始化)

 1 /**
 2  * 页面的初始数据
 3  */
 4 data: {
 5  ec: {
 6   lazyLoad: true //初始化加载
 7  }
 8 },
 9 onLoad: function (options) {
10  let that = this;
11  this.oneComponent = this.selectComponent('#mychart');
12  let url = "xxxxx";
13  let params = {
14   "openId": options.id,
15  };
16 wx.request({
17   url: "xxxx",
18   method: 'POST'
19   data: params,
20   header: header,
21   success: (res) => {
22    that.setData({
23     leftData: xxx,//从后台获取的数据
24     rightData: xxxx //从后台获取的数据
25   });
26   },
27  //给图表加上数据
28  that.initGraph(that.data.leftData, that.data.rightData)
29  })
30 }

 

初始化图表

 1 initGraph: function (leftData, rightData) {
 2  this.oneComponent.init((canvas, width, height) => {
 3   const chart = echarts.init(canvas, null, {
 4    width: width,
 5    height: height
 6   });
 7   initChart(chart, leftData, rightData);
 8   this.chart = chart;
 9   return chart;
10  });
11 }

 

 发包时记得压缩,不然会很大,就这一个都这么大

 

  <ec-canvas id="mychart-dom-line" canvas-id="mychart-line" ec="{{ ec }}" force-use-old-canvas="true"></ec-canvas>    添加这个force-use-old-canvas="true",在pc端调试时可以让图表随屏幕滚动。在真机调试时也要加上这个,否则会报错。在发布上线时把这个去掉,影响滑动图表失真
 
 
真机运行问题,canvans层级过高,影响弹出框,在弹出框上面了,一种解决方式;https://developers.weixin.qq.com/miniprogram/dev/component/cover-view.html

 

 

 

 

显然这种方式不太好

 

 https://blog.csdn.net/liya_nan/article/details/82023761

 

小程序手指滑入事件

https://github.com/qbright/wx-touch-event

小程序通过npm安装插件

https://www.cnblogs.com/rui-yang/p/12374040.html


 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM