qiun-data-charts.vue 51 KB

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