u-charts.vue 3.2 KB

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