Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

qiun-data-charts.vue 51KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841
  1. <!--
  2. * qiun-data-charts 秋云高性能跨全端图表组件
  3. * Copyright (c) 2021 QIUN® 秋云 https://www.ucharts.cn All rights reserved.
  4. * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  5. * 复制使用请保留本段注释,感谢支持开源!
  6. * 为方便更多开发者使用,如有更好的建议请提交码云 Pull Requests !
  7. *
  8. * uCharts®官方网站
  9. * https://www.uCharts.cn
  10. *
  11. * 开源地址:
  12. * https://gitee.com/uCharts/uCharts
  13. *
  14. * uni-app插件市场地址:
  15. * http://ext.dcloud.net.cn/plugin?id=271
  16. *
  17. -->
  18. <template>
  19. <view class="chartsview" :id="'ChartBoxId'+cid">
  20. <view v-if="mixinDatacomLoading">
  21. <!-- 自定义加载状态,请改这里 -->
  22. <qiun-loading :loadingType="loadingType" />
  23. </view>
  24. <view v-if="mixinDatacomErrorMessage && errorShow" @tap="reloading">
  25. <!-- 自定义错误提示,请改这里 -->
  26. <qiun-error :errorMessage="errorMessage" />
  27. </view>
  28. <!-- APP和H5采用renderjs渲染图表 -->
  29. <!-- #ifdef APP-VUE || H5 -->
  30. <block v-if="echarts">
  31. <view :style="{ background: background }" style="width: 100%;height: 100%;" :data-directory="directory"
  32. :id="'EC'+cid" :prop="echartsOpts" :change:prop="rdcharts.ecinit" :resize="echartsResize"
  33. :change:resize="rdcharts.ecresize" v-show="showchart" />
  34. </block>
  35. <block v-else>
  36. <view v-on:tap="rdcharts.tap" v-on:mousemove="rdcharts.mouseMove" v-on:mousedown="rdcharts.mouseDown"
  37. v-on:mouseup="rdcharts.mouseUp" v-on:touchstart="rdcharts.touchStart"
  38. v-on:touchmove="rdcharts.touchMove" v-on:touchend="rdcharts.touchEnd" :id="'UC'+cid" :prop="uchartsOpts"
  39. :change:prop="rdcharts.ucinit">
  40. <canvas :id="cid" :canvasId="cid"
  41. :style="{ width: cWidth + 'px', height: cHeight + 'px', background: background }"
  42. :disable-scroll="disableScroll" @error="_error" v-show="showchart" />
  43. </view>
  44. </block>
  45. <!-- #endif -->
  46. <!-- 支付宝小程序 -->
  47. <!-- #ifdef MP-ALIPAY -->
  48. <block v-if="ontouch">
  49. <canvas :id="cid" :canvasId="cid" :width="cWidth * pixel" :height="cHeight * pixel"
  50. :style="{ width: cWidth + 'px', height: cHeight + 'px', background: background }"
  51. :disable-scroll="disScroll" @tap="_tap" @touchstart="_touchStart" @touchmove="_touchMove"
  52. @touchend="_touchEnd" @error="_error" v-show="showchart" />
  53. </block>
  54. <block v-if="!ontouch">
  55. <canvas :id="cid" :canvasId="cid" :width="cWidth * pixel" :height="cHeight * pixel"
  56. :style="{ width: cWidth + 'px', height: cHeight + 'px', background: background }"
  57. :disable-scroll="disScroll" @tap="_tap" @error="_error" v-show="showchart" />
  58. </block>
  59. <!-- #endif -->
  60. <!-- 其他小程序通过vue渲染图表 -->
  61. <!-- #ifdef MP-WEIXIN || MP-BAIDU || MP-QQ || MP-TOUTIAO || MP-KUAISHOU || MP-LARK || MP-JD || MP-360 -->
  62. <block v-if="type2d">
  63. <view v-if="ontouch" @tap="_tap">
  64. <canvas :id="cid" :canvasId="cid"
  65. :style="{ width: cWidth + 'px', height: cHeight + 'px', background: background }" type="2d"
  66. :disable-scroll="disScroll" @touchstart="_touchStart" @touchmove="_touchMove" @touchend="_touchEnd"
  67. @error="_error" v-show="showchart" />
  68. </view>
  69. <view v-if="!ontouch" @tap="_tap">
  70. <canvas :id="cid" :canvasId="cid"
  71. :style="{ width: cWidth + 'px', height: cHeight + 'px', background: background }" type="2d"
  72. :disable-scroll="disScroll" @error="_error" v-show="showchart" />
  73. </view>
  74. </block>
  75. <block v-if="!type2d">
  76. <view v-if="ontouch" @tap="_tap">
  77. <canvas :id="cid" :canvasId="cid"
  78. :style="{ width: cWidth + 'px', height: cHeight + 'px', background: background }"
  79. @touchstart="_touchStart" @touchmove="_touchMove" @touchend="_touchEnd" :disable-scroll="disScroll"
  80. @error="_error" v-if="showchart" />
  81. </view>
  82. <view v-if="!ontouch">
  83. <canvas :id="cid" :canvasId="cid"
  84. :style="{ width: cWidth + 'px', height: cHeight + 'px', background: background }"
  85. :disable-scroll="disScroll" @tap="_tap" @error="_error" v-if="showchart" />
  86. </view>
  87. </block>
  88. <!-- #endif -->
  89. </view>
  90. </template>
  91. <script>
  92. import uCharts from '../../js_sdk/u-charts/u-charts.js';
  93. import cfu from '../../js_sdk/u-charts/config-ucharts.js';
  94. // #ifdef APP-VUE || H5
  95. import cfe from '../../js_sdk/u-charts/config-echarts.js';
  96. // #endif
  97. function deepCloneAssign(origin = {}, ...args) {
  98. for (let i in args) {
  99. for (let key in args[i]) {
  100. if (args[i].hasOwnProperty(key)) {
  101. origin[key] = args[i][key] && typeof args[i][key] === 'object' ? deepCloneAssign(Array.isArray(args[i][
  102. key
  103. ]) ? [] : {}, origin[key], args[i][key]) : args[i][key];
  104. }
  105. }
  106. }
  107. return origin;
  108. }
  109. function formatterAssign(args, formatter) {
  110. for (let key in args) {
  111. if (args.hasOwnProperty(key) && args[key] !== null && typeof args[key] === 'object') {
  112. formatterAssign(args[key], formatter)
  113. } else if (key === 'format' && typeof args[key] === 'string') {
  114. args['formatter'] = formatter[args[key]] ? formatter[args[key]] : undefined;
  115. }
  116. }
  117. return args;
  118. }
  119. // 时间转换函数,为了匹配uniClinetDB读取出的时间与categories不同
  120. function getFormatDate(date) {
  121. var seperator = "-";
  122. var year = date.getFullYear();
  123. var month = date.getMonth() + 1;
  124. var strDate = date.getDate();
  125. if (month >= 1 && month <= 9) {
  126. month = "0" + month;
  127. }
  128. if (strDate >= 0 && strDate <= 9) {
  129. strDate = "0" + strDate;
  130. }
  131. var currentdate = year + seperator + month + seperator + strDate;
  132. return currentdate;
  133. }
  134. var lastMoveTime = null;
  135. /**
  136. * 防抖
  137. *
  138. * @param { Function } fn 要执行的方法
  139. * @param { Number } wait 防抖多少毫秒
  140. *
  141. * 在 vue 中使用(注意:不能使用箭头函数,否则this指向不对,并且不能再次封装如:
  142. * move(){ // 错误调用方式
  143. * debounce(function () {
  144. * console.log(this.title);
  145. * }, 1000)});
  146. * 应该直接使用:// 正确调用方式
  147. * move: debounce(function () {
  148. * console.log(this.title);
  149. * }, 1000)
  150. */
  151. function debounce(fn, wait) {
  152. let timer = false;
  153. return function() {
  154. clearTimeout(timer);
  155. timer && clearTimeout(timer);
  156. timer = setTimeout(() => {
  157. timer = false;
  158. fn.apply(this, arguments); // 把参数传进去
  159. }, wait);
  160. };
  161. }
  162. export default {
  163. name: 'qiun-data-charts',
  164. mixins: [uniCloud.mixinDatacom],
  165. props: {
  166. type: {
  167. type: String,
  168. default: null
  169. },
  170. canvasId: {
  171. type: String,
  172. default: 'uchartsid'
  173. },
  174. canvas2d: {
  175. type: Boolean,
  176. default: false
  177. },
  178. background: {
  179. type: String,
  180. default: 'rgba(0,0,0,0)'
  181. },
  182. animation: {
  183. type: Boolean,
  184. default: true
  185. },
  186. chartData: {
  187. type: Object,
  188. default () {
  189. return {
  190. categories: [],
  191. series: []
  192. };
  193. }
  194. },
  195. opts: {
  196. type: Object,
  197. default () {
  198. return {};
  199. }
  200. },
  201. eopts: {
  202. type: Object,
  203. default () {
  204. return {};
  205. }
  206. },
  207. loadingType: {
  208. type: Number,
  209. default: 2
  210. },
  211. errorShow: {
  212. type: Boolean,
  213. default: true
  214. },
  215. errorReload: {
  216. type: Boolean,
  217. default: true
  218. },
  219. errorMessage: {
  220. type: String,
  221. default: null
  222. },
  223. inScrollView: {
  224. type: Boolean,
  225. default: false
  226. },
  227. reshow: {
  228. type: Boolean,
  229. default: false
  230. },
  231. reload: {
  232. type: Boolean,
  233. default: false
  234. },
  235. disableScroll: {
  236. type: Boolean,
  237. default: false
  238. },
  239. optsWatch: {
  240. type: Boolean,
  241. default: true
  242. },
  243. onzoom: {
  244. type: Boolean,
  245. default: false
  246. },
  247. ontap: {
  248. type: Boolean,
  249. default: true
  250. },
  251. ontouch: {
  252. type: Boolean,
  253. default: false
  254. },
  255. onmouse: {
  256. type: Boolean,
  257. default: true
  258. },
  259. onmovetip: {
  260. type: Boolean,
  261. default: false
  262. },
  263. echartsH5: {
  264. type: Boolean,
  265. default: false
  266. },
  267. echartsApp: {
  268. type: Boolean,
  269. default: false
  270. },
  271. tooltipShow: {
  272. type: Boolean,
  273. default: true
  274. },
  275. tooltipFormat: {
  276. type: String,
  277. default: undefined
  278. },
  279. tooltipCustom: {
  280. type: Object,
  281. default: undefined
  282. },
  283. startDate: {
  284. type: String,
  285. default: undefined
  286. },
  287. endDate: {
  288. type: String,
  289. default: undefined
  290. },
  291. textEnum: {
  292. type: Array,
  293. default () {
  294. return []
  295. }
  296. },
  297. groupEnum: {
  298. type: Array,
  299. default () {
  300. return []
  301. }
  302. },
  303. pageScrollTop: {
  304. type: Number,
  305. default: 0
  306. },
  307. directory: {
  308. type: String,
  309. default: '/'
  310. },
  311. tapLegend: {
  312. type: Boolean,
  313. default: true
  314. },
  315. menus: {
  316. type: Array,
  317. default () {
  318. return []
  319. }
  320. }
  321. },
  322. data() {
  323. return {
  324. cid: 'uchartsid',
  325. inWx: false,
  326. inAli: false,
  327. inTt: false,
  328. inBd: false,
  329. inH5: false,
  330. inApp: false,
  331. inWin: false,
  332. type2d: true,
  333. disScroll: false,
  334. openmouse: false,
  335. pixel: 1,
  336. cWidth: 375,
  337. cHeight: 250,
  338. showchart: false,
  339. echarts: false,
  340. echartsResize: {
  341. state: false
  342. },
  343. uchartsOpts: {},
  344. echartsOpts: {},
  345. drawData: {},
  346. lastDrawTime: null,
  347. };
  348. },
  349. created() {
  350. this.cid = this.canvasId
  351. if (this.canvasId == 'uchartsid' || this.canvasId == '') {
  352. let t = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  353. let len = t.length
  354. let id = ''
  355. for (let i = 0; i < 32; i++) {
  356. id += t.charAt(Math.floor(Math.random() * len))
  357. }
  358. this.cid = id
  359. }
  360. const systemInfo = uni.getSystemInfoSync()
  361. if (systemInfo.platform === 'windows' || systemInfo.platform === 'mac') {
  362. this.inWin = true;
  363. }
  364. // #ifdef MP-WEIXIN
  365. this.inWx = true;
  366. if (this.canvas2d === false || systemInfo.platform === 'windows' || systemInfo.platform === 'mac') {
  367. this.type2d = false;
  368. } else {
  369. this.type2d = true;
  370. this.pixel = systemInfo.pixelRatio;
  371. }
  372. // #endif
  373. //非微信小程序端强制关闭canvas2d模式
  374. // #ifndef MP-WEIXIN
  375. this.type2d = false;
  376. // #endif
  377. // #ifdef MP-TOUTIAO || MP-LARK || MP-ALIPAY
  378. this.type2d = this.canvas2d;
  379. // #endif
  380. // #ifdef MP-ALIPAY
  381. this.inAli = true;
  382. this.pixel = systemInfo.pixelRatio;
  383. // #endif
  384. // #ifdef MP-BAIDU
  385. this.inBd = true;
  386. // #endif
  387. // #ifdef MP-TOUTIAO
  388. this.inTt = true;
  389. // #endif
  390. this.disScroll = this.disableScroll;
  391. },
  392. mounted() {
  393. // #ifdef APP-VUE
  394. this.inApp = true;
  395. if (this.echartsApp === true) {
  396. this.echarts = true;
  397. this.openmouse = false;
  398. }
  399. // #endif
  400. // #ifdef APP-NVUE
  401. this.inApp = true;
  402. this.mixinDatacomLoading = false
  403. this.mixinDatacomErrorMessage = "暂不支持NVUE"
  404. // #endif
  405. // #ifdef H5
  406. this.inH5 = true;
  407. if (this.inWin === true) {
  408. this.openmouse = this.onmouse;
  409. }
  410. if (this.echartsH5 === true) {
  411. this.echarts = true;
  412. }
  413. // #endif
  414. this.$nextTick(() => {
  415. this.beforeInit();
  416. })
  417. // #ifndef MP-ALIPAY || MP-BAIDU || MP-TOUTIAO || APP-VUE
  418. const time = this.inH5 ? 500 : 200;
  419. const _this = this;
  420. uni.onWindowResize(
  421. debounce(function(res) {
  422. if (_this.mixinDatacomLoading == true) {
  423. return;
  424. }
  425. let errmsg = _this.mixinDatacomErrorMessage;
  426. if (errmsg !== null && errmsg !== 'null' && errmsg !== '') {
  427. return;
  428. }
  429. if (_this.echarts) {
  430. _this.echartsResize.state = !_this.echartsResize.state;
  431. } else {
  432. _this.resizeHandler();
  433. }
  434. }, time)
  435. );
  436. // #endif
  437. },
  438. destroyed() {
  439. if (this.echarts === true) {
  440. delete cfe.option[this.cid]
  441. delete cfe.instance[this.cid]
  442. } else {
  443. delete cfu.option[this.cid]
  444. delete cfu.instance[this.cid]
  445. }
  446. // #ifndef MP-ALIPAY || MP-BAIDU || MP-TOUTIAO
  447. uni.offWindowResize(() => {})
  448. // #endif
  449. },
  450. watch: {
  451. chartDataProps: {
  452. handler(val, oldval) {
  453. if (typeof val === 'object') {
  454. if (JSON.stringify(val) !== JSON.stringify(oldval)) {
  455. this._clearChart();
  456. if (val.series && val.series.length > 0) {
  457. this.beforeInit();
  458. } else {
  459. this.mixinDatacomLoading = true;
  460. this.showchart = false;
  461. this.mixinDatacomErrorMessage = null;
  462. }
  463. }
  464. } else {
  465. this.mixinDatacomLoading = false;
  466. this._clearChart();
  467. this.showchart = false;
  468. this.mixinDatacomErrorMessage = '参数错误:chartData数据类型错误';
  469. }
  470. },
  471. immediate: false,
  472. deep: true
  473. },
  474. localdata: {
  475. handler(val, oldval) {
  476. if (JSON.stringify(val) !== JSON.stringify(oldval)) {
  477. if (val.length > 0) {
  478. this.beforeInit();
  479. } else {
  480. this.mixinDatacomLoading = true;
  481. this._clearChart();
  482. this.showchart = false;
  483. this.mixinDatacomErrorMessage = null;
  484. }
  485. }
  486. },
  487. immediate: false,
  488. deep: true
  489. },
  490. optsProps: {
  491. handler(val, oldval) {
  492. if (typeof val === 'object') {
  493. if (JSON.stringify(val) !== JSON.stringify(oldval) && this.echarts === false && this.optsWatch ==
  494. true) {
  495. this.checkData(this.drawData);
  496. }
  497. } else {
  498. this.mixinDatacomLoading = false;
  499. this._clearChart();
  500. this.showchart = false;
  501. this.mixinDatacomErrorMessage = '参数错误:opts数据类型错误';
  502. }
  503. },
  504. immediate: false,
  505. deep: true
  506. },
  507. eoptsProps: {
  508. handler(val, oldval) {
  509. if (typeof val === 'object') {
  510. if (JSON.stringify(val) !== JSON.stringify(oldval) && this.echarts === true) {
  511. this.checkData(this.drawData);
  512. }
  513. } else {
  514. this.mixinDatacomLoading = false;
  515. this.showchart = false;
  516. this.mixinDatacomErrorMessage = '参数错误:eopts数据类型错误';
  517. }
  518. },
  519. immediate: false,
  520. deep: true
  521. },
  522. reshow(val, oldval) {
  523. if (val === true && this.mixinDatacomLoading === false) {
  524. setTimeout(() => {
  525. this.mixinDatacomErrorMessage = null;
  526. this.echartsResize.state = !this.echartsResize.state;
  527. this.checkData(this.drawData);
  528. }, 200);
  529. }
  530. },
  531. reload(val, oldval) {
  532. if (val === true) {
  533. this.showchart = false;
  534. this.mixinDatacomErrorMessage = null;
  535. this.reloading();
  536. }
  537. },
  538. mixinDatacomErrorMessage(val, oldval) {
  539. if (val) {
  540. this.emitMsg({
  541. name: 'error',
  542. params: {
  543. type: "error",
  544. errorShow: this.errorShow,
  545. msg: val,
  546. id: this.cid
  547. }
  548. });
  549. if (this.errorShow) {
  550. console.log('[秋云图表组件]' + val);
  551. }
  552. }
  553. },
  554. errorMessage(val, oldval) {
  555. if (val && this.errorShow && val !== null && val !== 'null' && val !== '') {
  556. this.showchart = false;
  557. this.mixinDatacomLoading = false;
  558. this.mixinDatacomErrorMessage = val;
  559. } else {
  560. this.showchart = false;
  561. this.mixinDatacomErrorMessage = null;
  562. this.reloading();
  563. }
  564. }
  565. },
  566. computed: {
  567. optsProps() {
  568. return JSON.parse(JSON.stringify(this.opts));
  569. },
  570. eoptsProps() {
  571. return JSON.parse(JSON.stringify(this.eopts));
  572. },
  573. chartDataProps() {
  574. return JSON.parse(JSON.stringify(this.chartData));
  575. },
  576. },
  577. methods: {
  578. beforeInit() {
  579. this.mixinDatacomErrorMessage = null;
  580. if (typeof this.chartData === 'object' && this.chartData != null && this.chartData.series !== undefined &&
  581. this.chartData.series.length > 0) {
  582. //拷贝一下chartData,为了opts变更后统一数据来源
  583. this.drawData = deepCloneAssign({}, this.chartData);
  584. this.mixinDatacomLoading = false;
  585. this.showchart = true;
  586. this.checkData(this.chartData);
  587. } else if (this.localdata.length > 0) {
  588. this.mixinDatacomLoading = false;
  589. this.showchart = true;
  590. this.localdataInit(this.localdata);
  591. } else if (this.collection !== '') {
  592. this.mixinDatacomLoading = false;
  593. this.getCloudData();
  594. } else {
  595. this.mixinDatacomLoading = true;
  596. }
  597. },
  598. localdataInit(resdata) {
  599. //替换enum类型为正确的描述
  600. if (this.groupEnum.length > 0) {
  601. for (let i = 0; i < resdata.length; i++) {
  602. for (let j = 0; j < this.groupEnum.length; j++) {
  603. if (resdata[i].group === this.groupEnum[j].value) {
  604. resdata[i].group = this.groupEnum[j].text
  605. }
  606. }
  607. }
  608. }
  609. if (this.textEnum.length > 0) {
  610. for (let i = 0; i < resdata.length; i++) {
  611. for (let j = 0; j < this.textEnum.length; j++) {
  612. if (resdata[i].text === this.textEnum[j].value) {
  613. resdata[i].text = this.textEnum[j].text
  614. }
  615. }
  616. }
  617. }
  618. let needCategories = false;
  619. let tmpData = {
  620. categories: [],
  621. series: []
  622. }
  623. let tmpcategories = []
  624. let tmpseries = [];
  625. //拼接categories
  626. if (this.echarts === true) {
  627. needCategories = cfe.categories.includes(this.type)
  628. } else {
  629. needCategories = cfu.categories.includes(this.type)
  630. }
  631. if (needCategories === true) {
  632. //如果props中的chartData带有categories,则优先使用chartData的categories
  633. if (this.chartData && this.chartData.categories && this.chartData.categories.length > 0) {
  634. tmpcategories = this.chartData.categories
  635. } else {
  636. //如果是日期类型的数据,不管是本地数据还是云数据,都按起止日期自动拼接categories
  637. if (this.startDate && this.endDate) {
  638. let idate = new Date(this.startDate)
  639. let edate = new Date(this.endDate)
  640. while (idate <= edate) {
  641. tmpcategories.push(getFormatDate(idate))
  642. idate = idate.setDate(idate.getDate() + 1)
  643. idate = new Date(idate)
  644. }
  645. //否则从结果中去重并拼接categories
  646. } else {
  647. let tempckey = {};
  648. resdata.map(function(item, index) {
  649. if (item.text != undefined && !tempckey[item.text]) {
  650. tmpcategories.push(item.text)
  651. tempckey[item.text] = true
  652. }
  653. });
  654. }
  655. }
  656. tmpData.categories = tmpcategories
  657. }
  658. //拼接series
  659. let tempskey = {};
  660. resdata.map(function(item, index) {
  661. if (item.group != undefined && !tempskey[item.group]) {
  662. tmpseries.push({
  663. name: item.group,
  664. data: []
  665. });
  666. tempskey[item.group] = true;
  667. }
  668. });
  669. //如果没有获取到分组名称(可能是带categories的数据,也可能是不带的饼图类)
  670. if (tmpseries.length == 0) {
  671. tmpseries = [{
  672. name: '默认分组',
  673. data: []
  674. }];
  675. //如果是需要categories的图表类型
  676. if (needCategories === true) {
  677. for (let j = 0; j < tmpcategories.length; j++) {
  678. let seriesdata = 0;
  679. for (let i = 0; i < resdata.length; i++) {
  680. if (resdata[i].text == tmpcategories[j]) {
  681. seriesdata = resdata[i].value;
  682. }
  683. }
  684. tmpseries[0].data.push(seriesdata);
  685. }
  686. //如果是饼图类的图表类型
  687. } else {
  688. for (let i = 0; i < resdata.length; i++) {
  689. tmpseries[0].data.push({
  690. "name": resdata[i].text,
  691. "value": resdata[i].value
  692. });
  693. }
  694. }
  695. //如果有分组名
  696. } else {
  697. for (let k = 0; k < tmpseries.length; k++) {
  698. //如果有categories
  699. if (tmpcategories.length > 0) {
  700. for (let j = 0; j < tmpcategories.length; j++) {
  701. let seriesdata = 0;
  702. for (let i = 0; i < resdata.length; i++) {
  703. if (tmpseries[k].name == resdata[i].group && resdata[i].text == tmpcategories[j]) {
  704. seriesdata = resdata[i].value;
  705. }
  706. }
  707. tmpseries[k].data.push(seriesdata);
  708. }
  709. //如果传了group而没有传text,即没有categories(正常情况下这种数据是不符合数据要求规范的)
  710. } else {
  711. for (let i = 0; i < resdata.length; i++) {
  712. if (tmpseries[k].name == resdata[i].group) {
  713. tmpseries[k].data.push(resdata[i].value);
  714. }
  715. }
  716. }
  717. }
  718. }
  719. tmpData.series = tmpseries
  720. //拷贝一下chartData,为了opts变更后统一数据来源
  721. this.drawData = deepCloneAssign({}, tmpData);
  722. this.checkData(tmpData)
  723. },
  724. reloading() {
  725. if (this.errorReload === false) {
  726. return;
  727. }
  728. this.showchart = false;
  729. this.mixinDatacomErrorMessage = null;
  730. if (this.collection !== '') {
  731. this.mixinDatacomLoading = false;
  732. this.onMixinDatacomPropsChange(true);
  733. } else {
  734. this.beforeInit();
  735. }
  736. },
  737. checkData(anyData) {
  738. let cid = this.cid
  739. //复位opts或eopts
  740. if (this.echarts === true) {
  741. cfe.option[cid] = deepCloneAssign({}, this.eopts);
  742. cfe.option[cid].id = cid;
  743. cfe.option[cid].type = this.type;
  744. } else {
  745. if (this.type && cfu.type.includes(this.type)) {
  746. cfu.option[cid] = deepCloneAssign({}, cfu[this.type], this.opts);
  747. cfu.option[cid].canvasId = cid;
  748. } else {
  749. this.mixinDatacomLoading = false;
  750. this.showchart = false;
  751. this.mixinDatacomErrorMessage = '参数错误:props参数中type类型不正确';
  752. }
  753. }
  754. //挂载categories和series
  755. let newData = deepCloneAssign({}, anyData);
  756. if (newData.series !== undefined && newData.series.length > 0) {
  757. this.mixinDatacomErrorMessage = null;
  758. if (this.echarts === true) {
  759. cfe.option[cid].chartData = newData;
  760. this.$nextTick(() => {
  761. this.init()
  762. })
  763. } else {
  764. cfu.option[cid].categories = newData.categories;
  765. cfu.option[cid].series = newData.series;
  766. this.$nextTick(() => {
  767. this.init()
  768. })
  769. }
  770. }
  771. },
  772. resizeHandler() {
  773. //渲染防抖
  774. let currTime = Date.now();
  775. let lastDrawTime = this.lastDrawTime ? this.lastDrawTime : currTime - 3000;
  776. let duration = currTime - lastDrawTime;
  777. if (duration < 1000) return;
  778. let chartdom = uni
  779. .createSelectorQuery()
  780. // #ifndef MP-ALIPAY
  781. .in(this)
  782. // #endif
  783. .select('#ChartBoxId' + this.cid)
  784. .boundingClientRect(data => {
  785. this.showchart = true;
  786. if (data.width > 0 && data.height > 0) {
  787. if (data.width !== this.cWidth || data.height !== this.cHeight) {
  788. this.checkData(this.drawData)
  789. }
  790. }
  791. })
  792. .exec();
  793. },
  794. getCloudData() {
  795. if (this.mixinDatacomLoading == true) {
  796. return;
  797. }
  798. this.mixinDatacomLoading = true;
  799. this.mixinDatacomGet()
  800. .then(res => {
  801. this.mixinDatacomResData = res.result.data;
  802. this.localdataInit(this.mixinDatacomResData);
  803. })
  804. .catch(err => {
  805. this.mixinDatacomLoading = false;
  806. this.showchart = false;
  807. this.mixinDatacomErrorMessage = '请求错误:' + err;
  808. });
  809. },
  810. onMixinDatacomPropsChange(needReset, changed) {
  811. if (needReset == true && this.collection !== '') {
  812. this.showchart = false;
  813. this.mixinDatacomErrorMessage = null;
  814. this._clearChart();
  815. this.getCloudData();
  816. }
  817. },
  818. _clearChart() {
  819. let cid = this.cid
  820. if (this.echarts !== true && cfu.option[cid] && cfu.option[cid].context) {
  821. const ctx = cfu.option[cid].context;
  822. if (typeof ctx === "object" && !!!cfu.option[cid].update) {
  823. ctx.clearRect(0, 0, this.cWidth * this.pixel, this.cHeight * this.pixel);
  824. ctx.draw();
  825. }
  826. }
  827. },
  828. init() {
  829. let cid = this.cid
  830. let chartdom = uni
  831. .createSelectorQuery()
  832. // #ifndef MP-ALIPAY
  833. .in(this)
  834. // #endif
  835. .select('#ChartBoxId' + cid)
  836. .boundingClientRect(data => {
  837. if (data.width > 0 && data.height > 0) {
  838. this.mixinDatacomLoading = false;
  839. this.showchart = true;
  840. this.lastDrawTime = Date.now();
  841. this.cWidth = data.width;
  842. this.cHeight = data.height;
  843. if (this.echarts !== true) {
  844. cfu.option[cid].background = this.background == 'rgba(0,0,0,0)' ? '#FFFFFF' : this
  845. .background;
  846. cfu.option[cid].canvas2d = this.type2d;
  847. cfu.option[cid].pixelRatio = this.pixel;
  848. cfu.option[cid].animation = this.animation;
  849. cfu.option[cid].width = data.width * this.pixel;
  850. cfu.option[cid].height = data.height * this.pixel;
  851. cfu.option[cid].onzoom = this.onzoom;
  852. cfu.option[cid].ontap = this.ontap;
  853. cfu.option[cid].ontouch = this.ontouch;
  854. cfu.option[cid].onmouse = this.openmouse;
  855. cfu.option[cid].onmovetip = this.onmovetip;
  856. cfu.option[cid].tooltipShow = this.tooltipShow;
  857. cfu.option[cid].tooltipFormat = this.tooltipFormat;
  858. cfu.option[cid].tooltipCustom = this.tooltipCustom;
  859. cfu.option[cid].inScrollView = this.inScrollView;
  860. cfu.option[cid].lastDrawTime = this.lastDrawTime;
  861. cfu.option[cid].tapLegend = this.tapLegend;
  862. }
  863. //如果是H5或者App端,采用renderjs渲染图表
  864. if (this.inH5 || this.inApp) {
  865. if (this.echarts == true) {
  866. cfe.option[cid].ontap = this.ontap;
  867. cfe.option[cid].onmouse = this.openmouse;
  868. cfe.option[cid].tooltipShow = this.tooltipShow;
  869. cfe.option[cid].tooltipFormat = this.tooltipFormat;
  870. cfe.option[cid].tooltipCustom = this.tooltipCustom;
  871. cfe.option[cid].lastDrawTime = this.lastDrawTime;
  872. this.echartsOpts = deepCloneAssign({}, cfe.option[cid]);
  873. } else {
  874. cfu.option[cid].rotateLock = cfu.option[cid].rotate;
  875. this.uchartsOpts = deepCloneAssign({}, cfu.option[cid]);
  876. }
  877. //如果是小程序端,采用uCharts渲染
  878. } else {
  879. cfu.option[cid] = formatterAssign(cfu.option[cid], cfu.formatter)
  880. this.mixinDatacomErrorMessage = null;
  881. this.mixinDatacomLoading = false;
  882. this.showchart = true;
  883. this.$nextTick(() => {
  884. if (this.type2d === true) {
  885. const query = uni.createSelectorQuery().in(this)
  886. query
  887. .select('#' + cid)
  888. .fields({
  889. node: true,
  890. size: true
  891. })
  892. .exec(res => {
  893. if (res[0]) {
  894. const canvas = res[0].node;
  895. const ctx = canvas.getContext('2d');
  896. cfu.option[cid].context = ctx;
  897. cfu.option[cid].rotateLock = cfu.option[cid].rotate;
  898. if (cfu.instance[cid] && cfu.option[cid] && cfu.option[
  899. cid].update === true) {
  900. this._updataUChart(cid)
  901. } else {
  902. canvas.width = data.width * this.pixel;
  903. canvas.height = data.height * this.pixel;
  904. canvas._width = data.width * this.pixel;
  905. canvas._height = data.height * this.pixel;
  906. setTimeout(() => {
  907. cfu.option[cid].context.restore();
  908. cfu.option[cid].context.save();
  909. this._newChart(cid)
  910. }, 100)
  911. }
  912. } else {
  913. this.showchart = false;
  914. this.mixinDatacomErrorMessage =
  915. '参数错误:开启2d模式后,未获取到dom节点,canvas-id:' + cid;
  916. }
  917. });
  918. } else {
  919. if (this.inAli) {
  920. cfu.option[cid].rotateLock = cfu.option[cid].rotate;
  921. }
  922. cfu.option[cid].context = uni.createCanvasContext(cid, this);
  923. if (cfu.instance[cid] && cfu.option[cid] && cfu.option[cid].update ===
  924. true) {
  925. this._updataUChart(cid)
  926. } else {
  927. setTimeout(() => {
  928. cfu.option[cid].context.restore();
  929. cfu.option[cid].context.save();
  930. this._newChart(cid)
  931. }, 100)
  932. }
  933. }
  934. })
  935. }
  936. } else {
  937. this.mixinDatacomLoading = false;
  938. this.showchart = false;
  939. if (this.reshow == true) {
  940. this.mixinDatacomErrorMessage = '布局错误:未获取到父元素宽高尺寸!canvas-id:' + cid;
  941. }
  942. }
  943. })
  944. .exec();
  945. },
  946. saveImage() {
  947. uni.canvasToTempFilePath({
  948. canvasId: this.cid,
  949. success: res => {
  950. //#ifdef H5
  951. var a = document.createElement("a");
  952. a.href = res.tempFilePath;
  953. a.download = this.cid;
  954. a.target = '_blank'
  955. a.click();
  956. //#endif
  957. //#ifndef H5
  958. uni.saveImageToPhotosAlbum({
  959. filePath: res.tempFilePath,
  960. success: function() {
  961. uni.showToast({
  962. title: '保存成功',
  963. duration: 2000
  964. });
  965. }
  966. });
  967. //#endif
  968. }
  969. }, this);
  970. },
  971. getImage() {
  972. if (this.type2d == false) {
  973. uni.canvasToTempFilePath({
  974. canvasId: this.cid,
  975. success: res => {
  976. this.emitMsg({
  977. name: 'getImage',
  978. params: {
  979. type: "getImage",
  980. base64: res.tempFilePath
  981. }
  982. });
  983. }
  984. }, this);
  985. } else {
  986. const query = uni.createSelectorQuery().in(this)
  987. query
  988. .select('#' + this.cid)
  989. .fields({
  990. node: true,
  991. size: true
  992. })
  993. .exec(res => {
  994. if (res[0]) {
  995. const canvas = res[0].node;
  996. this.emitMsg({
  997. name: 'getImage',
  998. params: {
  999. type: "getImage",
  1000. base64: canvas.toDataURL('image/png')
  1001. }
  1002. });
  1003. }
  1004. });
  1005. }
  1006. },
  1007. // #ifndef APP-VUE || H5
  1008. _newChart(cid) {
  1009. if (this.mixinDatacomLoading == true) {
  1010. return;
  1011. }
  1012. this.showchart = true;
  1013. cfu.instance[cid] = new uCharts(cfu.option[cid]);
  1014. cfu.instance[cid].addEventListener('renderComplete', () => {
  1015. this.emitMsg({
  1016. name: 'complete',
  1017. params: {
  1018. type: "complete",
  1019. complete: true,
  1020. id: cid,
  1021. opts: cfu.instance[cid].opts
  1022. }
  1023. });
  1024. cfu.instance[cid].delEventListener('renderComplete')
  1025. });
  1026. cfu.instance[cid].addEventListener('scrollLeft', () => {
  1027. this.emitMsg({
  1028. name: 'scrollLeft',
  1029. params: {
  1030. type: "scrollLeft",
  1031. scrollLeft: true,
  1032. id: cid,
  1033. opts: cfu.instance[cid].opts
  1034. }
  1035. });
  1036. });
  1037. cfu.instance[cid].addEventListener('scrollRight', () => {
  1038. this.emitMsg({
  1039. name: 'scrollRight',
  1040. params: {
  1041. type: "scrollRight",
  1042. scrollRight: true,
  1043. id: cid,
  1044. opts: cfu.instance[cid].opts
  1045. }
  1046. });
  1047. });
  1048. },
  1049. _updataUChart(cid) {
  1050. cfu.instance[cid].updateData(cfu.option[cid])
  1051. },
  1052. _tooltipDefault(item, category, index, opts) {
  1053. if (category) {
  1054. let data = item.data
  1055. if (typeof item.data === "object") {
  1056. data = item.data.value
  1057. }
  1058. return category + ' ' + item.name + ':' + data;
  1059. } else {
  1060. if (item.properties && item.properties.name) {
  1061. return item.properties.name;
  1062. } else {
  1063. return item.name + ':' + item.data;
  1064. }
  1065. }
  1066. },
  1067. _showTooltip(e) {
  1068. let cid = this.cid
  1069. let tc = cfu.option[cid].tooltipCustom
  1070. if (tc && tc !== undefined && tc !== null) {
  1071. let offset = undefined;
  1072. if (tc.x >= 0 && tc.y >= 0) {
  1073. offset = {
  1074. x: tc.x,
  1075. y: tc.y + 10
  1076. };
  1077. }
  1078. cfu.instance[cid].showToolTip(e, {
  1079. index: tc.index,
  1080. offset: offset,
  1081. textList: tc.textList,
  1082. formatter: (item, category, index, opts) => {
  1083. if (typeof cfu.option[cid].tooltipFormat === 'string' && cfu.formatter[cfu.option[
  1084. cid].tooltipFormat]) {
  1085. return cfu.formatter[cfu.option[cid].tooltipFormat](item, category, index,
  1086. opts);
  1087. } else {
  1088. return this._tooltipDefault(item, category, index, opts);
  1089. }
  1090. }
  1091. });
  1092. } else {
  1093. cfu.instance[cid].showToolTip(e, {
  1094. formatter: (item, category, index, opts) => {
  1095. if (typeof cfu.option[cid].tooltipFormat === 'string' && cfu.formatter[cfu.option[
  1096. cid].tooltipFormat]) {
  1097. return cfu.formatter[cfu.option[cid].tooltipFormat](item, category, index,
  1098. opts);
  1099. } else {
  1100. return this._tooltipDefault(item, category, index, opts);
  1101. }
  1102. }
  1103. });
  1104. }
  1105. },
  1106. _tap(e, move) {
  1107. console.log(e, move, '=========前', this.inScrollView, this.inAli)
  1108. let cid = this.cid
  1109. let currentIndex = null;
  1110. let legendIndex = null;
  1111. if (this.inScrollView === true || this.inAli) {
  1112. let chartdom = uni
  1113. .createSelectorQuery()
  1114. // #ifndef MP-ALIPAY
  1115. .in(this)
  1116. .select('#ChartBoxId' + cid)
  1117. // #endif
  1118. // #ifdef MP-ALIPAY
  1119. .select('#' + this.cid)
  1120. // #endif
  1121. .boundingClientRect(data => {
  1122. e.changedTouches = [];
  1123. if (this.inAli) {
  1124. e.changedTouches.unshift({
  1125. x: e.detail.clientX - data.left,
  1126. y: e.detail.clientY - data.top
  1127. });
  1128. } else {
  1129. e.changedTouches.unshift({
  1130. x: e.detail.x - data.left,
  1131. y: e.detail.y - data.top - this.pageScrollTop
  1132. });
  1133. }
  1134. if (move) {
  1135. if (this.tooltipShow === true) {
  1136. this._showTooltip(e);
  1137. }
  1138. } else {
  1139. currentIndex = cfu.instance[cid].getCurrentDataIndex(e);
  1140. legendIndex = cfu.instance[cid].getLegendDataIndex(e);
  1141. if (this.tapLegend === true) {
  1142. cfu.instance[cid].touchLegend(e);
  1143. }
  1144. if (this.tooltipShow === true) {
  1145. this._showTooltip(e);
  1146. }
  1147. this.emitMsg({
  1148. name: 'getIndex',
  1149. params: {
  1150. type: "getIndex",
  1151. event: {
  1152. x: e.detail.x - data.left,
  1153. y: e.detail.y - data.top
  1154. },
  1155. currentIndex: currentIndex,
  1156. legendIndex: legendIndex,
  1157. id: cid,
  1158. opts: cfu.instance[cid].opts
  1159. }
  1160. });
  1161. }
  1162. console.log(e, move, '=========后2')
  1163. })
  1164. .exec();
  1165. } else {
  1166. console.log(e, move, '=========后4')
  1167. if (move) {
  1168. if (this.tooltipShow === true) {
  1169. this._showTooltip(e);
  1170. }
  1171. } else {
  1172. e.changedTouches = [];
  1173. e.changedTouches.unshift({
  1174. x: e.detail.x - e.currentTarget.offsetLeft,
  1175. y: e.detail.y - e.currentTarget.offsetTop
  1176. });
  1177. currentIndex = cfu.instance[cid].getCurrentDataIndex(e);
  1178. legendIndex = cfu.instance[cid].getLegendDataIndex(e);
  1179. if (this.tapLegend === true) {
  1180. cfu.instance[cid].touchLegend(e);
  1181. }
  1182. if (this.tooltipShow === true) {
  1183. this._showTooltip(e);
  1184. }
  1185. this.emitMsg({
  1186. name: 'getIndex',
  1187. params: {
  1188. type: "getIndex",
  1189. event: {
  1190. x: e.detail.x,
  1191. y: e.detail.y - e.currentTarget.offsetTop
  1192. },
  1193. currentIndex: currentIndex,
  1194. legendIndex: legendIndex,
  1195. id: cid,
  1196. opts: cfu.instance[cid].opts
  1197. }
  1198. });
  1199. }
  1200. }
  1201. console.log(e, move, '=========后')
  1202. },
  1203. _touchStart(e) {
  1204. let cid = this.cid
  1205. lastMoveTime = Date.now();
  1206. if (cfu.option[cid].enableScroll === true && e.touches.length == 1) {
  1207. cfu.instance[cid].scrollStart(e);
  1208. }
  1209. this.emitMsg({
  1210. name: 'getTouchStart',
  1211. params: {
  1212. type: "touchStart",
  1213. event: e.changedTouches[0],
  1214. id: cid,
  1215. opts: cfu.instance[cid].opts
  1216. }
  1217. });
  1218. },
  1219. _touchMove(e) {
  1220. let cid = this.cid
  1221. let currMoveTime = Date.now();
  1222. let duration = currMoveTime - lastMoveTime;
  1223. let touchMoveLimit = cfu.option[cid].touchMoveLimit || 24;
  1224. if (duration < Math.floor(1000 / touchMoveLimit)) return; //每秒60帧
  1225. lastMoveTime = currMoveTime;
  1226. if (cfu.option[cid].enableScroll === true && e.changedTouches.length == 1) {
  1227. cfu.instance[cid].scroll(e);
  1228. }
  1229. if (this.ontap === true && cfu.option[cid].enableScroll === false && this.onmovetip === true) {
  1230. this._tap(e, true)
  1231. }
  1232. if (this.ontouch === true && cfu.option[cid].enableScroll === true && this.onzoom === true && e
  1233. .changedTouches.length == 2) {
  1234. cfu.instance[cid].dobuleZoom(e);
  1235. }
  1236. this.emitMsg({
  1237. name: 'getTouchMove',
  1238. params: {
  1239. type: "touchMove",
  1240. event: e.changedTouches[0],
  1241. id: cid,
  1242. opts: cfu.instance[cid].opts
  1243. }
  1244. });
  1245. },
  1246. _touchEnd(e) {
  1247. let cid = this.cid
  1248. if (cfu.option[cid].enableScroll === true && e.touches.length == 0) {
  1249. cfu.instance[cid].scrollEnd(e);
  1250. }
  1251. this.emitMsg({
  1252. name: 'getTouchEnd',
  1253. params: {
  1254. type: "touchEnd",
  1255. event: e.changedTouches[0],
  1256. id: cid,
  1257. opts: cfu.instance[cid].opts
  1258. }
  1259. });
  1260. if (this.ontap === true && cfu.option[cid].enableScroll === false && this.onmovetip === true) {
  1261. this._tap(e, true)
  1262. }
  1263. },
  1264. // #endif
  1265. _error(e) {
  1266. this.mixinDatacomErrorMessage = e.detail.errMsg;
  1267. },
  1268. emitMsg(msg) {
  1269. this.$emit(msg.name, msg.params);
  1270. },
  1271. getRenderType() {
  1272. //防止如果开启echarts且父元素为v-if的情况renderjs监听不到prop变化的问题
  1273. if (this.echarts === true && this.mixinDatacomLoading === false) {
  1274. this.beforeInit()
  1275. }
  1276. },
  1277. toJSON() {
  1278. return this
  1279. }
  1280. }
  1281. };
  1282. </script>
  1283. <!-- #ifdef APP-VUE || H5 -->
  1284. <script module="rdcharts" lang="renderjs">
  1285. import uChartsRD from '../../js_sdk/u-charts/u-charts.js';
  1286. import cfu from '../../js_sdk/u-charts/config-ucharts.js';
  1287. import cfe from '../../js_sdk/u-charts/config-echarts.js';
  1288. var that = {};
  1289. var rootdom = null;
  1290. function rddeepCloneAssign(origin = {}, ...args) {
  1291. for (let i in args) {
  1292. for (let key in args[i]) {
  1293. if (args[i].hasOwnProperty(key)) {
  1294. origin[key] = args[i][key] && typeof args[i][key] === 'object' ? rddeepCloneAssign(Array.isArray(args[
  1295. i][key]) ? [] : {}, origin[key], args[i][key]) : args[i][key];
  1296. }
  1297. }
  1298. }
  1299. return origin;
  1300. }
  1301. function rdformatterAssign(args, formatter) {
  1302. for (let key in args) {
  1303. if (args.hasOwnProperty(key) && args[key] !== null && typeof args[key] === 'object') {
  1304. rdformatterAssign(args[key], formatter)
  1305. } else if (key === 'format' && typeof args[key] === 'string') {
  1306. args['formatter'] = formatter[args[key]] ? formatter[args[key]] : undefined;
  1307. }
  1308. }
  1309. return args;
  1310. }
  1311. export default {
  1312. data() {
  1313. return {
  1314. rid: null
  1315. }
  1316. },
  1317. mounted() {
  1318. rootdom = {
  1319. top: 0,
  1320. left: 0
  1321. }
  1322. // #ifdef H5
  1323. let dm = document.querySelectorAll('uni-main')[0]
  1324. if (dm === undefined) {
  1325. dm = document.querySelectorAll('uni-page-wrapper')[0]
  1326. }
  1327. rootdom = {
  1328. top: dm.offsetTop,
  1329. left: dm.offsetLeft
  1330. }
  1331. // #endif
  1332. setTimeout(() => {
  1333. if (this.rid === null) {
  1334. this.$ownerInstance && this.$ownerInstance.callMethod('getRenderType')
  1335. }
  1336. }, 200)
  1337. },
  1338. destroyed() {
  1339. delete cfu.option[this.rid]
  1340. delete cfu.instance[this.rid]
  1341. delete cfe.option[this.rid]
  1342. delete cfe.instance[this.rid]
  1343. },
  1344. methods: {
  1345. //==============以下是ECharts的方法====================
  1346. ecinit(newVal, oldVal, owner, instance) {
  1347. let cid = JSON.stringify(newVal.id)
  1348. this.rid = cid
  1349. that[cid] = this.$ownerInstance || instance
  1350. let eopts = JSON.parse(JSON.stringify(newVal))
  1351. let type = eopts.type;
  1352. //载入并覆盖默认配置
  1353. if (type && cfe.type.includes(type)) {
  1354. cfe.option[cid] = rddeepCloneAssign({}, cfe[type], eopts);
  1355. } else {
  1356. cfe.option[cid] = rddeepCloneAssign({}, eopts);
  1357. }
  1358. let newData = eopts.chartData;
  1359. if (newData) {
  1360. //挂载categories和series
  1361. if (cfe.option[cid].xAxis && cfe.option[cid].xAxis.type && cfe.option[cid].xAxis.type === 'category') {
  1362. cfe.option[cid].xAxis.data = newData.categories
  1363. }
  1364. if (cfe.option[cid].yAxis && cfe.option[cid].yAxis.type && cfe.option[cid].yAxis.type === 'category') {
  1365. cfe.option[cid].yAxis.data = newData.categories
  1366. }
  1367. cfe.option[cid].series = []
  1368. for (var i = 0; i < newData.series.length; i++) {
  1369. cfe.option[cid].seriesTemplate = cfe.option[cid].seriesTemplate ? cfe.option[cid].seriesTemplate :
  1370. {}
  1371. let Template = rddeepCloneAssign({}, cfe.option[cid].seriesTemplate, newData.series[i])
  1372. cfe.option[cid].series.push(Template)
  1373. }
  1374. }
  1375. if (typeof window.echarts === 'object') {
  1376. this.newEChart()
  1377. } else {
  1378. const script = document.createElement('script')
  1379. // #ifdef APP-VUE
  1380. script.src = './uni_modules/qiun-data-charts/static/app-plus/echarts.min.js'
  1381. // #endif
  1382. // #ifdef H5
  1383. const rooturl = window.location.origin
  1384. const directory = instance.getDataset().directory
  1385. script.src = rooturl + directory + 'uni_modules/qiun-data-charts/static/h5/echarts.min.js'
  1386. // #endif
  1387. script.onload = this.newEChart
  1388. document.head.appendChild(script)
  1389. }
  1390. },
  1391. ecresize(newVal, oldVal, owner, instance) {
  1392. if (cfe.instance[this.rid]) {
  1393. cfe.instance[this.rid].resize()
  1394. }
  1395. },
  1396. newEChart() {
  1397. let cid = this.rid
  1398. if (cfe.instance[cid] === undefined) {
  1399. cfe.instance[cid] = echarts.init(that[cid].$el.children[0])
  1400. //ontap开启后才触发click事件
  1401. if (cfe.option[cid].ontap === true) {
  1402. cfe.instance[cid].on('click', resdata => {
  1403. let event = JSON.parse(JSON.stringify({
  1404. x: resdata.event.offsetX,
  1405. y: resdata.event.offsetY
  1406. }))
  1407. that[cid].callMethod('emitMsg', {
  1408. name: "getIndex",
  1409. params: {
  1410. type: "getIndex",
  1411. event: event,
  1412. currentIndex: resdata.dataIndex,
  1413. value: resdata.data,
  1414. seriesName: resdata.seriesName,
  1415. id: cid
  1416. }
  1417. })
  1418. })
  1419. // 增加ECharts的highlight消息,实现按下移动返回索引功能。add by onefish 创建于 2021-12-11 09:50
  1420. cfe.instance[cid].on('highlight', resdata => {
  1421. that[cid].callMethod('emitMsg', {
  1422. name: "getHighlight",
  1423. params: {
  1424. type: "highlight",
  1425. res: resdata,
  1426. id: cid
  1427. }
  1428. })
  1429. })
  1430. }
  1431. this.updataEChart(cid, cfe.option[cid])
  1432. } else {
  1433. this.updataEChart(cid, cfe.option[cid])
  1434. }
  1435. },
  1436. updataEChart(cid, option) {
  1437. //替换option内format属性为formatter的预定义方法
  1438. option = rdformatterAssign(option, cfe.formatter)
  1439. if (option.tooltip) {
  1440. option.tooltip.show = option.tooltipShow ? true : false;
  1441. option.tooltip.position = this.tooltipPosition()
  1442. //tooltipFormat方法,替换组件的tooltipFormat为config-echarts.js内对应的方法
  1443. if (typeof option.tooltipFormat === 'string' && cfe.formatter[option.tooltipFormat]) {
  1444. option.tooltip.formatter = option.tooltip.formatter ? option.tooltip.formatter : cfe.formatter[
  1445. option.tooltipFormat]
  1446. }
  1447. }
  1448. // 颜色渐变添加的方法
  1449. if (option.series) {
  1450. for (let i in option.series) {
  1451. let linearGradient = option.series[i].linearGradient
  1452. if (linearGradient) {
  1453. option.series[i].color = new echarts.graphic.LinearGradient(linearGradient[0], linearGradient[
  1454. 1], linearGradient[2], linearGradient[3], linearGradient[4])
  1455. }
  1456. }
  1457. }
  1458. cfe.instance[cid].setOption(option, option.notMerge)
  1459. cfe.instance[cid].on('finished', function() {
  1460. that[cid].callMethod('emitMsg', {
  1461. name: "complete",
  1462. params: {
  1463. type: "complete",
  1464. complete: true,
  1465. id: cid
  1466. }
  1467. })
  1468. if (cfe.instance[cid]) {
  1469. cfe.instance[cid].off('finished')
  1470. }
  1471. });
  1472. //修复init初始化实例获取宽高不正确问题
  1473. if (
  1474. typeof that[cid].$el.children[0].clientWidth != 'undefined' &&
  1475. (
  1476. Math.abs(that[cid].$el.children[0].clientWidth - cfe.instance[cid].getWidth()) > 3 ||
  1477. Math.abs(that[cid].$el.children[0].clientHeight - cfe.instance[cid].getHeight()) > 3
  1478. )
  1479. ) {
  1480. this.ecresize();
  1481. }
  1482. },
  1483. tooltipPosition() {
  1484. return (point, params, dom, rect, size) => {
  1485. let x = point[0]
  1486. let y = point[1]
  1487. let viewWidth = size.viewSize[0]
  1488. let viewHeight = size.viewSize[1]
  1489. let boxWidth = size.contentSize[0]
  1490. let boxHeight = size.contentSize[1]
  1491. let posX = x + 30
  1492. let posY = y + 30
  1493. if (posX + boxWidth > viewWidth) {
  1494. posX = x - boxWidth - 30
  1495. }
  1496. if (posY + boxHeight > viewHeight) {
  1497. posY = y - boxHeight - 30
  1498. }
  1499. return [posX, posY]
  1500. }
  1501. },
  1502. //==============以下是uCharts的方法====================
  1503. ucinit(newVal, oldVal, owner, instance) {
  1504. if (JSON.stringify(newVal) == JSON.stringify(oldVal)) {
  1505. return;
  1506. }
  1507. if (!newVal.canvasId) {
  1508. return;
  1509. }
  1510. let cid = JSON.parse(JSON.stringify(newVal.canvasId))
  1511. this.rid = cid
  1512. that[cid] = this.$ownerInstance || instance
  1513. cfu.option[cid] = JSON.parse(JSON.stringify(newVal))
  1514. cfu.option[cid] = rdformatterAssign(cfu.option[cid], cfu.formatter)
  1515. let canvasdom = document.getElementById(cid)
  1516. if (canvasdom && canvasdom.children[0]) {
  1517. cfu.option[cid].context = canvasdom.children[0].getContext("2d")
  1518. if (cfu.instance[cid] && cfu.option[cid] && cfu.option[cid].update === true) {
  1519. this.updataUChart()
  1520. } else {
  1521. setTimeout(() => {
  1522. cfu.option[cid].context.restore();
  1523. cfu.option[cid].context.save();
  1524. this.newUChart()
  1525. }, 100)
  1526. }
  1527. }
  1528. },
  1529. newUChart() {
  1530. let cid = this.rid
  1531. cfu.instance[cid] = new uChartsRD(cfu.option[cid])
  1532. cfu.instance[cid].addEventListener('renderComplete', () => {
  1533. that[cid].callMethod('emitMsg', {
  1534. name: "complete",
  1535. params: {
  1536. type: "complete",
  1537. complete: true,
  1538. id: cid,
  1539. opts: cfu.instance[cid].opts
  1540. }
  1541. })
  1542. cfu.instance[cid].delEventListener('renderComplete')
  1543. });
  1544. cfu.instance[cid].addEventListener('scrollLeft', () => {
  1545. that[cid].callMethod('emitMsg', {
  1546. name: "scrollLeft",
  1547. params: {
  1548. type: "scrollLeft",
  1549. scrollLeft: true,
  1550. id: cid,
  1551. opts: cfu.instance[cid].opts
  1552. }
  1553. })
  1554. });
  1555. cfu.instance[cid].addEventListener('scrollRight', () => {
  1556. that[cid].callMethod('emitMsg', {
  1557. name: "scrollRight",
  1558. params: {
  1559. type: "scrollRight",
  1560. scrollRight: true,
  1561. id: cid,
  1562. opts: cfu.instance[cid].opts
  1563. }
  1564. })
  1565. });
  1566. },
  1567. updataUChart() {
  1568. let cid = this.rid
  1569. cfu.instance[cid].updateData(cfu.option[cid])
  1570. },
  1571. tooltipDefault(item, category, index, opts) {
  1572. if (category) {
  1573. let data = item.data
  1574. if (typeof item.data === "object") {
  1575. data = item.data.value
  1576. }
  1577. return category + ' ' + item.name + ':' + data;
  1578. } else {
  1579. if (item.properties && item.properties.name) {
  1580. return item.properties.name;
  1581. } else {
  1582. return item.name + ':' + item.data;
  1583. }
  1584. }
  1585. },
  1586. showTooltip(e, cid) {
  1587. let tc = cfu.option[cid].tooltipCustom
  1588. if (tc && tc !== undefined && tc !== null) {
  1589. let offset = undefined;
  1590. if (tc.x >= 0 && tc.y >= 0) {
  1591. offset = {
  1592. x: tc.x,
  1593. y: tc.y + 10
  1594. };
  1595. }
  1596. cfu.instance[cid].showToolTip(e, {
  1597. index: tc.index,
  1598. offset: offset,
  1599. textList: tc.textList,
  1600. formatter: (item, category, index, opts) => {
  1601. if (typeof cfu.option[cid].tooltipFormat === 'string' && cfu.formatter[cfu.option[
  1602. cid].tooltipFormat]) {
  1603. return cfu.formatter[cfu.option[cid].tooltipFormat](item, category, index,
  1604. opts);
  1605. } else {
  1606. return this.tooltipDefault(item, category, index, opts);
  1607. }
  1608. }
  1609. });
  1610. } else {
  1611. cfu.instance[cid].showToolTip(e, {
  1612. formatter: (item, category, index, opts) => {
  1613. if (typeof cfu.option[cid].tooltipFormat === 'string' && cfu.formatter[cfu.option[
  1614. cid].tooltipFormat]) {
  1615. return cfu.formatter[cfu.option[cid].tooltipFormat](item, category, index,
  1616. opts);
  1617. } else {
  1618. return this.tooltipDefault(item, category, index, opts);
  1619. }
  1620. }
  1621. });
  1622. }
  1623. },
  1624. tap(e) {
  1625. let cid = this.rid
  1626. let ontap = cfu.option[cid].ontap
  1627. let tooltipShow = cfu.option[cid].tooltipShow
  1628. let tapLegend = cfu.option[cid].tapLegend
  1629. if (ontap == false) return;
  1630. let currentIndex = null
  1631. let legendIndex = null
  1632. let rchartdom = document.getElementById('UC' + cid).getBoundingClientRect()
  1633. let tmpe = {}
  1634. if (e.detail.x) { //tap或者click的事件
  1635. tmpe = {
  1636. x: e.detail.x - rchartdom.left,
  1637. y: e.detail.y - rchartdom.top + rootdom.top
  1638. }
  1639. } else { //mouse的事件
  1640. tmpe = {
  1641. x: e.clientX - rchartdom.left,
  1642. y: e.clientY - rchartdom.top + rootdom.top
  1643. }
  1644. }
  1645. e.changedTouches = [];
  1646. e.changedTouches.unshift(tmpe)
  1647. currentIndex = cfu.instance[cid].getCurrentDataIndex(e)
  1648. legendIndex = cfu.instance[cid].getLegendDataIndex(e)
  1649. if (tapLegend === true) {
  1650. cfu.instance[cid].touchLegend(e);
  1651. }
  1652. if (tooltipShow == true) {
  1653. this.showTooltip(e, cid)
  1654. }
  1655. that[cid].callMethod('emitMsg', {
  1656. name: "getIndex",
  1657. params: {
  1658. type: "getIndex",
  1659. event: tmpe,
  1660. currentIndex: currentIndex,
  1661. legendIndex: legendIndex,
  1662. id: cid,
  1663. opts: cfu.instance[cid].opts
  1664. }
  1665. })
  1666. },
  1667. touchStart(e) {
  1668. let cid = this.rid
  1669. let ontouch = cfu.option[cid].ontouch
  1670. if (ontouch == false) return;
  1671. if (cfu.option[cid].enableScroll === true && e.touches.length == 1) {
  1672. cfu.instance[cid].scrollStart(e);
  1673. }
  1674. that[cid].callMethod('emitMsg', {
  1675. name: "getTouchStart",
  1676. params: {
  1677. type: "touchStart",
  1678. event: e.changedTouches[0],
  1679. id: cid,
  1680. opts: cfu.instance[cid].opts
  1681. }
  1682. })
  1683. },
  1684. touchMove(e) {
  1685. let cid = this.rid
  1686. let ontouch = cfu.option[cid].ontouch
  1687. if (ontouch == false) return;
  1688. if (cfu.option[cid].enableScroll === true && e.changedTouches.length == 1) {
  1689. cfu.instance[cid].scroll(e);
  1690. }
  1691. if (cfu.option[cid].ontap === true && cfu.option[cid].enableScroll === false && cfu.option[cid]
  1692. .onmovetip === true) {
  1693. let rchartdom = document.getElementById('UC' + cid).getBoundingClientRect()
  1694. let tmpe = {
  1695. x: e.changedTouches[0].clientX - rchartdom.left,
  1696. y: e.changedTouches[0].clientY - rchartdom.top + rootdom.top
  1697. }
  1698. e.changedTouches.unshift(tmpe)
  1699. if (cfu.option[cid].tooltipShow === true) {
  1700. this.showTooltip(e, cid)
  1701. }
  1702. }
  1703. if (ontouch === true && cfu.option[cid].enableScroll === true && cfu.option[cid].onzoom === true && e
  1704. .changedTouches.length == 2) {
  1705. cfu.instance[cid].dobuleZoom(e);
  1706. }
  1707. that[cid].callMethod('emitMsg', {
  1708. name: "getTouchMove",
  1709. params: {
  1710. type: "touchMove",
  1711. event: e.changedTouches[0],
  1712. id: cid,
  1713. opts: cfu.instance[cid].opts
  1714. }
  1715. })
  1716. },
  1717. touchEnd(e) {
  1718. let cid = this.rid
  1719. let ontouch = cfu.option[cid].ontouch
  1720. if (ontouch == false) return;
  1721. if (cfu.option[cid].enableScroll === true && e.touches.length == 0) {
  1722. cfu.instance[cid].scrollEnd(e);
  1723. }
  1724. that[cid].callMethod('emitMsg', {
  1725. name: "getTouchEnd",
  1726. params: {
  1727. type: "touchEnd",
  1728. event: e.changedTouches[0],
  1729. id: cid,
  1730. opts: cfu.instance[cid].opts
  1731. }
  1732. })
  1733. },
  1734. mouseDown(e) {
  1735. let cid = this.rid
  1736. let onmouse = cfu.option[cid].onmouse
  1737. if (onmouse == false) return;
  1738. let rchartdom = document.getElementById('UC' + cid).getBoundingClientRect()
  1739. let tmpe = {}
  1740. tmpe = {
  1741. x: e.clientX - rchartdom.left,
  1742. y: e.clientY - rchartdom.top + rootdom.top
  1743. }
  1744. e.changedTouches = [];
  1745. e.changedTouches.unshift(tmpe)
  1746. cfu.instance[cid].scrollStart(e)
  1747. cfu.option[cid].mousedown = true;
  1748. that[cid].callMethod('emitMsg', {
  1749. name: "getTouchStart",
  1750. params: {
  1751. type: "mouseDown",
  1752. event: tmpe,
  1753. id: cid,
  1754. opts: cfu.instance[cid].opts
  1755. }
  1756. })
  1757. },
  1758. mouseMove(e) {
  1759. let cid = this.rid
  1760. let onmouse = cfu.option[cid].onmouse
  1761. let tooltipShow = cfu.option[cid].tooltipShow
  1762. if (onmouse == false) return;
  1763. let rchartdom = document.getElementById('UC' + cid).getBoundingClientRect()
  1764. let tmpe = {}
  1765. tmpe = {
  1766. x: e.clientX - rchartdom.left,
  1767. y: e.clientY - rchartdom.top + rootdom.top
  1768. }
  1769. e.changedTouches = [];
  1770. e.changedTouches.unshift(tmpe)
  1771. if (cfu.option[cid].mousedown) {
  1772. cfu.instance[cid].scroll(e)
  1773. that[cid].callMethod('emitMsg', {
  1774. name: "getTouchMove",
  1775. params: {
  1776. type: "mouseMove",
  1777. event: tmpe,
  1778. id: cid,
  1779. opts: cfu.instance[cid].opts
  1780. }
  1781. })
  1782. } else if (cfu.instance[cid]) {
  1783. if (tooltipShow == true) {
  1784. this.showTooltip(e, cid)
  1785. }
  1786. }
  1787. },
  1788. mouseUp(e) {
  1789. let cid = this.rid
  1790. let onmouse = cfu.option[cid].onmouse
  1791. if (onmouse == false) return;
  1792. let rchartdom = document.getElementById('UC' + cid).getBoundingClientRect()
  1793. let tmpe = {}
  1794. tmpe = {
  1795. x: e.clientX - rchartdom.left,
  1796. y: e.clientY - rchartdom.top + rootdom.top
  1797. }
  1798. e.changedTouches = [];
  1799. e.changedTouches.unshift(tmpe)
  1800. cfu.instance[cid].scrollEnd(e)
  1801. cfu.option[cid].mousedown = false;
  1802. that[cid].callMethod('emitMsg', {
  1803. name: "getTouchEnd",
  1804. params: {
  1805. type: "mouseUp",
  1806. event: tmpe,
  1807. id: cid,
  1808. opts: cfu.instance[cid].opts
  1809. }
  1810. })
  1811. },
  1812. }
  1813. }
  1814. </script>
  1815. <!-- #endif -->
  1816. <style scoped>
  1817. .chartsview {
  1818. width: 100%;
  1819. height: 100%;
  1820. display: flex;
  1821. flex: 1;
  1822. justify-content: center;
  1823. align-items: center;
  1824. }
  1825. </style>