¶Ô±ÈÐÂÎļþ |
| | |
| | | <!-- |
| | | * qiun-data-charts ç§äºé«æ§è½è·¨å
¨ç«¯å¾è¡¨ç»ä»¶ |
| | | * Copyright (c) 2021 QIUN® ç§äº https://www.ucharts.cn All rights reserved. |
| | | * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
| | | * å¤å¶ä½¿ç¨è¯·ä¿çæ¬æ®µæ³¨éï¼æè°¢æ¯æå¼æºï¼ |
| | | * 为æ¹ä¾¿æ´å¤å¼åè
使ç¨ï¼å¦ææ´å¥½ç建议请æäº¤ç äº Pull Requests ï¼ |
| | | * |
| | | * uChartsÂ®å®æ¹ç½ç« |
| | | * https://www.uCharts.cn |
| | | * |
| | | * 弿ºå°å: |
| | | * https://gitee.com/uCharts/uCharts |
| | | * |
| | | * uni-appæä»¶å¸åºå°åï¼ |
| | | * http://ext.dcloud.net.cn/plugin?id=271 |
| | | * |
| | | --> |
| | | <template> |
| | | <view class="chartsview" :id="'ChartBoxId'+cid"> |
| | | <view v-if="mixinDatacomLoading"> |
| | | <!-- èªå®ä¹å è½½ç¶æï¼è¯·æ¹è¿é --> |
| | | <qiun-loading :loadingType="loadingType" /> |
| | | </view> |
| | | <view v-if="mixinDatacomErrorMessage && errorShow" @tap="reloading"> |
| | | <!-- èªå®ä¹é误æç¤ºï¼è¯·æ¹è¿é --> |
| | | <qiun-error :errorMessage="errorMessage" /> |
| | | </view> |
| | | <!-- APPåH5éç¨renderjs渲æå¾è¡¨ --> |
| | | <!-- #ifdef APP-VUE || H5 --> |
| | | <block v-if="echarts"> |
| | | <view |
| | | :style="{ background: background }" |
| | | style="width: 100%;height: 100%;" |
| | | :data-directory="directory" |
| | | :id="'EC'+cid" |
| | | :prop="echartsOpts" |
| | | :change:prop="rdcharts.ecinit" |
| | | :resize="echartsResize" |
| | | :change:resize="rdcharts.ecresize" |
| | | v-show="showchart" |
| | | /> |
| | | </block> |
| | | <block v-else> |
| | | <view |
| | | v-on:tap="rdcharts.tap" |
| | | v-on:mousemove="rdcharts.mouseMove" |
| | | v-on:mousedown="rdcharts.mouseDown" |
| | | v-on:mouseup="rdcharts.mouseUp" |
| | | v-on:touchstart="rdcharts.touchStart" |
| | | v-on:touchmove="rdcharts.touchMove" |
| | | v-on:touchend="rdcharts.touchEnd" |
| | | :id="'UC'+cid" |
| | | :prop="uchartsOpts" |
| | | :change:prop="rdcharts.ucinit" |
| | | > |
| | | <canvas |
| | | :id="cid" |
| | | :canvasId="cid" |
| | | :style="{ width: cWidth + 'px', height: cHeight + 'px', background: background }" |
| | | :disable-scroll="disableScroll" |
| | | @error="_error" |
| | | v-show="showchart" |
| | | /> |
| | | </view> |
| | | </block> |
| | | <!-- #endif --> |
| | | <!-- æ¯ä»å®å°ç¨åº --> |
| | | <!-- #ifdef MP-ALIPAY --> |
| | | <block v-if="ontouch"> |
| | | <canvas |
| | | :id="cid" |
| | | :canvasId="cid" |
| | | :width="cWidth * pixel" |
| | | :height="cHeight * pixel" |
| | | :style="{ width: cWidth + 'px', height: cHeight + 'px', background: background }" |
| | | :disable-scroll="disScroll" |
| | | @tap="_tap" |
| | | @touchstart="_touchStart" |
| | | @touchmove="_touchMove" |
| | | @touchend="_touchEnd" |
| | | @error="_error" |
| | | v-show="showchart" |
| | | /> |
| | | </block> |
| | | <block v-if="!ontouch"> |
| | | <canvas |
| | | :id="cid" |
| | | :canvasId="cid" |
| | | :width="cWidth * pixel" |
| | | :height="cHeight * pixel" |
| | | :style="{ width: cWidth + 'px', height: cHeight + 'px', background: background }" |
| | | :disable-scroll="disScroll" |
| | | @tap="_tap" |
| | | @error="_error" |
| | | v-show="showchart" |
| | | /> |
| | | </block> |
| | | <!-- #endif --> |
| | | <!-- å
¶ä»å°ç¨åºéè¿vue渲æå¾è¡¨ --> |
| | | <!-- #ifdef MP-WEIXIN || MP-BAIDU || MP-QQ || MP-TOUTIAO || MP-KUAISHOU || MP-LARK || MP-JD || MP-360 --> |
| | | <block v-if="type2d"> |
| | | <view v-if="ontouch" @tap="_tap"> |
| | | <canvas |
| | | :id="cid" |
| | | :canvasId="cid" |
| | | :style="{ width: cWidth + 'px', height: cHeight + 'px', background: background }" |
| | | type="2d" |
| | | :disable-scroll="disScroll" |
| | | @touchstart="_touchStart" |
| | | @touchmove="_touchMove" |
| | | @touchend="_touchEnd" |
| | | @error="_error" |
| | | v-show="showchart" |
| | | /> |
| | | </view> |
| | | <view v-if="!ontouch" @tap="_tap"> |
| | | <canvas |
| | | :id="cid" |
| | | :canvasId="cid" |
| | | :style="{ width: cWidth + 'px', height: cHeight + 'px', background: background }" |
| | | type="2d" |
| | | :disable-scroll="disScroll" |
| | | @error="_error" |
| | | v-show="showchart" |
| | | /> |
| | | </view> |
| | | </block> |
| | | <block v-if="!type2d"> |
| | | <view v-if="ontouch" @tap="_tap"> |
| | | <canvas |
| | | :id="cid" |
| | | :canvasId="cid" |
| | | :style="{ width: cWidth + 'px', height: cHeight + 'px', background: background }" |
| | | @touchstart="_touchStart" |
| | | @touchmove="_touchMove" |
| | | @touchend="_touchEnd" |
| | | :disable-scroll="disScroll" |
| | | @error="_error" |
| | | v-if="showchart" |
| | | /> |
| | | </view> |
| | | <view v-if="!ontouch" > |
| | | <canvas |
| | | :id="cid" |
| | | :canvasId="cid" |
| | | :style="{ width: cWidth + 'px', height: cHeight + 'px', background: background }" |
| | | :disable-scroll="disScroll" |
| | | @tap="_tap" |
| | | @error="_error" |
| | | v-if="showchart" |
| | | /> |
| | | </view> |
| | | </block> |
| | | <!-- #endif --> |
| | | </view> |
| | | </template> |
| | | |
| | | <script> |
| | | import uCharts from '../../js_sdk/u-charts/u-charts.js'; |
| | | import cfu from '../../js_sdk/u-charts/config-ucharts.js'; |
| | | // #ifdef APP-VUE || H5 |
| | | import cfe from '../../js_sdk/u-charts/config-echarts.js'; |
| | | // #endif |
| | | |
| | | function deepCloneAssign(origin = {}, ...args) { |
| | | for (let i in args) { |
| | | for (let key in args[i]) { |
| | | if (args[i].hasOwnProperty(key)) { |
| | | origin[key] = args[i][key] && typeof args[i][key] === 'object' ? deepCloneAssign(Array.isArray(args[i][key]) ? [] : {}, origin[key], args[i][key]) : args[i][key]; |
| | | } |
| | | } |
| | | } |
| | | return origin; |
| | | } |
| | | |
| | | function formatterAssign(args,formatter) { |
| | | for (let key in args) { |
| | | if(args.hasOwnProperty(key) && args[key] !== null && typeof args[key] === 'object'){ |
| | | formatterAssign(args[key],formatter) |
| | | }else if(key === 'format' && typeof args[key] === 'string'){ |
| | | args['formatter'] = formatter[args[key]] ? formatter[args[key]] : undefined; |
| | | } |
| | | } |
| | | return args; |
| | | } |
| | | |
| | | // æ¶é´è½¬æ¢å½æ°ï¼ä¸ºäºå¹é
uniClinetDB读ååºçæ¶é´ä¸categoriesä¸å |
| | | function getFormatDate(date) { |
| | | var seperator = "-"; |
| | | var year = date.getFullYear(); |
| | | var month = date.getMonth() + 1; |
| | | var strDate = date.getDate(); |
| | | if (month >= 1 && month <= 9) { |
| | | month = "0" + month; |
| | | } |
| | | if (strDate >= 0 && strDate <= 9) { |
| | | strDate = "0" + strDate; |
| | | } |
| | | var currentdate = year + seperator + month + seperator + strDate; |
| | | return currentdate; |
| | | } |
| | | |
| | | var lastMoveTime = null; |
| | | /** |
| | | * 鲿 |
| | | * |
| | | * @param { Function } fn è¦æ§è¡çæ¹æ³ |
| | | * @param { Number } wait 鲿å¤å°æ¯«ç§ |
| | | * |
| | | * å¨ vue ä¸ä½¿ç¨ï¼æ³¨æï¼ä¸è½ä½¿ç¨ç®å¤´å½æ°ï¼å¦åthisæåä¸å¯¹ï¼å¹¶ä¸ä¸è½å次å°è£
å¦ï¼ |
| | | * move(){ // é误è°ç¨æ¹å¼ |
| | | * debounce(function () { |
| | | * console.log(this.title); |
| | | * }, 1000)}ï¼; |
| | | * åºè¯¥ç´æ¥ä½¿ç¨ï¼// æ£ç¡®è°ç¨æ¹å¼ |
| | | * move: debounce(function () { |
| | | * console.log(this.title); |
| | | * }, 1000) |
| | | */ |
| | | function debounce(fn, wait) { |
| | | let timer = false; |
| | | return function() { |
| | | clearTimeout(timer); |
| | | timer && clearTimeout(timer); |
| | | timer = setTimeout(() => { |
| | | timer = false; |
| | | fn.apply(this, arguments); // æåæ°ä¼ è¿å» |
| | | }, wait); |
| | | }; |
| | | } |
| | | |
| | | export default { |
| | | name: 'qiun-data-charts', |
| | | mixins: [uniCloud.mixinDatacom], |
| | | props: { |
| | | type: { |
| | | type: String, |
| | | default: null |
| | | }, |
| | | canvasId: { |
| | | type: String, |
| | | default: 'uchartsid' |
| | | }, |
| | | canvas2d: { |
| | | type: Boolean, |
| | | default: false |
| | | }, |
| | | background: { |
| | | type: String, |
| | | default: 'rgba(0,0,0,0)' |
| | | }, |
| | | animation: { |
| | | type: Boolean, |
| | | default: true |
| | | }, |
| | | chartData: { |
| | | type: Object, |
| | | default() { |
| | | return { |
| | | categories: [], |
| | | series: [] |
| | | }; |
| | | } |
| | | }, |
| | | opts: { |
| | | type: Object, |
| | | default() { |
| | | return {}; |
| | | } |
| | | }, |
| | | eopts: { |
| | | type: Object, |
| | | default() { |
| | | return {}; |
| | | } |
| | | }, |
| | | loadingType: { |
| | | type: Number, |
| | | default: 2 |
| | | }, |
| | | errorShow: { |
| | | type: Boolean, |
| | | default: true |
| | | }, |
| | | errorReload: { |
| | | type: Boolean, |
| | | default: true |
| | | }, |
| | | errorMessage: { |
| | | type: String, |
| | | default: null |
| | | }, |
| | | inScrollView: { |
| | | type: Boolean, |
| | | default: false |
| | | }, |
| | | reshow: { |
| | | type: Boolean, |
| | | default: false |
| | | }, |
| | | reload: { |
| | | type: Boolean, |
| | | default: false |
| | | }, |
| | | disableScroll: { |
| | | type: Boolean, |
| | | default: false |
| | | }, |
| | | optsWatch: { |
| | | type: Boolean, |
| | | default: true |
| | | }, |
| | | onzoom: { |
| | | type: Boolean, |
| | | default: false |
| | | }, |
| | | ontap: { |
| | | type: Boolean, |
| | | default: true |
| | | }, |
| | | ontouch: { |
| | | type: Boolean, |
| | | default: false |
| | | }, |
| | | onmouse: { |
| | | type: Boolean, |
| | | default: true |
| | | }, |
| | | onmovetip: { |
| | | type: Boolean, |
| | | default: false |
| | | }, |
| | | echartsH5: { |
| | | type: Boolean, |
| | | default: false |
| | | }, |
| | | echartsApp: { |
| | | type: Boolean, |
| | | default: false |
| | | }, |
| | | tooltipShow: { |
| | | type: Boolean, |
| | | default: true |
| | | }, |
| | | tooltipFormat: { |
| | | type: String, |
| | | default: undefined |
| | | }, |
| | | tooltipCustom: { |
| | | type: Object, |
| | | default: undefined |
| | | }, |
| | | startDate: { |
| | | type: String, |
| | | default: undefined |
| | | }, |
| | | endDate: { |
| | | type: String, |
| | | default: undefined |
| | | }, |
| | | textEnum: { |
| | | type: Array, |
| | | default () { |
| | | return [] |
| | | } |
| | | }, |
| | | groupEnum: { |
| | | type: Array, |
| | | default () { |
| | | return [] |
| | | } |
| | | }, |
| | | pageScrollTop: { |
| | | type: Number, |
| | | default: 0 |
| | | }, |
| | | directory: { |
| | | type: String, |
| | | default: '/' |
| | | }, |
| | | tapLegend: { |
| | | type: Boolean, |
| | | default: true |
| | | }, |
| | | menus: { |
| | | type: Array, |
| | | default () { |
| | | return [] |
| | | } |
| | | } |
| | | }, |
| | | data() { |
| | | return { |
| | | cid: 'uchartsid', |
| | | inWx: false, |
| | | inAli: false, |
| | | inTt: false, |
| | | inBd: false, |
| | | inH5: false, |
| | | inApp: false, |
| | | inWin: false, |
| | | type2d: true, |
| | | disScroll: false, |
| | | openmouse: false, |
| | | pixel: 1, |
| | | cWidth: 375, |
| | | cHeight: 250, |
| | | showchart: false, |
| | | echarts: false, |
| | | echartsResize:{ |
| | | state:false |
| | | }, |
| | | uchartsOpts: {}, |
| | | echartsOpts: {}, |
| | | drawData:{}, |
| | | lastDrawTime:null, |
| | | }; |
| | | }, |
| | | created(){ |
| | | this.cid = this.canvasId |
| | | if (this.canvasId == 'uchartsid' || this.canvasId == '') { |
| | | let t = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' |
| | | let len = t.length |
| | | let id = '' |
| | | for (let i = 0; i < 32; i++) { |
| | | id += t.charAt(Math.floor(Math.random() * len)) |
| | | } |
| | | this.cid = id |
| | | } |
| | | const systemInfo = uni.getSystemInfoSync() |
| | | if(systemInfo.platform === 'windows' || systemInfo.platform === 'mac'){ |
| | | this.inWin = true; |
| | | } |
| | | // #ifdef MP-WEIXIN |
| | | this.inWx = true; |
| | | if (this.canvas2d === false || systemInfo.platform === 'windows' || systemInfo.platform === 'mac') { |
| | | this.type2d = false; |
| | | }else{ |
| | | this.type2d = true; |
| | | this.pixel = systemInfo.pixelRatio; |
| | | } |
| | | // #endif |
| | | //é微信å°ç¨åºç«¯å¼ºå¶å
³écanvas2dæ¨¡å¼ |
| | | // #ifndef MP-WEIXIN |
| | | this.type2d = false; |
| | | // #endif |
| | | // #ifdef MP-TOUTIAO || MP-LARK || MP-ALIPAY |
| | | this.type2d = this.canvas2d; |
| | | // #endif |
| | | // #ifdef MP-ALIPAY |
| | | this.inAli = true; |
| | | this.pixel = systemInfo.pixelRatio; |
| | | // #endif |
| | | // #ifdef MP-BAIDU |
| | | this.inBd = true; |
| | | // #endif |
| | | // #ifdef MP-TOUTIAO |
| | | this.inTt = true; |
| | | // #endif |
| | | this.disScroll = this.disableScroll; |
| | | }, |
| | | mounted() { |
| | | // #ifdef APP-VUE |
| | | this.inApp = true; |
| | | if (this.echartsApp === true) { |
| | | this.echarts = true; |
| | | this.openmouse = false; |
| | | } |
| | | // #endif |
| | | // #ifdef APP-NVUE |
| | | this.inApp = true; |
| | | this.mixinDatacomLoading = false |
| | | this.mixinDatacomErrorMessage = "æä¸æ¯æNVUE" |
| | | // #endif |
| | | // #ifdef H5 |
| | | this.inH5 = true; |
| | | if(this.inWin === true){ |
| | | this.openmouse = this.onmouse; |
| | | } |
| | | if (this.echartsH5 === true) { |
| | | this.echarts = true; |
| | | } |
| | | // #endif |
| | | this.$nextTick(()=>{ |
| | | this.beforeInit(); |
| | | }) |
| | | // #ifndef MP-ALIPAY || MP-BAIDU || MP-TOUTIAO || APP-VUE |
| | | const time = this.inH5 ? 500 : 200; |
| | | const _this = this; |
| | | uni.onWindowResize( |
| | | debounce(function(res) { |
| | | if (_this.mixinDatacomLoading == true) { |
| | | return; |
| | | } |
| | | let errmsg = _this.mixinDatacomErrorMessage; |
| | | if (errmsg !== null && errmsg !== 'null' && errmsg !== '') { |
| | | return; |
| | | } |
| | | if (_this.echarts) { |
| | | _this.echartsResize.state = !_this.echartsResize.state; |
| | | } else { |
| | | _this.resizeHandler(); |
| | | } |
| | | }, time) |
| | | ); |
| | | // #endif |
| | | }, |
| | | destroyed(){ |
| | | if(this.echarts === true){ |
| | | delete cfe.option[this.cid] |
| | | delete cfe.instance[this.cid] |
| | | }else{ |
| | | delete cfu.option[this.cid] |
| | | delete cfu.instance[this.cid] |
| | | } |
| | | // #ifndef MP-ALIPAY || MP-BAIDU || MP-TOUTIAO |
| | | uni.offWindowResize(()=>{}) |
| | | // #endif |
| | | }, |
| | | watch: { |
| | | chartDataProps: { |
| | | handler(val, oldval) { |
| | | if (typeof val === 'object') { |
| | | if (JSON.stringify(val) !== JSON.stringify(oldval)) { |
| | | this._clearChart(); |
| | | if (val.series && val.series.length > 0) { |
| | | this.beforeInit(); |
| | | }else{ |
| | | this.mixinDatacomLoading = true; |
| | | this.showchart = false; |
| | | this.mixinDatacomErrorMessage = null; |
| | | } |
| | | } |
| | | } else { |
| | | this.mixinDatacomLoading = false; |
| | | this._clearChart(); |
| | | this.showchart = false; |
| | | this.mixinDatacomErrorMessage = 'åæ°é误ï¼chartDataæ°æ®ç±»åé误'; |
| | | } |
| | | }, |
| | | immediate: false, |
| | | deep: true |
| | | }, |
| | | localdata:{ |
| | | handler(val, oldval) { |
| | | if (JSON.stringify(val) !== JSON.stringify(oldval)) { |
| | | if (val.length > 0) { |
| | | this.beforeInit(); |
| | | }else{ |
| | | this.mixinDatacomLoading = true; |
| | | this._clearChart(); |
| | | this.showchart = false; |
| | | this.mixinDatacomErrorMessage = null; |
| | | } |
| | | } |
| | | }, |
| | | immediate: false, |
| | | deep: true |
| | | }, |
| | | optsProps: { |
| | | handler(val, oldval) { |
| | | if (typeof val === 'object') { |
| | | if (JSON.stringify(val) !== JSON.stringify(oldval) && this.echarts === false && this.optsWatch == true) { |
| | | this.checkData(this.drawData); |
| | | } |
| | | } else { |
| | | this.mixinDatacomLoading = false; |
| | | this._clearChart(); |
| | | this.showchart = false; |
| | | this.mixinDatacomErrorMessage = 'åæ°é误ï¼optsæ°æ®ç±»åé误'; |
| | | } |
| | | }, |
| | | immediate: false, |
| | | deep: true |
| | | }, |
| | | eoptsProps: { |
| | | handler(val, oldval) { |
| | | if (typeof val === 'object') { |
| | | if (JSON.stringify(val) !== JSON.stringify(oldval) && this.echarts === true) { |
| | | this.checkData(this.drawData); |
| | | } |
| | | } else { |
| | | this.mixinDatacomLoading = false; |
| | | this.showchart = false; |
| | | this.mixinDatacomErrorMessage = 'åæ°é误ï¼eoptsæ°æ®ç±»åé误'; |
| | | } |
| | | }, |
| | | immediate: false, |
| | | deep: true |
| | | }, |
| | | reshow(val, oldval) { |
| | | if (val === true && this.mixinDatacomLoading === false) { |
| | | setTimeout(() => { |
| | | this.mixinDatacomErrorMessage = null; |
| | | this.echartsResize.state = !this.echartsResize.state; |
| | | this.checkData(this.drawData); |
| | | }, 200); |
| | | } |
| | | }, |
| | | reload(val, oldval) { |
| | | if (val === true) { |
| | | this.showchart = false; |
| | | this.mixinDatacomErrorMessage = null; |
| | | this.reloading(); |
| | | } |
| | | }, |
| | | mixinDatacomErrorMessage(val, oldval) { |
| | | if (val) { |
| | | this.emitMsg({name: 'error', params: {type:"error", errorShow: this.errorShow, msg: val, id: this.cid}}); |
| | | if(this.errorShow){ |
| | | console.log('[ç§äºå¾è¡¨ç»ä»¶]' + val); |
| | | } |
| | | } |
| | | }, |
| | | errorMessage(val, oldval) { |
| | | if (val && this.errorShow && val !== null && val !== 'null' && val !== '') { |
| | | this.showchart = false; |
| | | this.mixinDatacomLoading = false; |
| | | this.mixinDatacomErrorMessage = val; |
| | | } else { |
| | | this.showchart = false; |
| | | this.mixinDatacomErrorMessage = null; |
| | | this.reloading(); |
| | | } |
| | | } |
| | | }, |
| | | computed: { |
| | | optsProps() { |
| | | return JSON.parse(JSON.stringify(this.opts)); |
| | | }, |
| | | eoptsProps() { |
| | | return JSON.parse(JSON.stringify(this.eopts)); |
| | | }, |
| | | chartDataProps() { |
| | | return JSON.parse(JSON.stringify(this.chartData)); |
| | | }, |
| | | }, |
| | | methods: { |
| | | beforeInit(){ |
| | | this.mixinDatacomErrorMessage = null; |
| | | if (typeof this.chartData === 'object' && this.chartData != null && this.chartData.series !== undefined && this.chartData.series.length > 0) { |
| | | //æ·è´ä¸ä¸chartDataï¼ä¸ºäºoptsåæ´åç»ä¸æ°æ®æ¥æº |
| | | this.drawData = deepCloneAssign({}, this.chartData); |
| | | this.mixinDatacomLoading = false; |
| | | this.showchart = true; |
| | | this.checkData(this.chartData); |
| | | }else if(this.localdata.length>0){ |
| | | this.mixinDatacomLoading = false; |
| | | this.showchart = true; |
| | | this.localdataInit(this.localdata); |
| | | }else if(this.collection !== ''){ |
| | | this.mixinDatacomLoading = false; |
| | | this.getCloudData(); |
| | | }else{ |
| | | this.mixinDatacomLoading = true; |
| | | } |
| | | }, |
| | | localdataInit(resdata){ |
| | | //æ¿æ¢enumç±»å为æ£ç¡®çæè¿° |
| | | if(this.groupEnum.length>0){ |
| | | for (let i = 0; i < resdata.length; i++) { |
| | | for (let j = 0; j < this.groupEnum.length; j++) { |
| | | if(resdata[i].group === this.groupEnum[j].value){ |
| | | resdata[i].group = this.groupEnum[j].text |
| | | } |
| | | } |
| | | } |
| | | } |
| | | if(this.textEnum.length>0){ |
| | | for (let i = 0; i < resdata.length; i++) { |
| | | for (let j = 0; j < this.textEnum.length; j++) { |
| | | if(resdata[i].text === this.textEnum[j].value){ |
| | | resdata[i].text = this.textEnum[j].text |
| | | } |
| | | } |
| | | } |
| | | } |
| | | let needCategories = false; |
| | | let tmpData = {categories:[], series:[]} |
| | | let tmpcategories = [] |
| | | let tmpseries = []; |
| | | //æ¼æ¥categories |
| | | if(this.echarts === true){ |
| | | needCategories = cfe.categories.includes(this.type) |
| | | }else{ |
| | | needCategories = cfu.categories.includes(this.type) |
| | | } |
| | | if(needCategories === true){ |
| | | //妿propsä¸çchartData带æcategoriesï¼åä¼å
使ç¨chartDataçcategories |
| | | if(this.chartData && this.chartData.categories && this.chartData.categories.length>0){ |
| | | tmpcategories = this.chartData.categories |
| | | }else{ |
| | | //å¦ææ¯æ¥æç±»åçæ°æ®ï¼ä¸ç®¡æ¯æ¬å°æ°æ®è¿æ¯äºæ°æ®ï¼é½æèµ·æ¢æ¥æèªå¨æ¼æ¥categories |
| | | if(this.startDate && this.endDate){ |
| | | let idate = new Date(this.startDate) |
| | | let edate = new Date(this.endDate) |
| | | while (idate <= edate) { |
| | | tmpcategories.push(getFormatDate(idate)) |
| | | idate = idate.setDate(idate.getDate() + 1) |
| | | idate = new Date(idate) |
| | | } |
| | | //å¦åä»ç»æä¸å»éå¹¶æ¼æ¥categories |
| | | }else{ |
| | | let tempckey = {}; |
| | | resdata.map(function(item, index) { |
| | | if (item.text != undefined && !tempckey[item.text]) { |
| | | tmpcategories.push(item.text) |
| | | tempckey[item.text] = true |
| | | } |
| | | }); |
| | | } |
| | | } |
| | | tmpData.categories = tmpcategories |
| | | } |
| | | //æ¼æ¥series |
| | | let tempskey = {}; |
| | | resdata.map(function(item, index) { |
| | | if (item.group != undefined && !tempskey[item.group]) { |
| | | tmpseries.push({ name: item.group, data: [] }); |
| | | tempskey[item.group] = true; |
| | | } |
| | | }); |
| | | //å¦ææ²¡æè·åå°åç»åç§°(å¯è½æ¯å¸¦categoriesçæ°æ®ï¼ä¹å¯è½æ¯ä¸å¸¦ç饼å¾ç±») |
| | | if (tmpseries.length == 0) { |
| | | tmpseries = [{ name: 'é»è®¤åç»', data: [] }]; |
| | | //妿æ¯éè¦categoriesçå¾è¡¨ç±»å |
| | | if(needCategories === true){ |
| | | for (let j = 0; j < tmpcategories.length; j++) { |
| | | let seriesdata = 0; |
| | | for (let i = 0; i < resdata.length; i++) { |
| | | if (resdata[i].text == tmpcategories[j]) { |
| | | seriesdata = resdata[i].value; |
| | | } |
| | | } |
| | | tmpseries[0].data.push(seriesdata); |
| | | } |
| | | //妿æ¯é¥¼å¾ç±»çå¾è¡¨ç±»å |
| | | }else{ |
| | | for (let i = 0; i < resdata.length; i++) { |
| | | tmpseries[0].data.push({"name": resdata[i].text,"value": resdata[i].value}); |
| | | } |
| | | } |
| | | //妿æåç»å |
| | | } else { |
| | | for (let k = 0; k < tmpseries.length; k++) { |
| | | //妿æcategories |
| | | if (tmpcategories.length > 0) { |
| | | for (let j = 0; j < tmpcategories.length; j++) { |
| | | let seriesdata = 0; |
| | | for (let i = 0; i < resdata.length; i++) { |
| | | if (tmpseries[k].name == resdata[i].group && resdata[i].text == tmpcategories[j]) { |
| | | seriesdata = resdata[i].value; |
| | | } |
| | | } |
| | | tmpseries[k].data.push(seriesdata); |
| | | } |
| | | //å¦æä¼ äºgroupèæ²¡æä¼ textï¼å³æ²¡æcategoriesï¼æ£å¸¸æ
åµä¸è¿ç§æ°æ®æ¯ä¸ç¬¦åæ°æ®è¦æ±è§èçï¼ |
| | | } else { |
| | | for (let i = 0; i < resdata.length; i++) { |
| | | if (tmpseries[k].name == resdata[i].group) { |
| | | tmpseries[k].data.push(resdata[i].value); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | tmpData.series = tmpseries |
| | | //æ·è´ä¸ä¸chartDataï¼ä¸ºäºoptsåæ´åç»ä¸æ°æ®æ¥æº |
| | | this.drawData = deepCloneAssign({}, tmpData); |
| | | this.checkData(tmpData) |
| | | }, |
| | | reloading() { |
| | | if(this.errorReload === false){ |
| | | return; |
| | | } |
| | | this.showchart = false; |
| | | this.mixinDatacomErrorMessage = null; |
| | | if (this.collection !== '') { |
| | | this.mixinDatacomLoading = false; |
| | | this.onMixinDatacomPropsChange(true); |
| | | } else { |
| | | this.beforeInit(); |
| | | } |
| | | }, |
| | | checkData(anyData) { |
| | | let cid = this.cid |
| | | //å¤ä½optsæeopts |
| | | if(this.echarts === true){ |
| | | cfe.option[cid] = deepCloneAssign({}, this.eopts); |
| | | cfe.option[cid].id = cid; |
| | | cfe.option[cid].type = this.type; |
| | | }else{ |
| | | if (this.type && cfu.type.includes(this.type)) { |
| | | cfu.option[cid] = deepCloneAssign({}, cfu[this.type], this.opts); |
| | | cfu.option[cid].canvasId = cid; |
| | | } else { |
| | | this.mixinDatacomLoading = false; |
| | | this.showchart = false; |
| | | this.mixinDatacomErrorMessage = 'åæ°é误ï¼propsåæ°ä¸typeç±»å䏿£ç¡®'; |
| | | } |
| | | } |
| | | //æè½½categoriesåseries |
| | | let newData = deepCloneAssign({}, anyData); |
| | | if (newData.series !== undefined && newData.series.length > 0) { |
| | | this.mixinDatacomErrorMessage = null; |
| | | if (this.echarts === true) { |
| | | cfe.option[cid].chartData = newData; |
| | | this.$nextTick(()=>{ |
| | | this.init() |
| | | }) |
| | | }else{ |
| | | cfu.option[cid].categories = newData.categories; |
| | | cfu.option[cid].series = newData.series; |
| | | this.$nextTick(()=>{ |
| | | this.init() |
| | | }) |
| | | } |
| | | } |
| | | }, |
| | | resizeHandler() { |
| | | //渲æé²æ |
| | | let currTime = Date.now(); |
| | | let lastDrawTime = this.lastDrawTime?this.lastDrawTime:currTime-3000; |
| | | let duration = currTime - lastDrawTime; |
| | | if (duration < 1000) return; |
| | | let chartdom = uni |
| | | .createSelectorQuery() |
| | | // #ifndef MP-ALIPAY |
| | | .in(this) |
| | | // #endif |
| | | .select('#ChartBoxId'+this.cid) |
| | | .boundingClientRect(data => { |
| | | this.showchart = true; |
| | | if (data.width > 0 && data.height > 0) { |
| | | if (data.width !== this.cWidth || data.height !== this.cHeight) { |
| | | this.checkData(this.drawData) |
| | | } |
| | | } |
| | | }) |
| | | .exec(); |
| | | }, |
| | | getCloudData() { |
| | | if (this.mixinDatacomLoading == true) { |
| | | return; |
| | | } |
| | | this.mixinDatacomLoading = true; |
| | | this.mixinDatacomGet() |
| | | .then(res => { |
| | | this.mixinDatacomResData = res.result.data; |
| | | this.localdataInit(this.mixinDatacomResData); |
| | | }) |
| | | .catch(err => { |
| | | this.mixinDatacomLoading = false; |
| | | this.showchart = false; |
| | | this.mixinDatacomErrorMessage = '请æ±é误ï¼' + err; |
| | | }); |
| | | }, |
| | | onMixinDatacomPropsChange(needReset, changed) { |
| | | if (needReset == true && this.collection !== '') { |
| | | this.showchart = false; |
| | | this.mixinDatacomErrorMessage = null; |
| | | this._clearChart(); |
| | | this.getCloudData(); |
| | | } |
| | | }, |
| | | _clearChart() { |
| | | let cid = this.cid |
| | | if (this.echarts !== true && cfu.option[cid] && cfu.option[cid].context) { |
| | | const ctx = cfu.option[cid].context; |
| | | if(typeof ctx === "object" && !!!cfu.option[cid].update){ |
| | | ctx.clearRect(0, 0, this.cWidth*this.pixel, this.cHeight*this.pixel); |
| | | ctx.draw(); |
| | | } |
| | | } |
| | | }, |
| | | init() { |
| | | let cid = this.cid |
| | | let chartdom = uni |
| | | .createSelectorQuery() |
| | | // #ifndef MP-ALIPAY |
| | | .in(this) |
| | | // #endif |
| | | .select('#ChartBoxId'+cid) |
| | | .boundingClientRect(data => { |
| | | if (data.width > 0 && data.height > 0) { |
| | | this.mixinDatacomLoading = false; |
| | | this.showchart = true; |
| | | this.lastDrawTime = Date.now(); |
| | | this.cWidth = data.width; |
| | | this.cHeight = data.height; |
| | | if(this.echarts !== true){ |
| | | cfu.option[cid].background = this.background == 'rgba(0,0,0,0)' ? '#FFFFFF' : this.background; |
| | | cfu.option[cid].canvas2d = this.type2d; |
| | | cfu.option[cid].pixelRatio = this.pixel; |
| | | cfu.option[cid].animation = this.animation; |
| | | cfu.option[cid].width = data.width * this.pixel; |
| | | cfu.option[cid].height = data.height * this.pixel; |
| | | cfu.option[cid].onzoom = this.onzoom; |
| | | cfu.option[cid].ontap = this.ontap; |
| | | cfu.option[cid].ontouch = this.ontouch; |
| | | cfu.option[cid].onmouse = this.openmouse; |
| | | cfu.option[cid].onmovetip = this.onmovetip; |
| | | cfu.option[cid].tooltipShow = this.tooltipShow; |
| | | cfu.option[cid].tooltipFormat = this.tooltipFormat; |
| | | cfu.option[cid].tooltipCustom = this.tooltipCustom; |
| | | cfu.option[cid].inScrollView = this.inScrollView; |
| | | cfu.option[cid].lastDrawTime = this.lastDrawTime; |
| | | cfu.option[cid].tapLegend = this.tapLegend; |
| | | } |
| | | //妿æ¯H5æè
App端ï¼éç¨renderjs渲æå¾è¡¨ |
| | | if (this.inH5 || this.inApp) { |
| | | if (this.echarts == true) { |
| | | cfe.option[cid].ontap = this.ontap; |
| | | cfe.option[cid].onmouse = this.openmouse; |
| | | cfe.option[cid].tooltipShow = this.tooltipShow; |
| | | cfe.option[cid].tooltipFormat = this.tooltipFormat; |
| | | cfe.option[cid].tooltipCustom = this.tooltipCustom; |
| | | cfe.option[cid].lastDrawTime = this.lastDrawTime; |
| | | this.echartsOpts = deepCloneAssign({}, cfe.option[cid]); |
| | | } else { |
| | | cfu.option[cid].rotateLock = cfu.option[cid].rotate; |
| | | this.uchartsOpts = deepCloneAssign({}, cfu.option[cid]); |
| | | } |
| | | //妿æ¯å°ç¨åºç«¯ï¼éç¨uCharts渲æ |
| | | } else { |
| | | cfu.option[cid] = formatterAssign(cfu.option[cid],cfu.formatter) |
| | | this.mixinDatacomErrorMessage = null; |
| | | this.mixinDatacomLoading = false; |
| | | this.showchart = true; |
| | | this.$nextTick(()=>{ |
| | | if (this.type2d === true) { |
| | | const query = uni.createSelectorQuery().in(this) |
| | | query |
| | | .select('#' + cid) |
| | | .fields({ node: true, size: true }) |
| | | .exec(res => { |
| | | if (res[0]) { |
| | | const canvas = res[0].node; |
| | | const ctx = canvas.getContext('2d'); |
| | | cfu.option[cid].context = ctx; |
| | | cfu.option[cid].rotateLock = cfu.option[cid].rotate; |
| | | if(cfu.instance[cid] && cfu.option[cid] && cfu.option[cid].update === true){ |
| | | this._updataUChart(cid) |
| | | }else{ |
| | | canvas.width = data.width * this.pixel; |
| | | canvas.height = data.height * this.pixel; |
| | | canvas._width = data.width * this.pixel; |
| | | canvas._height = data.height * this.pixel; |
| | | setTimeout(()=>{ |
| | | cfu.option[cid].context.restore(); |
| | | cfu.option[cid].context.save(); |
| | | this._newChart(cid) |
| | | },100) |
| | | } |
| | | } else { |
| | | this.showchart = false; |
| | | this.mixinDatacomErrorMessage = 'åæ°é误ï¼å¼å¯2d模å¼åï¼æªè·åå°domèç¹ï¼canvas-id:' + cid; |
| | | } |
| | | }); |
| | | } else { |
| | | if(this.inAli){ |
| | | cfu.option[cid].rotateLock = cfu.option[cid].rotate; |
| | | } |
| | | cfu.option[cid].context = uni.createCanvasContext(cid, this); |
| | | if(cfu.instance[cid] && cfu.option[cid] && cfu.option[cid].update === true){ |
| | | this._updataUChart(cid) |
| | | }else{ |
| | | setTimeout(()=>{ |
| | | cfu.option[cid].context.restore(); |
| | | cfu.option[cid].context.save(); |
| | | this._newChart(cid) |
| | | },100) |
| | | } |
| | | } |
| | | }) |
| | | } |
| | | } else { |
| | | this.mixinDatacomLoading = false; |
| | | this.showchart = false; |
| | | if (this.reshow == true) { |
| | | this.mixinDatacomErrorMessage = 'å¸å±éè¯¯ï¼æªè·åå°ç¶å
ç´ å®½é«å°ºå¯¸ï¼canvas-id:' + cid; |
| | | } |
| | | } |
| | | }) |
| | | .exec(); |
| | | }, |
| | | saveImage(){ |
| | | uni.canvasToTempFilePath({ |
| | | canvasId: this.cid, |
| | | success: res=>{ |
| | | //#ifdef H5 |
| | | var a = document.createElement("a"); |
| | | a.href = res.tempFilePath; |
| | | a.download = this.cid; |
| | | a.target = '_blank' |
| | | a.click(); |
| | | //#endif |
| | | //#ifndef H5 |
| | | uni.saveImageToPhotosAlbum({ |
| | | filePath: res.tempFilePath, |
| | | success: function () { |
| | | uni.showToast({ |
| | | title: 'ä¿åæå', |
| | | duration: 2000 |
| | | }); |
| | | } |
| | | }); |
| | | //#endif |
| | | } |
| | | },this); |
| | | }, |
| | | getImage(){ |
| | | if(this.type2d == false){ |
| | | uni.canvasToTempFilePath({ |
| | | canvasId: this.cid, |
| | | success: res=>{ |
| | | this.emitMsg({name: 'getImage', params: {type:"getImage", base64: res.tempFilePath}}); |
| | | } |
| | | },this); |
| | | }else{ |
| | | const query = uni.createSelectorQuery().in(this) |
| | | query |
| | | .select('#' + this.cid) |
| | | .fields({ node: true, size: true }) |
| | | .exec(res => { |
| | | if (res[0]) { |
| | | const canvas = res[0].node; |
| | | this.emitMsg({name: 'getImage', params: {type:"getImage", base64: canvas.toDataURL('image/png')}}); |
| | | } |
| | | }); |
| | | } |
| | | }, |
| | | // #ifndef APP-VUE || H5 |
| | | _newChart(cid) { |
| | | if (this.mixinDatacomLoading == true) { |
| | | return; |
| | | } |
| | | this.showchart = true; |
| | | cfu.instance[cid] = new uCharts(cfu.option[cid]); |
| | | cfu.instance[cid].addEventListener('renderComplete', () => { |
| | | this.emitMsg({name: 'complete', params: {type:"complete", complete: true, id: cid, opts: cfu.instance[cid].opts}}); |
| | | cfu.instance[cid].delEventListener('renderComplete') |
| | | }); |
| | | cfu.instance[cid].addEventListener('scrollLeft', () => { |
| | | this.emitMsg({name: 'scrollLeft', params: {type:"scrollLeft", scrollLeft: true, id: cid, opts: cfu.instance[cid].opts}}); |
| | | }); |
| | | cfu.instance[cid].addEventListener('scrollRight', () => { |
| | | this.emitMsg({name: 'scrollRight', params: {type:"scrollRight", scrollRight: true, id: cid, opts: cfu.instance[cid].opts}}); |
| | | }); |
| | | }, |
| | | _updataUChart(cid) { |
| | | cfu.instance[cid].updateData(cfu.option[cid]) |
| | | }, |
| | | _tooltipDefault(item, category, index, opts) { |
| | | if (category) { |
| | | let data = item.data |
| | | if(typeof item.data === "object"){ |
| | | data = item.data.value |
| | | } |
| | | return category + ' ' + item.name + ':' + data; |
| | | } else { |
| | | if (item.properties && item.properties.name) { |
| | | return item.properties.name; |
| | | } else { |
| | | return item.name + ':' + item.data; |
| | | } |
| | | } |
| | | }, |
| | | _showTooltip(e) { |
| | | let cid = this.cid |
| | | let tc = cfu.option[cid].tooltipCustom |
| | | if (tc && tc !== undefined && tc !== null) { |
| | | let offset = undefined; |
| | | if (tc.x >= 0 && tc.y >= 0) { |
| | | offset = { x: tc.x, y: tc.y + 10 }; |
| | | } |
| | | cfu.instance[cid].showToolTip(e, { |
| | | index: tc.index, |
| | | offset: offset, |
| | | textList: tc.textList, |
| | | formatter: (item, category, index, opts) => { |
| | | if (typeof cfu.option[cid].tooltipFormat === 'string' && cfu.formatter[cfu.option[cid].tooltipFormat]) { |
| | | return cfu.formatter[cfu.option[cid].tooltipFormat](item, category, index, opts); |
| | | } else { |
| | | return this._tooltipDefault(item, category, index, opts); |
| | | } |
| | | } |
| | | }); |
| | | } else { |
| | | cfu.instance[cid].showToolTip(e, { |
| | | formatter: (item, category, index, opts) => { |
| | | if (typeof cfu.option[cid].tooltipFormat === 'string' && cfu.formatter[cfu.option[cid].tooltipFormat]) { |
| | | return cfu.formatter[cfu.option[cid].tooltipFormat](item, category, index, opts); |
| | | } else { |
| | | return this._tooltipDefault(item, category, index, opts); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | }, |
| | | _tap(e,move) { |
| | | let cid = this.cid |
| | | let currentIndex = null; |
| | | let legendIndex = null; |
| | | if (this.inScrollView === true || this.inAli) { |
| | | let chartdom = uni |
| | | .createSelectorQuery() |
| | | // #ifndef MP-ALIPAY |
| | | .in(this) |
| | | .select('#ChartBoxId'+cid) |
| | | // #endif |
| | | // #ifdef MP-ALIPAY |
| | | .select('#'+this.cid) |
| | | // #endif |
| | | .boundingClientRect(data => { |
| | | e.changedTouches=[]; |
| | | if (this.inAli) { |
| | | e.changedTouches.unshift({ x: e.detail.clientX - data.left, y: e.detail.clientY - data.top}); |
| | | }else{ |
| | | e.changedTouches.unshift({ x: e.detail.x - data.left, y: e.detail.y - data.top - this.pageScrollTop}); |
| | | } |
| | | if(move){ |
| | | if (this.tooltipShow === true) { |
| | | this._showTooltip(e); |
| | | } |
| | | }else{ |
| | | currentIndex = cfu.instance[cid].getCurrentDataIndex(e); |
| | | legendIndex = cfu.instance[cid].getLegendDataIndex(e); |
| | | if(this.tapLegend === true){ |
| | | cfu.instance[cid].touchLegend(e); |
| | | } |
| | | if (this.tooltipShow === true) { |
| | | this._showTooltip(e); |
| | | } |
| | | this.emitMsg({name: 'getIndex', params: { type:"getIndex", event:{ x: e.detail.x - data.left, y: e.detail.y - data.top }, currentIndex: currentIndex, legendIndex: legendIndex, id: cid, opts: cfu.instance[cid].opts}}); |
| | | } |
| | | }) |
| | | .exec(); |
| | | } else { |
| | | if(move){ |
| | | if (this.tooltipShow === true) { |
| | | this._showTooltip(e); |
| | | } |
| | | }else{ |
| | | e.changedTouches=[]; |
| | | e.changedTouches.unshift({ x: e.detail.x - e.currentTarget.offsetLeft, y: e.detail.y - e.currentTarget.offsetTop }); |
| | | currentIndex = cfu.instance[cid].getCurrentDataIndex(e); |
| | | legendIndex = cfu.instance[cid].getLegendDataIndex(e); |
| | | if(this.tapLegend === true){ |
| | | cfu.instance[cid].touchLegend(e); |
| | | } |
| | | if (this.tooltipShow === true) { |
| | | this._showTooltip(e); |
| | | } |
| | | this.emitMsg({name: 'getIndex', params: {type:"getIndex", event:{ x: e.detail.x, y: e.detail.y - e.currentTarget.offsetTop }, currentIndex: currentIndex, legendIndex: legendIndex, id: cid, opts: cfu.instance[cid].opts}}); |
| | | } |
| | | } |
| | | }, |
| | | _touchStart(e) { |
| | | let cid = this.cid |
| | | lastMoveTime=Date.now(); |
| | | if(cfu.option[cid].enableScroll === true && e.touches.length == 1){ |
| | | cfu.instance[cid].scrollStart(e); |
| | | } |
| | | this.emitMsg({name:'getTouchStart', params:{type:"touchStart", event:e.changedTouches[0], id:cid, opts: cfu.instance[cid].opts}}); |
| | | }, |
| | | _touchMove(e) { |
| | | let cid = this.cid |
| | | let currMoveTime = Date.now(); |
| | | let duration = currMoveTime - lastMoveTime; |
| | | let touchMoveLimit = cfu.option[cid].touchMoveLimit || 24; |
| | | if (duration < Math.floor(1000 / touchMoveLimit)) return;//æ¯ç§60帧 |
| | | lastMoveTime = currMoveTime; |
| | | if(cfu.option[cid].enableScroll === true && e.changedTouches.length == 1){ |
| | | cfu.instance[cid].scroll(e); |
| | | } |
| | | if(this.ontap === true && cfu.option[cid].enableScroll === false && this.onmovetip === true){ |
| | | this._tap(e,true) |
| | | } |
| | | if(this.ontouch === true && cfu.option[cid].enableScroll === true && this.onzoom === true && e.changedTouches.length == 2){ |
| | | cfu.instance[cid].dobuleZoom(e); |
| | | } |
| | | this.emitMsg({name: 'getTouchMove', params: {type:"touchMove", event:e.changedTouches[0], id: cid, opts: cfu.instance[cid].opts}}); |
| | | }, |
| | | _touchEnd(e) { |
| | | let cid = this.cid |
| | | if(cfu.option[cid].enableScroll === true && e.touches.length == 0){ |
| | | cfu.instance[cid].scrollEnd(e); |
| | | } |
| | | this.emitMsg({name:'getTouchEnd', params:{type:"touchEnd", event:e.changedTouches[0], id:cid, opts: cfu.instance[cid].opts}}); |
| | | if(this.ontap === true && cfu.option[cid].enableScroll === false && this.onmovetip === true){ |
| | | this._tap(e,true) |
| | | } |
| | | }, |
| | | // #endif |
| | | _error(e) { |
| | | this.mixinDatacomErrorMessage = e.detail.errMsg; |
| | | }, |
| | | emitMsg(msg) { |
| | | this.$emit(msg.name, msg.params); |
| | | }, |
| | | getRenderType() { |
| | | //鲿¢å¦æå¼å¯echartsä¸ç¶å
ç´ ä¸ºv-ifçæ
åµrenderjsçå¬ä¸å°propååçé®é¢ |
| | | if(this.echarts===true && this.mixinDatacomLoading===false){ |
| | | this.beforeInit() |
| | | } |
| | | }, |
| | | toJSON(){ |
| | | return this |
| | | } |
| | | } |
| | | }; |
| | | </script> |
| | | |
| | | <!-- #ifdef APP-VUE || H5 --> |
| | | <script module="rdcharts" lang="renderjs"> |
| | | import uChartsRD from '../../js_sdk/u-charts/u-charts.js'; |
| | | import cfu from '../../js_sdk/u-charts/config-ucharts.js'; |
| | | import cfe from '../../js_sdk/u-charts/config-echarts.js'; |
| | | |
| | | var that = {}; |
| | | var rootdom = null; |
| | | |
| | | function rddeepCloneAssign(origin = {}, ...args) { |
| | | for (let i in args) { |
| | | for (let key in args[i]) { |
| | | if (args[i].hasOwnProperty(key)) { |
| | | origin[key] = args[i][key] && typeof args[i][key] === 'object' ? rddeepCloneAssign(Array.isArray(args[i][key]) ? [] : {}, origin[key], args[i][key]) : args[i][key]; |
| | | } |
| | | } |
| | | } |
| | | return origin; |
| | | } |
| | | |
| | | function rdformatterAssign(args,formatter) { |
| | | for (let key in args) { |
| | | if(args.hasOwnProperty(key) && args[key] !== null && typeof args[key] === 'object'){ |
| | | rdformatterAssign(args[key],formatter) |
| | | }else if(key === 'format' && typeof args[key] === 'string'){ |
| | | args['formatter'] = formatter[args[key]] ? formatter[args[key]] : undefined; |
| | | } |
| | | } |
| | | return args; |
| | | } |
| | | |
| | | export default { |
| | | data() { |
| | | return { |
| | | rid:null |
| | | } |
| | | }, |
| | | mounted() { |
| | | rootdom = {top:0,left:0} |
| | | let dm = document.querySelectorAll('uni-main')[0] |
| | | if(dm === undefined){ |
| | | dm = document.querySelectorAll('uni-page-wrapper')[0] |
| | | } |
| | | if(dm !== undefined){ |
| | | rootdom = {top:dm.offsetTop,left:dm.offsetLeft} |
| | | } |
| | | setTimeout(()=>{ |
| | | if(this.rid === null){ |
| | | this.$ownerInstance && this.$ownerInstance.callMethod('getRenderType') |
| | | } |
| | | },200) |
| | | }, |
| | | destroyed(){ |
| | | delete cfu.option[this.rid] |
| | | delete cfu.instance[this.rid] |
| | | delete cfe.option[this.rid] |
| | | delete cfe.instance[this.rid] |
| | | }, |
| | | methods: { |
| | | //==============以䏿¯EChartsçæ¹æ³==================== |
| | | ecinit(newVal, oldVal, owner, instance){ |
| | | let cid = JSON.stringify(newVal.id) |
| | | this.rid = cid |
| | | that[cid] = this.$ownerInstance || instance |
| | | let eopts = JSON.parse(JSON.stringify(newVal)) |
| | | let type = eopts.type; |
| | | //è½½å
¥å¹¶è¦çé»è®¤é
ç½® |
| | | if (type && cfe.type.includes(type)) { |
| | | cfe.option[cid] = rddeepCloneAssign({}, cfe[type], eopts); |
| | | }else{ |
| | | cfe.option[cid] = rddeepCloneAssign({}, eopts); |
| | | } |
| | | let newData = eopts.chartData; |
| | | if(newData){ |
| | | //æè½½categoriesåseries |
| | | if(cfe.option[cid].xAxis && cfe.option[cid].xAxis.type && cfe.option[cid].xAxis.type === 'category'){ |
| | | cfe.option[cid].xAxis.data = newData.categories |
| | | } |
| | | if(cfe.option[cid].yAxis && cfe.option[cid].yAxis.type && cfe.option[cid].yAxis.type === 'category'){ |
| | | cfe.option[cid].yAxis.data = newData.categories |
| | | } |
| | | cfe.option[cid].series = [] |
| | | for (var i = 0; i < newData.series.length; i++) { |
| | | cfe.option[cid].seriesTemplate = cfe.option[cid].seriesTemplate ? cfe.option[cid].seriesTemplate : {} |
| | | let Template = rddeepCloneAssign({},cfe.option[cid].seriesTemplate,newData.series[i]) |
| | | cfe.option[cid].series.push(Template) |
| | | } |
| | | } |
| | | |
| | | if (typeof window.echarts === 'object') { |
| | | this.newEChart() |
| | | }else{ |
| | | const script = document.createElement('script') |
| | | script.src = './uni_modules/qiun-data-charts/static/app-plus/echarts.min.js' |
| | | const rooturl = window.location.origin |
| | | const directory = instance.getDataset().directory |
| | | script.src = rooturl + directory + 'uni_modules/qiun-data-charts/static/h5/echarts.min.js' |
| | | script.onload = this.newEChart |
| | | document.head.appendChild(script) |
| | | } |
| | | }, |
| | | ecresize(newVal, oldVal, owner, instance){ |
| | | if(cfe.instance[this.rid]){ |
| | | cfe.instance[this.rid].resize() |
| | | } |
| | | }, |
| | | newEChart(){ |
| | | let cid = this.rid |
| | | if(cfe.instance[cid] === undefined){ |
| | | cfe.instance[cid] = echarts.init(that[cid].$el.children[0]) |
| | | //ontapå¼å¯åæè§¦åclickäºä»¶ |
| | | if(cfe.option[cid].ontap === true){ |
| | | cfe.instance[cid].on('click', resdata => { |
| | | let event = JSON.parse(JSON.stringify({ |
| | | x:resdata.event.offsetX,y:resdata.event.offsetY |
| | | })) |
| | | that[cid].callMethod('emitMsg',{name:"getIndex", params:{type:"getIndex", event:event, currentIndex:resdata.dataIndex, value:resdata.data, seriesName: resdata.seriesName,id:cid}}) |
| | | }) |
| | | // å¢å EChartsçhighlightæ¶æ¯ï¼å®ç°æä¸ç§»å¨è¿åç´¢å¼åè½ãadd by onefish åå»ºäº 2021-12-11 09:50 |
| | | cfe.instance[cid].on('highlight', resdata => { |
| | | that[cid].callMethod('emitMsg',{name:"getHighlight", params:{type:"highlight", res:resdata, id:cid}}) |
| | | }) |
| | | } |
| | | this.updataEChart(cid,cfe.option[cid]) |
| | | }else{ |
| | | this.updataEChart(cid,cfe.option[cid]) |
| | | } |
| | | }, |
| | | updataEChart(cid,option){ |
| | | //æ¿æ¢optionå
format屿§ä¸ºformatterçé¢å®ä¹æ¹æ³ |
| | | option = rdformatterAssign(option,cfe.formatter) |
| | | if(option.tooltip){ |
| | | option.tooltip.show = option.tooltipShow?true:false; |
| | | option.tooltip.position = this.tooltipPosition() |
| | | //tooltipFormatæ¹æ³ï¼æ¿æ¢ç»ä»¶çtooltipFormat为config-echarts.jså
对åºçæ¹æ³ |
| | | if (typeof option.tooltipFormat === 'string' && cfe.formatter[option.tooltipFormat]) { |
| | | option.tooltip.formatter = option.tooltip.formatter ? option.tooltip.formatter : cfe.formatter[option.tooltipFormat] |
| | | } |
| | | } |
| | | // é¢è²æ¸åæ·»å çæ¹æ³ |
| | | if (option.series) { |
| | | for (let i in option.series) { |
| | | let linearGradient = option.series[i].linearGradient |
| | | if (linearGradient) { |
| | | option.series[i].color = new echarts.graphic.LinearGradient(linearGradient[0],linearGradient[1],linearGradient[2],linearGradient[3],linearGradient[4]) |
| | | } |
| | | } |
| | | } |
| | | cfe.instance[cid].setOption(option, option.notMerge) |
| | | cfe.instance[cid].on('finished', function(){ |
| | | that[cid].callMethod('emitMsg',{name:"complete",params:{type:"complete",complete:true,id:cid}}) |
| | | if(cfe.instance[cid]){ |
| | | cfe.instance[cid].off('finished') |
| | | } |
| | | }); |
| | | |
| | | //ä¿®å¤initåå§åå®ä¾è·å宽é«ä¸æ£ç¡®é®é¢ |
| | | if( |
| | | typeof that[cid].$el.children[0].clientWidth != 'undefined' && |
| | | ( |
| | | Math.abs( that[cid].$el.children[0].clientWidth - cfe.instance[cid].getWidth() )>3 || |
| | | Math.abs( that[cid].$el.children[0].clientHeight - cfe.instance[cid].getHeight() )>3 |
| | | ) |
| | | ){this.ecresize();} |
| | | }, |
| | | tooltipPosition(){ |
| | | return (point, params, dom, rect, size) => { |
| | | let x = point[0] |
| | | let y = point[1] |
| | | let viewWidth = size.viewSize[0] |
| | | let viewHeight = size.viewSize[1] |
| | | let boxWidth = size.contentSize[0] |
| | | let boxHeight = size.contentSize[1] |
| | | let posX = x + 30 |
| | | let posY = y + 30 |
| | | if (posX + boxWidth > viewWidth) { |
| | | posX = x - boxWidth - 30 |
| | | } |
| | | if (posY + boxHeight > viewHeight) { |
| | | posY = y - boxHeight - 30 |
| | | } |
| | | return [posX, posY] |
| | | } |
| | | }, |
| | | //==============以䏿¯uChartsçæ¹æ³==================== |
| | | ucinit(newVal, oldVal, owner, instance){ |
| | | if(JSON.stringify(newVal) == JSON.stringify(oldVal)){ |
| | | return; |
| | | } |
| | | if(!newVal.canvasId){ |
| | | return; |
| | | } |
| | | let cid = JSON.parse(JSON.stringify(newVal.canvasId)) |
| | | this.rid = cid |
| | | that[cid] = this.$ownerInstance || instance |
| | | cfu.option[cid] = JSON.parse(JSON.stringify(newVal)) |
| | | cfu.option[cid] = rdformatterAssign(cfu.option[cid],cfu.formatter) |
| | | let canvasdom = document.getElementById(cid) |
| | | if(canvasdom && canvasdom.children[0]){ |
| | | cfu.option[cid].context = canvasdom.children[0].getContext("2d") |
| | | if(cfu.instance[cid] && cfu.option[cid] && cfu.option[cid].update === true){ |
| | | this.updataUChart() |
| | | }else{ |
| | | setTimeout(()=>{ |
| | | cfu.option[cid].context.restore(); |
| | | cfu.option[cid].context.save(); |
| | | this.newUChart() |
| | | },100) |
| | | } |
| | | } |
| | | }, |
| | | newUChart() { |
| | | let cid = this.rid |
| | | cfu.instance[cid] = new uChartsRD(cfu.option[cid]) |
| | | cfu.instance[cid].addEventListener('renderComplete', () => { |
| | | that[cid].callMethod('emitMsg',{name:"complete",params:{type:"complete",complete:true,id:cid, opts: cfu.instance[cid].opts}}) |
| | | cfu.instance[cid].delEventListener('renderComplete') |
| | | }); |
| | | cfu.instance[cid].addEventListener('scrollLeft', () => { |
| | | that[cid].callMethod('emitMsg',{name:"scrollLeft",params:{type:"scrollLeft",scrollLeft:true,id:cid, opts: cfu.instance[cid].opts}}) |
| | | }); |
| | | cfu.instance[cid].addEventListener('scrollRight', () => { |
| | | that[cid].callMethod('emitMsg',{name:"scrollRight",params:{type:"scrollRight",scrollRight:true,id:cid, opts: cfu.instance[cid].opts}}) |
| | | }); |
| | | }, |
| | | updataUChart() { |
| | | let cid = this.rid |
| | | cfu.instance[cid].updateData(cfu.option[cid]) |
| | | }, |
| | | tooltipDefault(item, category, index, opts) { |
| | | if (category) { |
| | | let data = item.data |
| | | if(typeof item.data === "object"){ |
| | | data = item.data.value |
| | | } |
| | | return category + ' ' + item.name + ':' + data; |
| | | } else { |
| | | if (item.properties && item.properties.name) { |
| | | return item.properties.name ; |
| | | } else { |
| | | return item.name + ':' + item.data; |
| | | } |
| | | } |
| | | }, |
| | | showTooltip(e,cid) { |
| | | let tc = cfu.option[cid].tooltipCustom |
| | | if (tc && tc !== undefined && tc !== null) { |
| | | let offset = undefined; |
| | | if (tc.x >= 0 && tc.y >= 0) { |
| | | offset = { x: tc.x, y: tc.y + 10 }; |
| | | } |
| | | cfu.instance[cid].showToolTip(e, { |
| | | index: tc.index, |
| | | offset: offset, |
| | | textList: tc.textList, |
| | | formatter: (item, category, index, opts) => { |
| | | if (typeof cfu.option[cid].tooltipFormat === 'string' && cfu.formatter[cfu.option[cid].tooltipFormat]) { |
| | | return cfu.formatter[cfu.option[cid].tooltipFormat](item, category, index, opts); |
| | | } else { |
| | | return this.tooltipDefault(item, category, index, opts); |
| | | } |
| | | } |
| | | }); |
| | | } else { |
| | | cfu.instance[cid].showToolTip(e, { |
| | | formatter: (item, category, index, opts) => { |
| | | if (typeof cfu.option[cid].tooltipFormat === 'string' && cfu.formatter[cfu.option[cid].tooltipFormat]) { |
| | | return cfu.formatter[cfu.option[cid].tooltipFormat](item, category, index, opts); |
| | | } else { |
| | | return this.tooltipDefault(item, category, index, opts); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | }, |
| | | tap(e) { |
| | | let cid = this.rid |
| | | let ontap = cfu.option[cid].ontap |
| | | let tooltipShow = cfu.option[cid].tooltipShow |
| | | let tapLegend = cfu.option[cid].tapLegend |
| | | if(ontap == false) return; |
| | | let currentIndex=null |
| | | let legendIndex=null |
| | | let rchartdom = document.getElementById('UC'+cid).getBoundingClientRect() |
| | | let tmpe = {} |
| | | if(e.detail.x){//tapæè
clickçäºä»¶ |
| | | tmpe = { x: e.detail.x - rchartdom.left, y:e.detail.y - rchartdom.top + rootdom.top} |
| | | }else{//mouseçäºä»¶ |
| | | tmpe = { x: e.clientX - rchartdom.left, y:e.clientY - rchartdom.top + rootdom.top} |
| | | } |
| | | e.changedTouches = []; |
| | | e.changedTouches.unshift(tmpe) |
| | | currentIndex=cfu.instance[cid].getCurrentDataIndex(e) |
| | | legendIndex=cfu.instance[cid].getLegendDataIndex(e) |
| | | if(tapLegend === true){ |
| | | cfu.instance[cid].touchLegend(e); |
| | | } |
| | | if(tooltipShow==true){ |
| | | this.showTooltip(e,cid) |
| | | } |
| | | that[cid].callMethod('emitMsg',{name:"getIndex",params:{type:"getIndex",event:tmpe,currentIndex:currentIndex,legendIndex:legendIndex,id:cid, opts: cfu.instance[cid].opts}}) |
| | | }, |
| | | touchStart(e) { |
| | | let cid = this.rid |
| | | let ontouch = cfu.option[cid].ontouch |
| | | if(ontouch == false) return; |
| | | if(cfu.option[cid].enableScroll === true && e.touches.length == 1){ |
| | | cfu.instance[cid].scrollStart(e); |
| | | } |
| | | that[cid].callMethod('emitMsg',{name:"getTouchStart",params:{type:"touchStart",event:e.changedTouches[0],id:cid, opts: cfu.instance[cid].opts}}) |
| | | }, |
| | | touchMove(e) { |
| | | let cid = this.rid |
| | | let ontouch = cfu.option[cid].ontouch |
| | | if(ontouch == false) return; |
| | | if(cfu.option[cid].enableScroll === true && e.changedTouches.length == 1){ |
| | | cfu.instance[cid].scroll(e); |
| | | } |
| | | if(cfu.option[cid].ontap === true && cfu.option[cid].enableScroll === false && cfu.option[cid].onmovetip === true){ |
| | | let rchartdom = document.getElementById('UC'+cid).getBoundingClientRect() |
| | | let tmpe = { x: e.changedTouches[0].clientX - rchartdom.left, y:e.changedTouches[0].clientY - rchartdom.top + rootdom.top} |
| | | e.changedTouches.unshift(tmpe) |
| | | if(cfu.option[cid].tooltipShow === true){ |
| | | this.showTooltip(e,cid) |
| | | } |
| | | } |
| | | if(ontouch === true && cfu.option[cid].enableScroll === true && cfu.option[cid].onzoom === true && e.changedTouches.length == 2){ |
| | | cfu.instance[cid].dobuleZoom(e); |
| | | } |
| | | that[cid].callMethod('emitMsg',{name:"getTouchMove",params:{type:"touchMove",event:e.changedTouches[0],id:cid, opts: cfu.instance[cid].opts}}) |
| | | }, |
| | | touchEnd(e) { |
| | | let cid = this.rid |
| | | let ontouch = cfu.option[cid].ontouch |
| | | if(ontouch == false) return; |
| | | if(cfu.option[cid].enableScroll === true && e.touches.length == 0){ |
| | | cfu.instance[cid].scrollEnd(e); |
| | | } |
| | | that[cid].callMethod('emitMsg',{name:"getTouchEnd",params:{type:"touchEnd",event:e.changedTouches[0],id:cid, opts: cfu.instance[cid].opts}}) |
| | | }, |
| | | mouseDown(e) { |
| | | let cid = this.rid |
| | | let onmouse = cfu.option[cid].onmouse |
| | | if(onmouse == false) return; |
| | | let rchartdom = document.getElementById('UC'+cid).getBoundingClientRect() |
| | | let tmpe = {} |
| | | tmpe = { x: e.clientX - rchartdom.left, y:e.clientY - rchartdom.top + rootdom.top} |
| | | e.changedTouches = []; |
| | | e.changedTouches.unshift(tmpe) |
| | | cfu.instance[cid].scrollStart(e) |
| | | cfu.option[cid].mousedown=true; |
| | | that[cid].callMethod('emitMsg',{name:"getTouchStart",params:{type:"mouseDown",event:tmpe,id:cid, opts: cfu.instance[cid].opts}}) |
| | | }, |
| | | mouseMove(e) { |
| | | let cid = this.rid |
| | | let onmouse = cfu.option[cid].onmouse |
| | | let tooltipShow = cfu.option[cid].tooltipShow |
| | | if(onmouse == false) return; |
| | | let rchartdom = document.getElementById('UC'+cid).getBoundingClientRect() |
| | | let tmpe = {} |
| | | tmpe = { x: e.clientX - rchartdom.left, y:e.clientY - rchartdom.top + rootdom.top} |
| | | e.changedTouches = []; |
| | | e.changedTouches.unshift(tmpe) |
| | | if(cfu.option[cid].mousedown){ |
| | | cfu.instance[cid].scroll(e) |
| | | that[cid].callMethod('emitMsg',{name:"getTouchMove",params:{type:"mouseMove",event:tmpe,id:cid, opts: cfu.instance[cid].opts}}) |
| | | }else if(cfu.instance[cid]){ |
| | | if(tooltipShow==true){ |
| | | this.showTooltip(e,cid) |
| | | } |
| | | } |
| | | }, |
| | | mouseUp(e) { |
| | | let cid = this.rid |
| | | let onmouse = cfu.option[cid].onmouse |
| | | if(onmouse == false) return; |
| | | let rchartdom = document.getElementById('UC'+cid).getBoundingClientRect() |
| | | let tmpe = {} |
| | | tmpe = { x: e.clientX - rchartdom.left, y:e.clientY - rchartdom.top + rootdom.top} |
| | | e.changedTouches = []; |
| | | e.changedTouches.unshift(tmpe) |
| | | cfu.instance[cid].scrollEnd(e) |
| | | cfu.option[cid].mousedown=false; |
| | | that[cid].callMethod('emitMsg',{name:"getTouchEnd",params:{type:"mouseUp",event:tmpe,id:cid, opts: cfu.instance[cid].opts}}) |
| | | }, |
| | | } |
| | | } |
| | | </script> |
| | | <!-- #endif --> |
| | | |
| | | <style scoped> |
| | | .chartsview { |
| | | width: 100%; |
| | | height: 100%; |
| | | display: flex; |
| | | flex: 1; |
| | | justify-content: center; |
| | | align-items: center; |
| | | } |
| | | </style> |