uEchart.vue 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <view :id="'mybox'+cid" class="u-echart" v-if="cid">
  3. <canvas :canvas-id="'uEchart'+cid" :id="'uEchart'+cid" class="u-echart" type="2d" @tap="tap">
  4. </canvas>
  5. </view>
  6. </template>
  7. <script>
  8. import uCharts from '@/uni_modules/qiun-data-charts/js_sdk/u-charts/u-charts.js';
  9. var uChartsInstance = {};
  10. export default {
  11. data() {
  12. return {
  13. cWidth: '',
  14. cHeight: '',
  15. pixelRatio: '',
  16. cid: '',
  17. scrollTop: 0
  18. };
  19. },
  20. created() {
  21. this.cid = this._uid;
  22. },
  23. mounted() {
  24. let _self = this
  25. uni.$on('onPageScroll', function(data) {
  26. _self.scrollTop = Number(data);
  27. })
  28. },
  29. methods: {
  30. initCharts(option) {
  31. const query = uni.createSelectorQuery().in(this);
  32. query.select('#mybox' + this._uid).boundingClientRect(data => {
  33. this.cWidth = data.width;
  34. this.cHeight = data.height;
  35. this.pixelRatio = uni.getSystemInfoSync().pixelRatio;
  36. this.drawCharts('uEchart' + this._uid, option);
  37. }).exec();
  38. },
  39. drawCharts(id, option) {
  40. const query = uni.createSelectorQuery().in(this);
  41. query.select('#' + id).fields({
  42. node: true,
  43. size: true
  44. }).exec(res => {
  45. if (res[0]) {
  46. const canvas = res[0].node;
  47. const ctx = canvas.getContext('2d');
  48. canvas.width = res[0].width * this.pixelRatio;
  49. canvas.height = res[0].height * this.pixelRatio;
  50. const commonOption = {
  51. context: ctx,
  52. width: this.cWidth * this.pixelRatio,
  53. height: this.cHeight * this.pixelRatio,
  54. pixelRatio: this.pixelRatio
  55. }
  56. const chartsOption = Object.assign(commonOption, option);
  57. uChartsInstance[id] = new uCharts(chartsOption);
  58. } else {
  59. console.error("[uCharts]: 未获取到 context");
  60. }
  61. });
  62. },
  63. tap(e) {
  64. const query = uni.createSelectorQuery().in(this);
  65. query.select('#mybox' + this._uid).boundingClientRect(data => {
  66. let rchartdom = data,
  67. tmpe = {};
  68. if (e.detail.x) { //tap或者click的事件
  69. tmpe = {
  70. x: e.detail.x - rchartdom.left,
  71. y: e.detail.y - rchartdom.top - this.scrollTop
  72. }
  73. } else { //mouse的事件
  74. tmpe = {
  75. x: e.clientX - rchartdom.left,
  76. y: e.clientY - rchartdom.top - this.scrollTop
  77. }
  78. }
  79. e.changedTouches = [];
  80. e.changedTouches.unshift(tmpe);
  81. uChartsInstance[e.target.id].showToolTip(e);
  82. }).exec();
  83. }
  84. }
  85. };
  86. </script>
  87. <style scoped>
  88. .u-echart {
  89. width: 100%;
  90. height: 100%;
  91. }
  92. </style>