qiun-data-charts.vue 51 KB

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