You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1513 regels
49 KiB

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