初始化(包含登录模块)
This commit is contained in:
123
components/mpvue-echarts/src/echarts.vue
Normal file
123
components/mpvue-echarts/src/echarts.vue
Normal file
@@ -0,0 +1,123 @@
|
||||
<template>
|
||||
<canvas v-if="canvasId" class="ec-canvas" :id="canvasId" :canvasId="canvasId" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd" @error="error"></canvas>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import WxCanvas from './wx-canvas';
|
||||
|
||||
export default {
|
||||
props: {
|
||||
canvasId: {
|
||||
type: String,
|
||||
default: 'ec-canvas'
|
||||
},
|
||||
lazyLoad: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
disableTouch: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
throttleTouch: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
}
|
||||
},
|
||||
// #ifdef H5
|
||||
mounted() {
|
||||
if (!this.lazyLoad) this.init();
|
||||
},
|
||||
// #endif
|
||||
// #ifndef H5
|
||||
onReady() {
|
||||
if (!this.lazyLoad) this.init();
|
||||
},
|
||||
// #endif
|
||||
methods: {
|
||||
setChart(chart){
|
||||
this.chart = chart
|
||||
},
|
||||
init() {
|
||||
const { canvasId } = this;
|
||||
this.ctx = wx.createCanvasContext(canvasId, this);
|
||||
|
||||
this.canvas = new WxCanvas(this.ctx, canvasId);
|
||||
|
||||
const query = wx.createSelectorQuery().in(this);
|
||||
query
|
||||
.select(`#${canvasId}`)
|
||||
.boundingClientRect(res => {
|
||||
if (!res) {
|
||||
setTimeout(() => this.init(), 50);
|
||||
return;
|
||||
}
|
||||
this.$emit('onInit', {
|
||||
width: res.width,
|
||||
height: res.height
|
||||
});
|
||||
})
|
||||
.exec();
|
||||
},
|
||||
canvasToTempFilePath(opt) {
|
||||
const { canvasId } = this;
|
||||
this.ctx.draw(true, () => {
|
||||
wx.canvasToTempFilePath({
|
||||
canvasId,
|
||||
...opt
|
||||
});
|
||||
});
|
||||
},
|
||||
touchStart(e) {
|
||||
const { disableTouch, chart } = this;
|
||||
if (disableTouch || !chart || !e.mp.touches.length) return;
|
||||
const touch = e.mp.touches[0];
|
||||
chart._zr.handler.dispatch('mousedown', {
|
||||
zrX: touch.x,
|
||||
zrY: touch.y
|
||||
});
|
||||
chart._zr.handler.dispatch('mousemove', {
|
||||
zrX: touch.x,
|
||||
zrY: touch.y
|
||||
});
|
||||
},
|
||||
touchMove(e) {
|
||||
const { disableTouch, throttleTouch, chart, lastMoveTime } = this;
|
||||
if (disableTouch || !chart || !e.mp.touches.length) return;
|
||||
|
||||
if (throttleTouch) {
|
||||
const currMoveTime = Date.now();
|
||||
if (currMoveTime - lastMoveTime < 240) return;
|
||||
this.lastMoveTime = currMoveTime;
|
||||
}
|
||||
|
||||
const touch = e.mp.touches[0];
|
||||
chart._zr.handler.dispatch('mousemove', {
|
||||
zrX: touch.x,
|
||||
zrY: touch.y
|
||||
});
|
||||
},
|
||||
touchEnd(e) {
|
||||
const { disableTouch, chart } = this;
|
||||
if (disableTouch || !chart) return;
|
||||
const touch = e.mp.changedTouches ? e.mp.changedTouches[0] : {};
|
||||
chart._zr.handler.dispatch('mouseup', {
|
||||
zrX: touch.x,
|
||||
zrY: touch.y
|
||||
});
|
||||
chart._zr.handler.dispatch('click', {
|
||||
zrX: touch.x,
|
||||
zrY: touch.y
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.ec-canvas {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
flex: 1;
|
||||
}
|
||||
</style>
|
||||
73
components/mpvue-echarts/src/wx-canvas.js
Normal file
73
components/mpvue-echarts/src/wx-canvas.js
Normal file
@@ -0,0 +1,73 @@
|
||||
export default class WxCanvas {
|
||||
constructor(ctx, canvasId) {
|
||||
this.ctx = ctx;
|
||||
this.canvasId = canvasId;
|
||||
this.chart = null;
|
||||
|
||||
WxCanvas.initStyle(ctx);
|
||||
this.initEvent();
|
||||
}
|
||||
|
||||
getContext(contextType) {
|
||||
return contextType === '2d' ? this.ctx : null;
|
||||
}
|
||||
|
||||
setChart(chart) {
|
||||
this.chart = chart;
|
||||
}
|
||||
|
||||
attachEvent() {
|
||||
// noop
|
||||
}
|
||||
|
||||
detachEvent() {
|
||||
// noop
|
||||
}
|
||||
|
||||
static initStyle(ctx) {
|
||||
const styles = ['fillStyle', 'strokeStyle', 'globalAlpha',
|
||||
'textAlign', 'textBaseAlign', 'shadow', 'lineWidth',
|
||||
'lineCap', 'lineJoin', 'lineDash', 'miterLimit', 'fontSize'];
|
||||
|
||||
styles.forEach((style) => {
|
||||
Object.defineProperty(ctx, style, {
|
||||
set: (value) => {
|
||||
if ((style !== 'fillStyle' && style !== 'strokeStyle')
|
||||
|| (value !== 'none' && value !== null)
|
||||
) {
|
||||
ctx[`set${style.charAt(0).toUpperCase()}${style.slice(1)}`](value);
|
||||
}
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
ctx.createRadialGradient = () => ctx.createCircularGradient(arguments);
|
||||
}
|
||||
|
||||
initEvent() {
|
||||
this.event = {};
|
||||
const eventNames = [{
|
||||
wxName: 'touchStart',
|
||||
ecName: 'mousedown',
|
||||
}, {
|
||||
wxName: 'touchMove',
|
||||
ecName: 'mousemove',
|
||||
}, {
|
||||
wxName: 'touchEnd',
|
||||
ecName: 'mouseup',
|
||||
}, {
|
||||
wxName: 'touchEnd',
|
||||
ecName: 'click',
|
||||
}];
|
||||
|
||||
eventNames.forEach((name) => {
|
||||
this.event[name.wxName] = (e) => {
|
||||
const touch = e.mp.touches[0];
|
||||
this.chart._zr.handler.dispatch(name.ecName, {
|
||||
zrX: name.wxName === 'tap' ? touch.clientX : touch.x,
|
||||
zrY: name.wxName === 'tap' ? touch.clientY : touch.y,
|
||||
});
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user