u-charts.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <canvas
  3. v-if="canvasId"
  4. :id="canvasId"
  5. :canvasId="canvasId"
  6. :style="{
  7. width: (show ? cWidth : 0) * pixelRatio + 'px',
  8. height: (show ? cHeight : 0) * pixelRatio + 'px',
  9. overflow: 'hidden',
  10. transform: 'scale(' + 1 / pixelRatio + ')',
  11. 'margin-left': (-cWidth * (pixelRatio - 1)) / 2 + 'px',
  12. 'margin-top': (-cHeight * (pixelRatio - 1)) / 2 + 'px'
  13. }"
  14. @touchstart="touchStart"
  15. @touchmove="touchMove"
  16. @touchend="touchEnd"
  17. @error="error"
  18. class="canvas"
  19. ></canvas>
  20. </template>
  21. <script>
  22. import crt from './js/chartsUtil';
  23. var canvases = {};
  24. export default {
  25. props: {
  26. chartType: {
  27. required: true,
  28. type: String,
  29. default: 'column'
  30. },
  31. extraType: {
  32. type: String,
  33. default: 'group'
  34. },
  35. opts: {
  36. required: true,
  37. type: Object,
  38. default() {
  39. return null;
  40. }
  41. },
  42. canvasId: {
  43. type: String,
  44. default: 'u-canvas'
  45. },
  46. cWidth: {
  47. type: Number,
  48. default: 375
  49. },
  50. cHeight: {
  51. type: Number,
  52. default: 250
  53. },
  54. pixelRatio: {
  55. type: Number,
  56. default: 1
  57. },
  58. show: {
  59. type: Boolean,
  60. default: true
  61. },
  62. scrollTop: {
  63. type: Number,
  64. default: 0
  65. }
  66. },
  67. mounted() {
  68. this.init();
  69. },
  70. methods: {
  71. init() {
  72. this.opts.type = this.chartType;
  73. // console.log(this.canvasId,this.chartType,this.extraType,this.opts.extra)
  74. if (this.extraType) {
  75. if (this.opts.extra && this.opts.extra[this.chartType]) this.opts.extra[this.chartType].type = this.extraType;
  76. else if (this.opts.extra) this.opts.extra[this.chartType] = { type: this.extraType };
  77. else this.opts.extra = JSON.parse('{"' + this.chartType + '":{"type":"' + this.extraType + '"}}');
  78. }
  79. // console.log(this.opts)
  80. canvases[this.canvasId] = crt.showCharts(this.canvasId, this.opts, this);
  81. },
  82. // 这里仅作为示例传入两个参数,cid为canvas-id,newdata为更新的数据,需要更多参数请自行修改
  83. changeData(cid, newdata, type, etype) {
  84. if (type) newdata.type = type;
  85. if (etype) {
  86. if (newdata.extra && newdata.extra[type]) newdata.extra[type].type = etype;
  87. else if (newdata.extra) newdata.extra[type] = { type: etype };
  88. else newdata.extra = JSON.parse('{"' + type + '":{"type":"' + etype + '"}}');
  89. }
  90. // console.log(canvases[cid])
  91. canvases[cid].updateData(newdata);
  92. },
  93. touchStart(e) {
  94. e = this.touchY(e, this.scrollTop);
  95. canvases[this.canvasId].showToolTip(e, {
  96. format: function(item, category) {
  97. return (category || '') + ' ' + item.name + ':' + (item.data.value || item.data);
  98. }
  99. });
  100. canvases[this.canvasId].scrollStart(e);
  101. },
  102. touchMove(e) {
  103. e = this.touchY(e, this.scrollTop);
  104. canvases[this.canvasId].scroll(e);
  105. },
  106. touchEnd(e) {
  107. e = this.touchY(e, this.scrollTop);
  108. canvases[this.canvasId].scrollEnd(e);
  109. },
  110. error(e) {
  111. console.log(e);
  112. },
  113. touchY(e, t) {
  114. var ty = e.changedTouches ? e.changedTouches[0].y : e.mp.changedTouches[0].y;
  115. if (e.changedTouches) {
  116. e.changedTouches[0].y = ty < 0 ? ty + t : ty;
  117. } else {
  118. e.mp.changedTouches[0].y = ty < 0 ? ty + t : ty;
  119. }
  120. return e;
  121. }
  122. }
  123. };
  124. </script>
  125. <style scoped>
  126. .charts {
  127. width: 100%;
  128. height: 100%;
  129. flex: 1;
  130. background-color: #ffffff;
  131. }
  132. .canvas{
  133. margin:auto !important
  134. }
  135. </style>