张涛 1 jaar geleden
bovenliggende
commit
324c28764a

+ 0
- 2
src/subpkg1/components/chart/index.jsx Bestand weergeven

@@ -2,7 +2,6 @@ import React from "react";
2 2
 // import * as echarts from '@/subpkg1/components/ec-canvas/echarts';
3 3
 import * as echarts from "echarts";
4 4
 
5
-import { View } from "@tarojs/components";
6 5
 
7 6
 /**
8 7
  * 使用本组件前, 页面组件必须引入 ec-canvas 组件
@@ -13,7 +12,6 @@ import { View } from "@tarojs/components";
13 12
 export default function Chart(props) {
14 13
   const { option, className, style } = props;
15 14
 
16
-  const [inited, setInited] = React.useState(false);
17 15
   const chartRef = React.useRef();
18 16
   const domRef = React.useRef();
19 17
 

+ 0
- 265
src/subpkg1/components/ec-canvas/ec-canvas.js Bestand weergeven

@@ -1,265 +0,0 @@
1
-import WxCanvas from './wx-canvas';
2
-import * as echarts from './echarts';
3
-
4
-let ctx;
5
-
6
-function compareVersion(v1, v2) {
7
-  v1 = v1.split('.')
8
-  v2 = v2.split('.')
9
-  const len = Math.max(v1.length, v2.length)
10
-
11
-  while (v1.length < len) {
12
-    v1.push('0')
13
-  }
14
-  while (v2.length < len) {
15
-    v2.push('0')
16
-  }
17
-
18
-  for (let i = 0; i < len; i++) {
19
-    const num1 = parseInt(v1[i])
20
-    const num2 = parseInt(v2[i])
21
-
22
-    if (num1 > num2) {
23
-      return 1
24
-    } else if (num1 < num2) {
25
-      return -1
26
-    }
27
-  }
28
-  return 0
29
-}
30
-
31
-Component({
32
-  properties: {
33
-    canvasId: {
34
-      type: String,
35
-      value: 'ec-canvas'
36
-    },
37
-
38
-    ec: {
39
-      type: Object
40
-    },
41
-
42
-    forceUseOldCanvas: {
43
-      type: Boolean,
44
-      value: false
45
-    }
46
-  },
47
-
48
-  data: {
49
-    isUseNewCanvas: false
50
-  },
51
-
52
-  ready: function () {
53
-    // Disable prograssive because drawImage doesn't support DOM as parameter
54
-    // See https://developers.weixin.qq.com/miniprogram/dev/api/canvas/CanvasContext.drawImage.html
55
-    echarts.registerPreprocessor(option => {
56
-      if (option && option.series) {
57
-        if (option.series.length > 0) {
58
-          option.series.forEach(series => {
59
-            series.progressive = 0;
60
-          });
61
-        }
62
-        else if (typeof option.series === 'object') {
63
-          option.series.progressive = 0;
64
-        }
65
-      }
66
-    });
67
-
68
-    if (!this.data.ec) {
69
-      console.warn('组件需绑定 ec 变量,例:<ec-canvas id="mychart-dom-bar" '
70
-        + 'canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>');
71
-      return;
72
-    }
73
-
74
-    if (!this.data.ec.lazyLoad) {
75
-      this.init();
76
-    }
77
-  },
78
-
79
-  methods: {
80
-    init: function (callback) {
81
-      const version = wx.getSystemInfoSync().SDKVersion
82
-
83
-      const canUseNewCanvas = compareVersion(version, '2.9.0') >= 0;
84
-      const forceUseOldCanvas = this.data.forceUseOldCanvas;
85
-      const isUseNewCanvas = canUseNewCanvas && !forceUseOldCanvas;
86
-      this.setData({ isUseNewCanvas });
87
-
88
-      if (forceUseOldCanvas && canUseNewCanvas) {
89
-        console.warn('开发者强制使用旧canvas,建议关闭');
90
-      }
91
-
92
-      if (isUseNewCanvas) {
93
-        // console.log('微信基础库版本大于2.9.0,开始使用<canvas type="2d"/>');
94
-        // 2.9.0 可以使用 <canvas type="2d"></canvas>
95
-        this.initByNewWay(callback);
96
-      } else {
97
-        const isValid = compareVersion(version, '1.9.91') >= 0
98
-        if (!isValid) {
99
-          console.error('微信基础库版本过低,需大于等于 1.9.91。'
100
-            + '参见:https://github.com/ecomfe/echarts-for-weixin'
101
-            + '#%E5%BE%AE%E4%BF%A1%E7%89%88%E6%9C%AC%E8%A6%81%E6%B1%82');
102
-          return;
103
-        } else {
104
-          console.warn('建议将微信基础库调整大于等于2.9.0版本。升级后绘图将有更好性能');
105
-          this.initByOldWay(callback);
106
-        }
107
-      }
108
-    },
109
-
110
-    initByOldWay(callback) {
111
-      // 1.9.91 <= version < 2.9.0:原来的方式初始化
112
-      ctx = wx.createCanvasContext(this.data.canvasId, this);
113
-      const canvas = new WxCanvas(ctx, this.data.canvasId, false);
114
-
115
-      echarts.setCanvasCreator(() => {
116
-        return canvas;
117
-      });
118
-      // const canvasDpr = wx.getSystemInfoSync().pixelRatio // 微信旧的canvas不能传入dpr
119
-      const canvasDpr = 1
120
-      var query = wx.createSelectorQuery().in(this);
121
-      query.select('.ec-canvas').boundingClientRect(res => {
122
-        if (typeof callback === 'function') {
123
-          this.chart = callback(canvas, res.width, res.height, canvasDpr);
124
-        }
125
-        else if (this.data.ec && typeof this.data.ec.onInit === 'function') {
126
-          this.chart = this.data.ec.onInit(canvas, res.width, res.height, canvasDpr);
127
-        }
128
-        else {
129
-          this.triggerEvent('init', {
130
-            canvas: canvas,
131
-            width: res.width,
132
-            height: res.height,
133
-            canvasDpr: canvasDpr // 增加了dpr,可方便外面echarts.init
134
-          });
135
-        }
136
-      }).exec();
137
-    },
138
-
139
-    initByNewWay(callback) {
140
-      // version >= 2.9.0:使用新的方式初始化
141
-      const query = wx.createSelectorQuery().in(this)
142
-      query
143
-        .select('.ec-canvas')
144
-        .fields({ node: true, size: true })
145
-        .exec(res => {
146
-          const canvasNode = res[0].node
147
-          this.canvasNode = canvasNode
148
-
149
-          const canvasDpr = wx.getSystemInfoSync().pixelRatio
150
-          const canvasWidth = res[0].width
151
-          const canvasHeight = res[0].height
152
-
153
-          const ctx = canvasNode.getContext('2d')
154
-
155
-          const canvas = new WxCanvas(ctx, this.data.canvasId, true, canvasNode)
156
-          echarts.setCanvasCreator(() => {
157
-            return canvas
158
-          })
159
-
160
-          if (typeof callback === 'function') {
161
-            this.chart = callback(canvas, canvasWidth, canvasHeight, canvasDpr)
162
-          } else if (this.data.ec && typeof this.data.ec.onInit === 'function') {
163
-            this.chart = this.data.ec.onInit(canvas, canvasWidth, canvasHeight, canvasDpr)
164
-          } else {
165
-            this.triggerEvent('init', {
166
-              canvas: canvas,
167
-              width: canvasWidth,
168
-              height: canvasHeight,
169
-              dpr: canvasDpr
170
-            })
171
-          }
172
-        })
173
-    },
174
-    canvasToTempFilePath(opt) {
175
-      if (this.data.isUseNewCanvas) {
176
-        // 新版
177
-        const query = wx.createSelectorQuery().in(this)
178
-        query
179
-          .select('.ec-canvas')
180
-          .fields({ node: true, size: true })
181
-          .exec(res => {
182
-            const canvasNode = res[0].node
183
-            opt.canvas = canvasNode
184
-            wx.canvasToTempFilePath(opt)
185
-          })
186
-      } else {
187
-        // 旧的
188
-        if (!opt.canvasId) {
189
-          opt.canvasId = this.data.canvasId;
190
-        }
191
-        ctx.draw(true, () => {
192
-          wx.canvasToTempFilePath(opt, this);
193
-        });
194
-      }
195
-    },
196
-
197
-    touchStart(e) {
198
-      if (this.chart && e.touches.length > 0) {
199
-        var touch = e.touches[0];
200
-        var handler = this.chart.getZr().handler;
201
-        handler.dispatch('mousedown', {
202
-          zrX: touch.x,
203
-          zrY: touch.y,
204
-          preventDefault: () => {},
205
-          stopImmediatePropagation: () => {},
206
-          stopPropagation: () => {}
207
-        });
208
-        handler.dispatch('mousemove', {
209
-          zrX: touch.x,
210
-          zrY: touch.y,
211
-          preventDefault: () => {},
212
-          stopImmediatePropagation: () => {},
213
-          stopPropagation: () => {}
214
-        });
215
-        handler.processGesture(wrapTouch(e), 'start');
216
-      }
217
-    },
218
-
219
-    touchMove(e) {
220
-      if (this.chart && e.touches.length > 0) {
221
-        var touch = e.touches[0];
222
-        var handler = this.chart.getZr().handler;
223
-        handler.dispatch('mousemove', {
224
-          zrX: touch.x,
225
-          zrY: touch.y,
226
-          preventDefault: () => {},
227
-          stopImmediatePropagation: () => {},
228
-          stopPropagation: () => {}
229
-        });
230
-        handler.processGesture(wrapTouch(e), 'change');
231
-      }
232
-    },
233
-
234
-    touchEnd(e) {
235
-      if (this.chart) {
236
-        const touch = e.changedTouches ? e.changedTouches[0] : {};
237
-        var handler = this.chart.getZr().handler;
238
-        handler.dispatch('mouseup', {
239
-          zrX: touch.x,
240
-          zrY: touch.y,
241
-          preventDefault: () => {},
242
-          stopImmediatePropagation: () => {},
243
-          stopPropagation: () => {}
244
-        });
245
-        handler.dispatch('click', {
246
-          zrX: touch.x,
247
-          zrY: touch.y,
248
-          preventDefault: () => {},
249
-          stopImmediatePropagation: () => {},
250
-          stopPropagation: () => {}
251
-        });
252
-        handler.processGesture(wrapTouch(e), 'end');
253
-      }
254
-    }
255
-  }
256
-});
257
-
258
-function wrapTouch(event) {
259
-  for (let i = 0; i < event.touches.length; ++i) {
260
-    const touch = event.touches[i];
261
-    touch.offsetX = touch.x;
262
-    touch.offsetY = touch.y;
263
-  }
264
-  return event;
265
-}

+ 0
- 4
src/subpkg1/components/ec-canvas/ec-canvas.json Bestand weergeven

@@ -1,4 +0,0 @@
1
-{
2
-  "component": true,
3
-  "usingComponents": {}
4
-}

+ 0
- 4
src/subpkg1/components/ec-canvas/ec-canvas.wxml Bestand weergeven

@@ -1,4 +0,0 @@
1
-<!-- 新的:接口对其了H5 -->
2
-<canvas wx:if="{{isUseNewCanvas}}" type="2d" class="ec-canvas" canvas-id="{{ canvasId }}" bindinit="init" bindtouchstart="{{ ec.disableTouch ? '' : 'touchStart' }}" bindtouchmove="{{ ec.disableTouch ? '' : 'touchMove' }}" bindtouchend="{{ ec.disableTouch ? '' : 'touchEnd' }}"></canvas>
3
-<!-- 旧的 -->
4
-<canvas wx:else class="ec-canvas" canvas-id="{{ canvasId }}" bindinit="init" bindtouchstart="{{ ec.disableTouch ? '' : 'touchStart' }}" bindtouchmove="{{ ec.disableTouch ? '' : 'touchMove' }}" bindtouchend="{{ ec.disableTouch ? '' : 'touchEnd' }}"></canvas>

+ 0
- 4
src/subpkg1/components/ec-canvas/ec-canvas.wxss Bestand weergeven

@@ -1,4 +0,0 @@
1
-.ec-canvas {
2
-  width: 100%;
3
-  height: 100%;
4
-}

+ 0
- 45
src/subpkg1/components/ec-canvas/echarts.js
Diff onderdrukt omdat het te groot bestand
Bestand weergeven


+ 0
- 111
src/subpkg1/components/ec-canvas/wx-canvas.js Bestand weergeven

@@ -1,111 +0,0 @@
1
-export default class WxCanvas {
2
-  constructor(ctx, canvasId, isNew, canvasNode) {
3
-    this.ctx = ctx;
4
-    this.canvasId = canvasId;
5
-    this.chart = null;
6
-    this.isNew = isNew
7
-    if (isNew) {
8
-      this.canvasNode = canvasNode;
9
-    }
10
-    else {
11
-      this._initStyle(ctx);
12
-    }
13
-
14
-    // this._initCanvas(zrender, ctx);
15
-
16
-    this._initEvent();
17
-  }
18
-
19
-  getContext(contextType) {
20
-    if (contextType === '2d') {
21
-      return this.ctx;
22
-    }
23
-  }
24
-
25
-  // canvasToTempFilePath(opt) {
26
-  //   if (!opt.canvasId) {
27
-  //     opt.canvasId = this.canvasId;
28
-  //   }
29
-  //   return wx.canvasToTempFilePath(opt, this);
30
-  // }
31
-
32
-  setChart(chart) {
33
-    this.chart = chart;
34
-  }
35
-
36
-  addEventListener() {
37
-    // noop
38
-  }
39
-
40
-  attachEvent() {
41
-    // noop
42
-  }
43
-
44
-  detachEvent() {
45
-    // noop
46
-  }
47
-
48
-  _initCanvas(zrender, ctx) {
49
-    zrender.util.getContext = function () {
50
-      return ctx;
51
-    };
52
-
53
-    zrender.util.$override('measureText', function (text, font) {
54
-      ctx.font = font || '12px sans-serif';
55
-      return ctx.measureText(text);
56
-    });
57
-  }
58
-
59
-  _initStyle(ctx) {
60
-    ctx.createRadialGradient = () => {
61
-      return ctx.createCircularGradient(arguments);
62
-    };
63
-  }
64
-
65
-  _initEvent() {
66
-    this.event = {};
67
-    const eventNames = [{
68
-      wxName: 'touchStart',
69
-      ecName: 'mousedown'
70
-    }, {
71
-      wxName: 'touchMove',
72
-      ecName: 'mousemove'
73
-    }, {
74
-      wxName: 'touchEnd',
75
-      ecName: 'mouseup'
76
-    }, {
77
-      wxName: 'touchEnd',
78
-      ecName: 'click'
79
-    }];
80
-    eventNames.forEach(name => {
81
-      this.event[name.wxName] = e => {
82
-        const touch = e.touches[0];
83
-        this.chart.getZr().handler.dispatch(name.ecName, {
84
-          zrX: name.wxName === 'tap' ? touch.clientX : touch.x,
85
-          zrY: name.wxName === 'tap' ? touch.clientY : touch.y,
86
-          preventDefault: () => {},
87
-          stopImmediatePropagation: () => {},
88
-          stopPropagation: () => {}
89
-        });
90
-      };
91
-    });
92
-  }
93
-
94
-  set width(w) {
95
-    if (this.canvasNode) this.canvasNode.width = w
96
-  }
97
-  set height(h) {
98
-    if (this.canvasNode) this.canvasNode.height = h
99
-  }
100
-
101
-  get width() {
102
-    if (this.canvasNode)
103
-      return this.canvasNode.width
104
-    return 0
105
-  }
106
-  get height() {
107
-    if (this.canvasNode)
108
-      return this.canvasNode.height
109
-    return 0
110
-  }
111
-}