qiun-data-charts.vue 50 KB

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