qiun-data-charts.vue 53 KB

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