qiun-data-charts.vue 54 KB

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