qiun-data-charts.vue 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  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
  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 || MP-KUAISHOU || MP-LARK || MP-JD -->
  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 uCharts 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.hasOwnProperty(key) && 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: 'rgba(0,0,0,0)'
  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. optsWatch: {
  304. type: Boolean,
  305. default: true
  306. },
  307. onzoom: {
  308. type: Boolean,
  309. default: false
  310. },
  311. ontap: {
  312. type: Boolean,
  313. default: true
  314. },
  315. ontouch: {
  316. type: Boolean,
  317. default: false
  318. },
  319. onmouse: {
  320. type: Boolean,
  321. default: true
  322. },
  323. onmovetip: {
  324. type: Boolean,
  325. default: false
  326. },
  327. echartsH5: {
  328. type: Boolean,
  329. default: false
  330. },
  331. echartsApp: {
  332. type: Boolean,
  333. default: false
  334. },
  335. tooltipShow: {
  336. type: Boolean,
  337. default: true
  338. },
  339. tooltipFormat: {
  340. type: String,
  341. default: undefined
  342. },
  343. tooltipCustom: {
  344. type: Object,
  345. default: undefined
  346. },
  347. startDate: {
  348. type: String,
  349. default: undefined
  350. },
  351. endDate: {
  352. type: String,
  353. default: undefined
  354. },
  355. textEnum: {
  356. type: Array,
  357. default () {
  358. return []
  359. }
  360. },
  361. groupEnum: {
  362. type: Array,
  363. default () {
  364. return []
  365. }
  366. },
  367. pageScrollTop: {
  368. type: Number,
  369. default: 0
  370. },
  371. directory: {
  372. type: String,
  373. default: '/'
  374. },
  375. tapLegend: {
  376. type: Boolean,
  377. default: true
  378. },
  379. menus: {
  380. type: Array,
  381. default () {
  382. return []
  383. }
  384. }
  385. },
  386. data() {
  387. return {
  388. cid: 'uchartsid',
  389. inWx: false,
  390. inAli: false,
  391. inTt: false,
  392. inBd: false,
  393. inH5: false,
  394. inApp: false,
  395. inWin: false,
  396. type2d: true,
  397. disScroll: false,
  398. openmouse: false,
  399. pixel: 1,
  400. cWidth: 375,
  401. cHeight: 250,
  402. showchart: false,
  403. echarts: false,
  404. echartsResize:false,
  405. uchartsOpts: {},
  406. echartsOpts: {},
  407. drawData:{},
  408. lastDrawTime:null,
  409. };
  410. },
  411. created(){
  412. this.cid = this.canvasId
  413. if (this.canvasId == 'uchartsid' || this.canvasId == '') {
  414. let t = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  415. let len = t.length
  416. let id = ''
  417. for (let i = 0; i < 32; i++) {
  418. id += t.charAt(Math.floor(Math.random() * len))
  419. }
  420. this.cid = id
  421. }
  422. const systemInfo = uni.getSystemInfoSync()
  423. if(systemInfo.platform === 'windows' || systemInfo.platform === 'mac'){
  424. this.inWin = true;
  425. }
  426. // #ifdef MP-WEIXIN
  427. this.inWx = true;
  428. if (this.canvas2d === false || systemInfo.platform === 'windows' || systemInfo.platform === 'mac') {
  429. this.type2d = false;
  430. }else{
  431. this.type2d = true;
  432. this.pixel = systemInfo.pixelRatio;
  433. }
  434. // #endif
  435. //非微信小程序端强制关闭canvas2d模式
  436. // #ifndef MP-WEIXIN
  437. this.type2d = false;
  438. // #endif
  439. // #ifdef MP-TOUTIAO || MP-LARK || MP-ALIPAY
  440. this.type2d = this.canvas2d;
  441. // #endif
  442. // #ifdef MP-ALIPAY
  443. this.inAli = true;
  444. this.pixel = systemInfo.pixelRatio;
  445. // #endif
  446. // #ifdef MP-BAIDU
  447. this.inBd = true;
  448. // #endif
  449. // #ifdef MP-TOUTIAO
  450. this.inTt = true;
  451. // #endif
  452. this.disScroll = this.disableScroll;
  453. },
  454. mounted() {
  455. // #ifdef APP-VUE
  456. this.inApp = true;
  457. if (this.echartsApp === true) {
  458. this.echarts = true;
  459. this.openmouse = false;
  460. }
  461. // #endif
  462. // #ifdef APP-NVUE
  463. this.inApp = true;
  464. this.mixinDatacomLoading = false
  465. this.mixinDatacomErrorMessage = "暂不支持NVUE"
  466. // #endif
  467. // #ifdef H5
  468. this.inH5 = true;
  469. if(this.inWin === true){
  470. this.openmouse = this.onmouse;
  471. }
  472. if (this.echartsH5 === true) {
  473. this.echarts = true;
  474. }
  475. // #endif
  476. this.$nextTick(()=>{
  477. this.beforeInit();
  478. })
  479. // #ifndef MP-ALIPAY || MP-BAIDU || MP-TOUTIAO || APP-VUE
  480. const time = this.inH5 ? 500 : 200;
  481. const _this = this;
  482. uni.onWindowResize(
  483. debounce(function(res) {
  484. if (_this.mixinDatacomLoading == true) {
  485. return;
  486. }
  487. let errmsg = _this.mixinDatacomErrorMessage;
  488. if (errmsg !== null && errmsg !== 'null' && errmsg !== '') {
  489. return;
  490. }
  491. if (_this.echarts) {
  492. _this.echartsResize = !_this.echartsResize;
  493. } else {
  494. _this.resizeHandler();
  495. }
  496. }, time)
  497. );
  498. // #endif
  499. },
  500. destroyed(){
  501. if(this.echarts === true){
  502. delete cfe.option[this.cid]
  503. delete cfe.instance[this.cid]
  504. }else{
  505. delete cfu.option[this.cid]
  506. delete cfu.instance[this.cid]
  507. }
  508. // #ifndef MP-ALIPAY || MP-BAIDU || MP-TOUTIAO
  509. uni.offWindowResize(()=>{})
  510. // #endif
  511. },
  512. watch: {
  513. chartDataProps: {
  514. handler(val, oldval) {
  515. if (typeof val === 'object') {
  516. if (JSON.stringify(val) !== JSON.stringify(oldval)) {
  517. this._clearChart();
  518. if (val.series && val.series.length > 0) {
  519. this.beforeInit();
  520. }else{
  521. this.mixinDatacomLoading = true;
  522. this.showchart = false;
  523. this.mixinDatacomErrorMessage = null;
  524. }
  525. }
  526. } else {
  527. this.mixinDatacomLoading = false;
  528. this._clearChart();
  529. this.showchart = false;
  530. this.mixinDatacomErrorMessage = '参数错误:chartData数据类型错误';
  531. }
  532. },
  533. immediate: false,
  534. deep: true
  535. },
  536. localdata:{
  537. handler(val, oldval) {
  538. if (JSON.stringify(val) !== JSON.stringify(oldval)) {
  539. if (val.length > 0) {
  540. this.beforeInit();
  541. }else{
  542. this.mixinDatacomLoading = true;
  543. this._clearChart();
  544. this.showchart = false;
  545. this.mixinDatacomErrorMessage = null;
  546. }
  547. }
  548. },
  549. immediate: false,
  550. deep: true
  551. },
  552. optsProps: {
  553. handler(val, oldval) {
  554. if (typeof val === 'object') {
  555. if (JSON.stringify(val) !== JSON.stringify(oldval) && this.echarts === false && this.optsWatch == true) {
  556. this.checkData(this.drawData);
  557. }
  558. } else {
  559. this.mixinDatacomLoading = false;
  560. this._clearChart();
  561. this.showchart = false;
  562. this.mixinDatacomErrorMessage = '参数错误:opts数据类型错误';
  563. }
  564. },
  565. immediate: false,
  566. deep: true
  567. },
  568. eoptsProps: {
  569. handler(val, oldval) {
  570. if (typeof val === 'object') {
  571. if (JSON.stringify(val) !== JSON.stringify(oldval) && this.echarts === true) {
  572. this.checkData(this.drawData);
  573. }
  574. } else {
  575. this.mixinDatacomLoading = false;
  576. this.showchart = false;
  577. this.mixinDatacomErrorMessage = '参数错误:eopts数据类型错误';
  578. }
  579. },
  580. immediate: false,
  581. deep: true
  582. },
  583. reshow(val, oldval) {
  584. if (val === true && this.mixinDatacomLoading === false) {
  585. setTimeout(() => {
  586. this.mixinDatacomErrorMessage = null;
  587. this.echartsResize = !this.echartsResize;
  588. this.checkData(this.drawData);
  589. }, 200);
  590. }
  591. },
  592. reload(val, oldval) {
  593. if (val === true) {
  594. this.showchart = false;
  595. this.mixinDatacomErrorMessage = null;
  596. this.reloading();
  597. }
  598. },
  599. mixinDatacomErrorMessage(val, oldval) {
  600. if (val) {
  601. this.emitMsg({name: 'error', params: {type:"error", errorShow: this.errorShow, msg: val, id: this.cid}});
  602. if(this.errorShow){
  603. console.log('[秋云图表组件]' + val);
  604. }
  605. }
  606. },
  607. errorMessage(val, oldval) {
  608. if (val && this.errorShow && val !== null && val !== 'null' && val !== '') {
  609. this.showchart = false;
  610. this.mixinDatacomLoading = false;
  611. this.mixinDatacomErrorMessage = val;
  612. } else {
  613. this.showchart = false;
  614. this.mixinDatacomErrorMessage = null;
  615. this.reloading();
  616. }
  617. }
  618. },
  619. computed: {
  620. optsProps() {
  621. return JSON.parse(JSON.stringify(this.opts));
  622. },
  623. eoptsProps() {
  624. return JSON.parse(JSON.stringify(this.eopts));
  625. },
  626. chartDataProps() {
  627. return JSON.parse(JSON.stringify(this.chartData));
  628. },
  629. },
  630. methods: {
  631. beforeInit(){
  632. this.mixinDatacomErrorMessage = null;
  633. if (typeof this.chartData === 'object' && this.chartData != null && this.chartData.series !== undefined && this.chartData.series.length > 0) {
  634. //拷贝一下chartData,为了opts变更后统一数据来源
  635. this.drawData = deepCloneAssign({}, this.chartData);
  636. this.mixinDatacomLoading = false;
  637. this.showchart = true;
  638. this.checkData(this.chartData);
  639. }else if(this.localdata.length>0){
  640. this.mixinDatacomLoading = false;
  641. this.showchart = true;
  642. this.localdataInit(this.localdata);
  643. }else if(this.collection !== ''){
  644. this.mixinDatacomLoading = false;
  645. this.getCloudData();
  646. }else{
  647. this.mixinDatacomLoading = true;
  648. }
  649. },
  650. localdataInit(resdata){
  651. //替换enum类型为正确的描述
  652. if(this.groupEnum.length>0){
  653. for (let i = 0; i < resdata.length; i++) {
  654. for (let j = 0; j < this.groupEnum.length; j++) {
  655. if(resdata[i].group === this.groupEnum[j].value){
  656. resdata[i].group = this.groupEnum[j].text
  657. }
  658. }
  659. }
  660. }
  661. if(this.textEnum.length>0){
  662. for (let i = 0; i < resdata.length; i++) {
  663. for (let j = 0; j < this.textEnum.length; j++) {
  664. if(resdata[i].text === this.textEnum[j].value){
  665. resdata[i].text = this.textEnum[j].text
  666. }
  667. }
  668. }
  669. }
  670. let needCategories = false;
  671. let tmpData = {categories:[], series:[]}
  672. let tmpcategories = []
  673. let tmpseries = [];
  674. //拼接categories
  675. if(this.echarts === true){
  676. needCategories = cfe.categories.includes(this.type)
  677. }else{
  678. needCategories = cfu.categories.includes(this.type)
  679. }
  680. if(needCategories === true){
  681. //如果props中的chartData带有categories,则优先使用chartData的categories
  682. if(this.chartData && this.chartData.categories && this.chartData.categories.length>0){
  683. tmpcategories = this.chartData.categories
  684. }else{
  685. //如果是日期类型的数据,不管是本地数据还是云数据,都按起止日期自动拼接categories
  686. if(this.startDate && this.endDate){
  687. let idate = new Date(this.startDate)
  688. let edate = new Date(this.endDate)
  689. while (idate <= edate) {
  690. tmpcategories.push(getFormatDate(idate))
  691. idate = idate.setDate(idate.getDate() + 1)
  692. idate = new Date(idate)
  693. }
  694. //否则从结果中去重并拼接categories
  695. }else{
  696. let tempckey = {};
  697. resdata.map(function(item, index) {
  698. if (item.text != undefined && !tempckey[item.text]) {
  699. tmpcategories.push(item.text)
  700. tempckey[item.text] = true
  701. }
  702. });
  703. }
  704. }
  705. tmpData.categories = tmpcategories
  706. }
  707. //拼接series
  708. let tempskey = {};
  709. resdata.map(function(item, index) {
  710. if (item.group != undefined && !tempskey[item.group]) {
  711. tmpseries.push({ name: item.group, data: [] });
  712. tempskey[item.group] = true;
  713. }
  714. });
  715. //如果没有获取到分组名称(可能是带categories的数据,也可能是不带的饼图类)
  716. if (tmpseries.length == 0) {
  717. tmpseries = [{ name: '默认分组', data: [] }];
  718. //如果是需要categories的图表类型
  719. if(needCategories === true){
  720. for (let j = 0; j < tmpcategories.length; j++) {
  721. let seriesdata = 0;
  722. for (let i = 0; i < resdata.length; i++) {
  723. if (resdata[i].text == tmpcategories[j]) {
  724. seriesdata = resdata[i].value;
  725. }
  726. }
  727. tmpseries[0].data.push(seriesdata);
  728. }
  729. //如果是饼图类的图表类型
  730. }else{
  731. for (let i = 0; i < resdata.length; i++) {
  732. tmpseries[0].data.push({"name": resdata[i].text,"value": resdata[i].value});
  733. }
  734. }
  735. //如果有分组名
  736. } else {
  737. for (let k = 0; k < tmpseries.length; k++) {
  738. //如果有categories
  739. if (tmpcategories.length > 0) {
  740. for (let j = 0; j < tmpcategories.length; j++) {
  741. let seriesdata = 0;
  742. for (let i = 0; i < resdata.length; i++) {
  743. if (tmpseries[k].name == resdata[i].group && resdata[i].text == tmpcategories[j]) {
  744. seriesdata = resdata[i].value;
  745. }
  746. }
  747. tmpseries[k].data.push(seriesdata);
  748. }
  749. //如果传了group而没有传text,即没有categories(正常情况下这种数据是不符合数据要求规范的)
  750. } else {
  751. for (let i = 0; i < resdata.length; i++) {
  752. if (tmpseries[k].name == resdata[i].group) {
  753. tmpseries[k].data.push(resdata[i].value);
  754. }
  755. }
  756. }
  757. }
  758. }
  759. tmpData.series = tmpseries
  760. //拷贝一下chartData,为了opts变更后统一数据来源
  761. this.drawData = deepCloneAssign({}, tmpData);
  762. this.checkData(tmpData)
  763. },
  764. reloading() {
  765. if(this.errorReload === false){
  766. return;
  767. }
  768. this.showchart = false;
  769. this.mixinDatacomErrorMessage = null;
  770. if (this.collection !== '') {
  771. this.mixinDatacomLoading = false;
  772. this.onMixinDatacomPropsChange(true);
  773. } else {
  774. this.beforeInit();
  775. }
  776. },
  777. checkData(anyData) {
  778. let cid = this.cid
  779. //复位opts或eopts
  780. if(this.echarts === true){
  781. cfe.option[cid] = deepCloneAssign({}, this.eopts);
  782. cfe.option[cid].id = cid;
  783. cfe.option[cid].type = this.type;
  784. }else{
  785. if (this.type && cfu.type.includes(this.type)) {
  786. cfu.option[cid] = deepCloneAssign({}, cfu[this.type], this.opts);
  787. cfu.option[cid].canvasId = cid;
  788. } else {
  789. this.mixinDatacomLoading = false;
  790. this.showchart = false;
  791. this.mixinDatacomErrorMessage = '参数错误:props参数中type类型不正确';
  792. }
  793. }
  794. //挂载categories和series
  795. let newData = deepCloneAssign({}, anyData);
  796. if (newData.series !== undefined && newData.series.length > 0) {
  797. this.mixinDatacomErrorMessage = null;
  798. if (this.echarts === true) {
  799. cfe.option[cid].chartData = newData;
  800. this.$nextTick(()=>{
  801. this.init()
  802. })
  803. }else{
  804. cfu.option[cid].categories = newData.categories;
  805. cfu.option[cid].series = newData.series;
  806. this.$nextTick(()=>{
  807. this.init()
  808. })
  809. }
  810. }
  811. },
  812. resizeHandler() {
  813. //渲染防抖
  814. let currTime = Date.now();
  815. let lastDrawTime = this.lastDrawTime?this.lastDrawTime:currTime-3000;
  816. let duration = currTime - lastDrawTime;
  817. if (duration < 1000) return;
  818. let chartdom = uni
  819. .createSelectorQuery()
  820. // #ifndef MP-ALIPAY
  821. .in(this)
  822. // #endif
  823. .select('#ChartBoxId'+this.cid)
  824. .boundingClientRect(data => {
  825. this.showchart = true;
  826. if (data.width > 0 && data.height > 0) {
  827. if (data.width !== this.cWidth || data.height !== this.cHeight) {
  828. this.checkData(this.drawData)
  829. }
  830. }
  831. })
  832. .exec();
  833. },
  834. getCloudData() {
  835. if (this.mixinDatacomLoading == true) {
  836. return;
  837. }
  838. this.mixinDatacomLoading = true;
  839. this.mixinDatacomGet()
  840. .then(res => {
  841. this.mixinDatacomResData = res.result.data;
  842. this.localdataInit(this.mixinDatacomResData);
  843. })
  844. .catch(err => {
  845. this.mixinDatacomLoading = false;
  846. this.showchart = false;
  847. this.mixinDatacomErrorMessage = '请求错误:' + err;
  848. });
  849. },
  850. onMixinDatacomPropsChange(needReset, changed) {
  851. if (needReset == true && this.collection !== '') {
  852. this.showchart = false;
  853. this.mixinDatacomErrorMessage = null;
  854. this._clearChart();
  855. this.getCloudData();
  856. }
  857. },
  858. _clearChart() {
  859. let cid = this.cid
  860. if (this.echrts !== true && cfu.option[cid] && cfu.option[cid].context) {
  861. const ctx = cfu.option[cid].context;
  862. if(typeof ctx === "object" && !cfu.option[cid].update){
  863. ctx.clearRect(0, 0, this.cWidth, this.cHeight);
  864. ctx.draw();
  865. }
  866. }
  867. },
  868. init() {
  869. let cid = this.cid
  870. let chartdom = uni
  871. .createSelectorQuery()
  872. // #ifndef MP-ALIPAY
  873. .in(this)
  874. // #endif
  875. .select('#ChartBoxId'+cid)
  876. .boundingClientRect(data => {
  877. if (data.width > 0 && data.height > 0) {
  878. this.mixinDatacomLoading = false;
  879. this.showchart = true;
  880. this.lastDrawTime = Date.now();
  881. this.cWidth = data.width;
  882. this.cHeight = data.height;
  883. if(this.echarts !== true){
  884. cfu.option[cid].background = this.background == 'rgba(0,0,0,0)' ? '#FFFFFF' : this.background;
  885. cfu.option[cid].canvas2d = this.type2d;
  886. cfu.option[cid].pixelRatio = this.pixel;
  887. cfu.option[cid].animation = this.animation;
  888. cfu.option[cid].width = data.width * this.pixel;
  889. cfu.option[cid].height = data.height * this.pixel;
  890. cfu.option[cid].onzoom = this.onzoom;
  891. cfu.option[cid].ontap = this.ontap;
  892. cfu.option[cid].ontouch = this.ontouch;
  893. cfu.option[cid].onmouse = this.openmouse;
  894. cfu.option[cid].onmovetip = this.onmovetip;
  895. cfu.option[cid].tooltipShow = this.tooltipShow;
  896. cfu.option[cid].tooltipFormat = this.tooltipFormat;
  897. cfu.option[cid].tooltipCustom = this.tooltipCustom;
  898. cfu.option[cid].inScrollView = this.inScrollView;
  899. cfu.option[cid].lastDrawTime = this.lastDrawTime;
  900. cfu.option[cid].tapLegend = this.tapLegend;
  901. }
  902. //如果是H5或者App端,采用renderjs渲染图表
  903. if (this.inH5 || this.inApp) {
  904. if (this.echarts == true) {
  905. cfe.option[cid].ontap = this.ontap;
  906. cfe.option[cid].onmouse = this.openmouse;
  907. cfe.option[cid].tooltipShow = this.tooltipShow;
  908. cfe.option[cid].tooltipFormat = this.tooltipFormat;
  909. cfe.option[cid].tooltipCustom = this.tooltipCustom;
  910. cfe.option[cid].lastDrawTime = this.lastDrawTime;
  911. this.echartsOpts = deepCloneAssign({}, cfe.option[cid]);
  912. } else {
  913. cfu.option[cid].rotateLock = cfu.option[cid].rotate;
  914. this.uchartsOpts = deepCloneAssign({}, cfu.option[cid]);
  915. }
  916. //如果是小程序端,采用uCharts渲染
  917. } else {
  918. cfu.option[cid] = formatterAssign(cfu.option[cid],cfu.formatter)
  919. this.mixinDatacomErrorMessage = null;
  920. this.mixinDatacomLoading = false;
  921. this.showchart = true;
  922. this.$nextTick(()=>{
  923. if (this.type2d === true) {
  924. const query = uni.createSelectorQuery().in(this)
  925. query
  926. .select('#' + cid)
  927. .fields({ node: true, size: true })
  928. .exec(res => {
  929. if (res[0]) {
  930. const canvas = res[0].node;
  931. const ctx = canvas.getContext('2d');
  932. cfu.option[cid].context = ctx;
  933. cfu.option[cid].rotateLock = cfu.option[cid].rotate;
  934. if(cfu.instance[cid] && cfu.option[cid] && cfu.option[cid].update === true){
  935. this._updataUChart(cid)
  936. }else{
  937. canvas.width = data.width * this.pixel;
  938. canvas.height = data.height * this.pixel;
  939. canvas._width = data.width * this.pixel;
  940. canvas._height = data.height * this.pixel;
  941. setTimeout(()=>{
  942. cfu.option[cid].context.restore();
  943. cfu.option[cid].context.save();
  944. this._newChart(cid)
  945. },100)
  946. }
  947. } else {
  948. this.showchart = false;
  949. this.mixinDatacomErrorMessage = '参数错误:开启2d模式后,未获取到dom节点,canvas-id:' + cid;
  950. }
  951. });
  952. } else {
  953. if(this.inAli){
  954. cfu.option[cid].rotateLock = cfu.option[cid].rotate;
  955. }
  956. cfu.option[cid].context = uni.createCanvasContext(cid, this);
  957. if(cfu.instance[cid] && cfu.option[cid] && cfu.option[cid].update === true){
  958. this._updataUChart(cid)
  959. }else{
  960. setTimeout(()=>{
  961. cfu.option[cid].context.restore();
  962. cfu.option[cid].context.save();
  963. this._newChart(cid)
  964. },100)
  965. }
  966. }
  967. })
  968. }
  969. } else {
  970. this.mixinDatacomLoading = false;
  971. this.showchart = false;
  972. if (this.reshow == true) {
  973. this.mixinDatacomErrorMessage = '布局错误:未获取到父元素宽高尺寸!canvas-id:' + cid;
  974. }
  975. }
  976. })
  977. .exec();
  978. },
  979. saveImage(){
  980. uni.canvasToTempFilePath({
  981. canvasId: this.cid,
  982. success: res=>{
  983. //#ifdef H5
  984. var a = document.createElement("a");
  985. a.href = res.tempFilePath;
  986. a.download = this.cid;
  987. a.target = '_blank'
  988. a.click();
  989. //#endif
  990. //#ifndef H5
  991. uni.saveImageToPhotosAlbum({
  992. filePath: res.tempFilePath,
  993. success: function () {
  994. uni.showToast({
  995. title: '保存成功',
  996. duration: 2000
  997. });
  998. }
  999. });
  1000. //#endif
  1001. }
  1002. },this);
  1003. },
  1004. getImage(){
  1005. if(this.type2d == false){
  1006. uni.canvasToTempFilePath({
  1007. canvasId: this.cid,
  1008. success: res=>{
  1009. this.emitMsg({name: 'getImage', params: {type:"getImage", base64: res.tempFilePath}});
  1010. }
  1011. },this);
  1012. }else{
  1013. const query = uni.createSelectorQuery().in(this)
  1014. query
  1015. .select('#' + this.cid)
  1016. .fields({ node: true, size: true })
  1017. .exec(res => {
  1018. if (res[0]) {
  1019. const canvas = res[0].node;
  1020. this.emitMsg({name: 'getImage', params: {type:"getImage", base64: canvas.toDataURL('image/png')}});
  1021. }
  1022. });
  1023. }
  1024. },
  1025. // #ifndef APP-VUE || H5
  1026. _newChart(cid) {
  1027. if (this.mixinDatacomLoading == true) {
  1028. return;
  1029. }
  1030. this.showchart = true;
  1031. cfu.instance[cid] = new uCharts(cfu.option[cid]);
  1032. cfu.instance[cid].addEventListener('renderComplete', () => {
  1033. this.emitMsg({name: 'complete', params: {type:"complete", complete: true, id: cid}});
  1034. cfu.instance[cid].delEventListener('renderComplete')
  1035. });
  1036. cfu.instance[cid].addEventListener('scrollLeft', () => {
  1037. this.emitMsg({name: 'scrollLeft', params: {type:"scrollLeft", scrollLeft: true, id: cid}});
  1038. });
  1039. cfu.instance[cid].addEventListener('scrollRight', () => {
  1040. this.emitMsg({name: 'scrollRight', params: {type:"scrollRight", scrollRight: true, id: cid}});
  1041. });
  1042. },
  1043. _updataUChart(cid) {
  1044. cfu.instance[cid].updateData(cfu.option[cid])
  1045. },
  1046. _tooltipDefault(item, category, index, opts) {
  1047. if (category) {
  1048. let data = item.data
  1049. if(typeof item.data === "object"){
  1050. data = item.data.value
  1051. }
  1052. return category + ' ' + item.name + ':' + data;
  1053. } else {
  1054. if (item.properties && item.properties.name) {
  1055. return item.properties.name;
  1056. } else {
  1057. return item.name + ':' + item.data;
  1058. }
  1059. }
  1060. },
  1061. _showTooltip(e) {
  1062. let cid = this.cid
  1063. let tc = cfu.option[cid].tooltipCustom
  1064. if (tc && tc !== undefined && tc !== null) {
  1065. let offset = undefined;
  1066. if (tc.x >= 0 && tc.y >= 0) {
  1067. offset = { x: tc.x, y: tc.y + 10 };
  1068. }
  1069. cfu.instance[cid].showToolTip(e, {
  1070. index: tc.index,
  1071. offset: offset,
  1072. textList: tc.textList,
  1073. formatter: (item, category, index, opts) => {
  1074. if (typeof cfu.option[cid].tooltipFormat === 'string' && cfu.formatter[cfu.option[cid].tooltipFormat]) {
  1075. return cfu.formatter[cfu.option[cid].tooltipFormat](item, category, index, opts);
  1076. } else {
  1077. return this._tooltipDefault(item, category, index, opts);
  1078. }
  1079. }
  1080. });
  1081. } else {
  1082. cfu.instance[cid].showToolTip(e, {
  1083. formatter: (item, category, index, opts) => {
  1084. if (typeof cfu.option[cid].tooltipFormat === 'string' && cfu.formatter[cfu.option[cid].tooltipFormat]) {
  1085. return cfu.formatter[cfu.option[cid].tooltipFormat](item, category, index, opts);
  1086. } else {
  1087. return this._tooltipDefault(item, category, index, opts);
  1088. }
  1089. }
  1090. });
  1091. }
  1092. },
  1093. _tap(e,move) {
  1094. let cid = this.cid
  1095. let currentIndex = null;
  1096. let legendIndex = null;
  1097. if (this.inScrollView === true || this.inAli) {
  1098. let chartdom = uni
  1099. .createSelectorQuery()
  1100. // #ifndef MP-ALIPAY
  1101. .in(this)
  1102. .select('#ChartBoxId'+cid)
  1103. // #endif
  1104. // #ifdef MP-ALIPAY
  1105. .select('#'+this.cid)
  1106. // #endif
  1107. .boundingClientRect(data => {
  1108. e.changedTouches=[];
  1109. if (this.inAli) {
  1110. e.changedTouches.unshift({ x: e.detail.clientX - data.left, y: e.detail.clientY - data.top});
  1111. }else{
  1112. e.changedTouches.unshift({ x: e.detail.x - data.left, y: e.detail.y - data.top - this.pageScrollTop});
  1113. }
  1114. if(move){
  1115. if (this.tooltipShow === true) {
  1116. this._showTooltip(e);
  1117. }
  1118. }else{
  1119. currentIndex = cfu.instance[cid].getCurrentDataIndex(e);
  1120. legendIndex = cfu.instance[cid].getLegendDataIndex(e);
  1121. if(this.tapLegend === true){
  1122. cfu.instance[cid].touchLegend(e);
  1123. }
  1124. if (this.tooltipShow === true) {
  1125. this._showTooltip(e);
  1126. }
  1127. 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}});
  1128. }
  1129. })
  1130. .exec();
  1131. } else {
  1132. if(move){
  1133. if (this.tooltipShow === true) {
  1134. this._showTooltip(e);
  1135. }
  1136. }else{
  1137. e.changedTouches=[];
  1138. e.changedTouches.unshift({ x: e.detail.x - e.currentTarget.offsetLeft, y: e.detail.y - e.currentTarget.offsetTop });
  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({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}});
  1148. }
  1149. }
  1150. },
  1151. _touchStart(e) {
  1152. let cid = this.cid
  1153. lastMoveTime=Date.now();
  1154. if(cfu.option[cid].enableScroll === true && e.touches.length == 1){
  1155. cfu.instance[cid].scrollStart(e);
  1156. }
  1157. this.emitMsg({name:'getTouchStart', params:{type:"touchStart", event:e.changedTouches[0], id:cid}});
  1158. },
  1159. _touchMove(e) {
  1160. let cid = this.cid
  1161. let currMoveTime = Date.now();
  1162. let duration = currMoveTime - lastMoveTime;
  1163. let touchMoveLimit = cfu.option[cid].touchMoveLimit || 24;
  1164. if (duration < Math.floor(1000 / touchMoveLimit)) return;//每秒60帧
  1165. lastMoveTime = currMoveTime;
  1166. if(cfu.option[cid].enableScroll === true && e.changedTouches.length == 1){
  1167. cfu.instance[cid].scroll(e);
  1168. }
  1169. if(this.ontap === true && cfu.option[cid].enableScroll === false && this.onmovetip === true){
  1170. this._tap(e,true)
  1171. }
  1172. if(this.ontouch === true && cfu.option[cid].enableScroll === true && this.onzoom === true && e.changedTouches.length == 2){
  1173. cfu.instance[cid].dobuleZoom(e);
  1174. }
  1175. this.emitMsg({name: 'getTouchMove', params: {type:"touchMove", event:e.changedTouches[0], id: cid}});
  1176. },
  1177. _touchEnd(e) {
  1178. let cid = this.cid
  1179. if(cfu.option[cid].enableScroll === true && e.touches.length == 0){
  1180. cfu.instance[cid].scrollEnd(e);
  1181. }
  1182. this.emitMsg({name:'getTouchEnd', params:{type:"touchEnd", event:e.changedTouches[0], id:cid}});
  1183. if(this.ontap === true && cfu.option[cid].enableScroll === false && this.onmovetip === true){
  1184. this._tap(e,true)
  1185. }
  1186. },
  1187. // #endif
  1188. _error(e) {
  1189. this.mixinDatacomErrorMessage = e.detail.errMsg;
  1190. },
  1191. emitMsg(msg) {
  1192. this.$emit(msg.name, msg.params);
  1193. },
  1194. getRenderType() {
  1195. //防止如果开启echarts且父元素为v-if的情况renderjs监听不到prop变化的问题
  1196. if(this.echarts===true && this.mixinDatacomLoading===false){
  1197. this.beforeInit()
  1198. }
  1199. },
  1200. toJSON(){
  1201. return this
  1202. }
  1203. }
  1204. };
  1205. </script>
  1206. <!-- #ifdef APP-VUE || H5 -->
  1207. <script module="rdcharts" lang="renderjs">
  1208. import uChartsRD from '../../js_sdk/u-charts/u-charts.js';
  1209. import cfu from '../../js_sdk/u-charts/config-ucharts.js';
  1210. import cfe from '../../js_sdk/u-charts/config-echarts.js';
  1211. var that = {};
  1212. var rootdom = null;
  1213. function rddeepCloneAssign(origin = {}, ...args) {
  1214. for (let i in args) {
  1215. for (let key in args[i]) {
  1216. if (args[i].hasOwnProperty(key)) {
  1217. origin[key] = args[i][key] && typeof args[i][key] === 'object' ? rddeepCloneAssign(Array.isArray(args[i][key]) ? [] : {}, origin[key], args[i][key]) : args[i][key];
  1218. }
  1219. }
  1220. }
  1221. return origin;
  1222. }
  1223. function rdformatterAssign(args,formatter) {
  1224. for (let key in args) {
  1225. if(args.hasOwnProperty(key) && args[key] !== null && typeof args[key] === 'object'){
  1226. rdformatterAssign(args[key],formatter)
  1227. }else if(key === 'format' && typeof args[key] === 'string'){
  1228. args['formatter'] = formatter[args[key]] ? formatter[args[key]] : undefined;
  1229. }
  1230. }
  1231. return args;
  1232. }
  1233. export default {
  1234. data() {
  1235. return {
  1236. rid:null
  1237. }
  1238. },
  1239. mounted() {
  1240. rootdom = {top:0,left:0}
  1241. // #ifdef H5
  1242. let dm = document.querySelectorAll('uni-main')[0]
  1243. if(dm === undefined){
  1244. dm = document.querySelectorAll('uni-page-wrapper')[0]
  1245. }
  1246. rootdom = {top:dm.offsetTop,left:dm.offsetLeft}
  1247. // #endif
  1248. setTimeout(()=>{
  1249. if(this.rid === null){
  1250. this.$ownerInstance && this.$ownerInstance.callMethod('getRenderType')
  1251. }
  1252. },200)
  1253. },
  1254. destroyed(){
  1255. delete cfu.option[this.rid]
  1256. delete cfu.instance[this.rid]
  1257. delete cfe.option[this.rid]
  1258. delete cfe.instance[this.rid]
  1259. },
  1260. methods: {
  1261. //==============以下是ECharts的方法====================
  1262. ecinit(newVal, oldVal, owner, instance){
  1263. let cid = JSON.stringify(newVal.id)
  1264. this.rid = cid
  1265. that[cid] = this.$ownerInstance || instance
  1266. let eopts = JSON.parse(JSON.stringify(newVal))
  1267. let type = eopts.type;
  1268. //载入并覆盖默认配置
  1269. if (type && cfe.type.includes(type)) {
  1270. cfe.option[cid] = rddeepCloneAssign({}, cfe[type], eopts);
  1271. }else{
  1272. cfe.option[cid] = rddeepCloneAssign({}, eopts);
  1273. }
  1274. let newData = eopts.chartData;
  1275. if(newData){
  1276. //挂载categories和series
  1277. if(cfe.option[cid].xAxis && cfe.option[cid].xAxis.type && cfe.option[cid].xAxis.type === 'category'){
  1278. cfe.option[cid].xAxis.data = newData.categories
  1279. }
  1280. if(cfe.option[cid].yAxis && cfe.option[cid].yAxis.type && cfe.option[cid].yAxis.type === 'category'){
  1281. cfe.option[cid].yAxis.data = newData.categories
  1282. }
  1283. cfe.option[cid].series = []
  1284. for (var i = 0; i < newData.series.length; i++) {
  1285. cfe.option[cid].seriesTemplate = cfe.option[cid].seriesTemplate ? cfe.option[cid].seriesTemplate : {}
  1286. let Template = rddeepCloneAssign({},cfe.option[cid].seriesTemplate,newData.series[i])
  1287. cfe.option[cid].series.push(Template)
  1288. }
  1289. }
  1290. if (typeof window.echarts === 'object') {
  1291. this.newEChart()
  1292. }else{
  1293. const script = document.createElement('script')
  1294. // #ifdef APP-VUE
  1295. script.src = './uni_modules/qiun-data-charts/static/app-plus/echarts.min.js'
  1296. // #endif
  1297. // #ifdef H5
  1298. const { origin, pathname } = window.location
  1299. const rooturl = origin + pathname
  1300. script.src = rooturl + 'uni_modules/qiun-data-charts/static/h5/echarts.min.js'
  1301. // #endif
  1302. script.onload = this.newEChart
  1303. document.head.appendChild(script)
  1304. }
  1305. },
  1306. ecresize(newVal, oldVal, owner, instance){
  1307. if(cfe.instance[this.rid]){
  1308. cfe.instance[this.rid].resize()
  1309. }
  1310. },
  1311. newEChart(){
  1312. let cid = this.rid
  1313. if(cfe.instance[cid] === undefined){
  1314. cfe.instance[cid] = echarts.init(that[cid].$el.children[0])
  1315. //ontap开启后才触发click事件
  1316. if(cfe.option[cid].ontap === true){
  1317. cfe.instance[cid].on('click', resdata => {
  1318. let event = JSON.parse(JSON.stringify({
  1319. x:resdata.event.offsetX,y:resdata.event.offsetY
  1320. }))
  1321. that[cid].callMethod('emitMsg',{name:"getIndex", params:{type:"getIndex", event:event, currentIndex:resdata.dataIndex, value:resdata.data, seriesName: resdata.seriesName,id:cid}})
  1322. })
  1323. // 增加ECharts的highlight消息,实现按下移动返回索引功能。add by onefish 创建于 2021-12-11 09:50
  1324. cfe.instance[cid].on('highlight', resdata => {
  1325. that[cid].callMethod('emitMsg',{name:"getHighlight", params:{type:"highlight", res:resdata, id:cid}})
  1326. })
  1327. }
  1328. this.updataEChart(cid,cfe.option[cid])
  1329. }else{
  1330. this.updataEChart(cid,cfe.option[cid])
  1331. }
  1332. },
  1333. updataEChart(cid,option){
  1334. //替换option内format属性为formatter的预定义方法
  1335. option = rdformatterAssign(option,cfe.formatter)
  1336. if(option.tooltip){
  1337. option.tooltip.show = option.tooltipShow?true:false;
  1338. option.tooltip.position = this.tooltipPosition()
  1339. //tooltipFormat方法,替换组件的tooltipFormat为config-echarts.js内对应的方法
  1340. if (typeof option.tooltipFormat === 'string' && cfe.formatter[option.tooltipFormat]) {
  1341. option.tooltip.formatter = option.tooltip.formatter ? option.tooltip.formatter : cfe.formatter[option.tooltipFormat]
  1342. }
  1343. }
  1344. // 颜色渐变添加的方法
  1345. if (option.series) {
  1346. for (let i in option.series) {
  1347. let linearGradient = option.series[i].linearGradient
  1348. if (linearGradient) {
  1349. option.series[i].color = new echarts.graphic.LinearGradient(linearGradient[0],linearGradient[1],linearGradient[2],linearGradient[3],linearGradient[4])
  1350. }
  1351. }
  1352. }
  1353. cfe.instance[cid].setOption(option, option.notMerge)
  1354. cfe.instance[cid].on('finished', function(){
  1355. that[cid].callMethod('emitMsg',{name:"complete",params:{type:"complete",complete:true,id:cid}})
  1356. if(cfe.instance[cid]){
  1357. cfe.instance[cid].off('finished')
  1358. }
  1359. })
  1360. },
  1361. tooltipPosition(){
  1362. return (point, params, dom, rect, size) => {
  1363. let x = point[0]
  1364. let y = point[1]
  1365. let viewWidth = size.viewSize[0]
  1366. let viewHeight = size.viewSize[1]
  1367. let boxWidth = size.contentSize[0]
  1368. let boxHeight = size.contentSize[1]
  1369. let posX = x + 30
  1370. let posY = y + 30
  1371. if (posX + boxWidth > viewWidth) {
  1372. posX = x - boxWidth - 30
  1373. }
  1374. if (posY + boxHeight > viewHeight) {
  1375. posY = y - boxHeight - 30
  1376. }
  1377. return [posX, posY]
  1378. }
  1379. },
  1380. //==============以下是uCharts的方法====================
  1381. ucinit(newVal, oldVal, owner, instance){
  1382. if(JSON.stringify(newVal) == JSON.stringify(oldVal)){
  1383. return;
  1384. }
  1385. if(!newVal.canvasId){
  1386. return;
  1387. }
  1388. let cid = JSON.parse(JSON.stringify(newVal.canvasId))
  1389. this.rid = cid
  1390. that[cid] = this.$ownerInstance || instance
  1391. cfu.option[cid] = JSON.parse(JSON.stringify(newVal))
  1392. cfu.option[cid] = rdformatterAssign(cfu.option[cid],cfu.formatter)
  1393. let canvasdom = document.getElementById(cid)
  1394. if(canvasdom && canvasdom.children[0]){
  1395. cfu.option[cid].context = canvasdom.children[0].getContext("2d")
  1396. if(cfu.instance[cid] && cfu.option[cid] && cfu.option[cid].update === true){
  1397. this.updataUChart()
  1398. }else{
  1399. setTimeout(()=>{
  1400. cfu.option[cid].context.restore();
  1401. cfu.option[cid].context.save();
  1402. this.newUChart()
  1403. },100)
  1404. }
  1405. }
  1406. },
  1407. newUChart() {
  1408. let cid = this.rid
  1409. cfu.instance[cid] = new uChartsRD(cfu.option[cid])
  1410. cfu.instance[cid].addEventListener('renderComplete', () => {
  1411. that[cid].callMethod('emitMsg',{name:"complete",params:{type:"complete",complete:true,id:cid}})
  1412. cfu.instance[cid].delEventListener('renderComplete')
  1413. });
  1414. cfu.instance[cid].addEventListener('scrollLeft', () => {
  1415. that[cid].callMethod('emitMsg',{name:"scrollLeft",params:{type:"scrollLeft",scrollLeft:true,id:cid}})
  1416. });
  1417. cfu.instance[cid].addEventListener('scrollRight', () => {
  1418. that[cid].callMethod('emitMsg',{name:"scrollRight",params:{type:"scrollRight",scrollRight:true,id:cid}})
  1419. });
  1420. },
  1421. updataUChart() {
  1422. let cid = this.rid
  1423. cfu.instance[cid].updateData(cfu.option[cid])
  1424. },
  1425. tooltipDefault(item, category, index, opts) {
  1426. if (category) {
  1427. let data = item.data
  1428. if(typeof item.data === "object"){
  1429. data = item.data.value
  1430. }
  1431. return category + ' ' + item.name + ':' + data;
  1432. } else {
  1433. if (item.properties && item.properties.name) {
  1434. return item.properties.name ;
  1435. } else {
  1436. return item.name + ':' + item.data;
  1437. }
  1438. }
  1439. },
  1440. showTooltip(e,cid) {
  1441. let tc = cfu.option[cid].tooltipCustom
  1442. if (tc && tc !== undefined && tc !== null) {
  1443. let offset = undefined;
  1444. if (tc.x >= 0 && tc.y >= 0) {
  1445. offset = { x: tc.x, y: tc.y + 10 };
  1446. }
  1447. cfu.instance[cid].showToolTip(e, {
  1448. index: tc.index,
  1449. offset: offset,
  1450. textList: tc.textList,
  1451. formatter: (item, category, index, opts) => {
  1452. if (typeof cfu.option[cid].tooltipFormat === 'string' && cfu.formatter[cfu.option[cid].tooltipFormat]) {
  1453. return cfu.formatter[cfu.option[cid].tooltipFormat](item, category, index, opts);
  1454. } else {
  1455. return this.tooltipDefault(item, category, index, opts);
  1456. }
  1457. }
  1458. });
  1459. } else {
  1460. cfu.instance[cid].showToolTip(e, {
  1461. formatter: (item, category, index, opts) => {
  1462. if (typeof cfu.option[cid].tooltipFormat === 'string' && cfu.formatter[cfu.option[cid].tooltipFormat]) {
  1463. return cfu.formatter[cfu.option[cid].tooltipFormat](item, category, index, opts);
  1464. } else {
  1465. return this.tooltipDefault(item, category, index, opts);
  1466. }
  1467. }
  1468. });
  1469. }
  1470. },
  1471. tap(e) {
  1472. let cid = this.rid
  1473. let ontap = cfu.option[cid].ontap
  1474. let tooltipShow = cfu.option[cid].tooltipShow
  1475. let tapLegend = cfu.option[cid].tapLegend
  1476. if(ontap == false) return;
  1477. let currentIndex=null
  1478. let legendIndex=null
  1479. let rchartdom = document.getElementById('UC'+cid).getBoundingClientRect()
  1480. let tmpe = {}
  1481. if(e.detail.x){//tap或者click的事件
  1482. tmpe = { x: e.detail.x - rchartdom.left, y:e.detail.y - rchartdom.top + rootdom.top}
  1483. }else{//mouse的事件
  1484. tmpe = { x: e.clientX - rchartdom.left, y:e.clientY - rchartdom.top + rootdom.top}
  1485. }
  1486. e.changedTouches = [];
  1487. e.changedTouches.unshift(tmpe)
  1488. currentIndex=cfu.instance[cid].getCurrentDataIndex(e)
  1489. legendIndex=cfu.instance[cid].getLegendDataIndex(e)
  1490. if(tapLegend === true){
  1491. cfu.instance[cid].touchLegend(e);
  1492. }
  1493. if(tooltipShow==true){
  1494. this.showTooltip(e,cid)
  1495. }
  1496. that[cid].callMethod('emitMsg',{name:"getIndex",params:{type:"getIndex",event:tmpe,currentIndex:currentIndex,legendIndex:legendIndex,id:cid, opts: cfu.instance[cid].opts}})
  1497. },
  1498. touchStart(e) {
  1499. let cid = this.rid
  1500. let ontouch = cfu.option[cid].ontouch
  1501. if(ontouch == false) return;
  1502. if(cfu.option[cid].enableScroll === true && e.touches.length == 1){
  1503. cfu.instance[cid].scrollStart(e);
  1504. }
  1505. that[cid].callMethod('emitMsg',{name:"getTouchStart",params:{type:"touchStart",event:e.changedTouches[0],id:cid}})
  1506. },
  1507. touchMove(e) {
  1508. let cid = this.rid
  1509. let ontouch = cfu.option[cid].ontouch
  1510. if(ontouch == false) return;
  1511. if(cfu.option[cid].enableScroll === true && e.changedTouches.length == 1){
  1512. cfu.instance[cid].scroll(e);
  1513. }
  1514. if(cfu.option[cid].ontap === true && cfu.option[cid].enableScroll === false && cfu.option[cid].onmovetip === true){
  1515. let rchartdom = document.getElementById('UC'+cid).getBoundingClientRect()
  1516. let tmpe = { x: e.changedTouches[0].clientX - rchartdom.left, y:e.changedTouches[0].clientY - rchartdom.top + rootdom.top}
  1517. e.changedTouches.unshift(tmpe)
  1518. if(cfu.option[cid].tooltipShow === true){
  1519. this.showTooltip(e,cid)
  1520. }
  1521. }
  1522. if(ontouch === true && cfu.option[cid].enableScroll === true && cfu.option[cid].onzoom === true && e.changedTouches.length == 2){
  1523. cfu.instance[cid].dobuleZoom(e);
  1524. }
  1525. that[cid].callMethod('emitMsg',{name:"getTouchMove",params:{type:"touchMove",event:e.changedTouches[0],id:cid}})
  1526. },
  1527. touchEnd(e) {
  1528. let cid = this.rid
  1529. let ontouch = cfu.option[cid].ontouch
  1530. if(ontouch == false) return;
  1531. if(cfu.option[cid].enableScroll === true && e.touches.length == 0){
  1532. cfu.instance[cid].scrollEnd(e);
  1533. }
  1534. that[cid].callMethod('emitMsg',{name:"getTouchEnd",params:{type:"touchEnd",event:e.changedTouches[0],id:cid}})
  1535. },
  1536. mouseDown(e) {
  1537. let cid = this.rid
  1538. let onmouse = cfu.option[cid].onmouse
  1539. if(onmouse == false) return;
  1540. let rchartdom = document.getElementById('UC'+cid).getBoundingClientRect()
  1541. let tmpe = {}
  1542. tmpe = { x: e.clientX - rchartdom.left, y:e.clientY - rchartdom.top + rootdom.top}
  1543. e.changedTouches = [];
  1544. e.changedTouches.unshift(tmpe)
  1545. cfu.instance[cid].scrollStart(e)
  1546. cfu.option[cid].mousedown=true;
  1547. that[cid].callMethod('emitMsg',{name:"getTouchStart",params:{type:"mouseDown",event:tmpe,id:cid}})
  1548. },
  1549. mouseMove(e) {
  1550. let cid = this.rid
  1551. let onmouse = cfu.option[cid].onmouse
  1552. let tooltipShow = cfu.option[cid].tooltipShow
  1553. if(onmouse == false) return;
  1554. let rchartdom = document.getElementById('UC'+cid).getBoundingClientRect()
  1555. let tmpe = {}
  1556. tmpe = { x: e.clientX - rchartdom.left, y:e.clientY - rchartdom.top + rootdom.top}
  1557. e.changedTouches = [];
  1558. e.changedTouches.unshift(tmpe)
  1559. if(cfu.option[cid].mousedown){
  1560. cfu.instance[cid].scroll(e)
  1561. that[cid].callMethod('emitMsg',{name:"getTouchMove",params:{type:"mouseMove",event:tmpe,id:cid}})
  1562. }else if(cfu.instance[cid]){
  1563. if(tooltipShow==true){
  1564. this.showTooltip(e,cid)
  1565. }
  1566. }
  1567. },
  1568. mouseUp(e) {
  1569. let cid = this.rid
  1570. let onmouse = cfu.option[cid].onmouse
  1571. if(onmouse == false) return;
  1572. let rchartdom = document.getElementById('UC'+cid).getBoundingClientRect()
  1573. let tmpe = {}
  1574. tmpe = { x: e.clientX - rchartdom.left, y:e.clientY - rchartdom.top + rootdom.top}
  1575. e.changedTouches = [];
  1576. e.changedTouches.unshift(tmpe)
  1577. cfu.instance[cid].scrollEnd(e)
  1578. cfu.option[cid].mousedown=false;
  1579. that[cid].callMethod('emitMsg',{name:"getTouchEnd",params:{type:"mouseUp",event:tmpe,id:cid}})
  1580. },
  1581. }
  1582. }
  1583. </script>
  1584. <!-- #endif -->
  1585. <style scoped>
  1586. .chartsview {
  1587. width: 100%;
  1588. height: 100%;
  1589. display: flex;
  1590. flex: 1;
  1591. justify-content: center;
  1592. align-items: center;
  1593. }
  1594. </style>