qiun-data-charts.vue 50 KB

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