component.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <!--#ifdef MP-TOUTIAO-->
  3. <canvas v-if="canvasId" :id="canvasId" :canvasId="canvasId" :style="{'width':cWidth*pixelRatio+'px','height':cHeight*pixelRatio+'px', 'transform': 'scale('+(1/pixelRatio)+')','margin-left':-cWidth*(pixelRatio-1)/2+'px','margin-top':-cHeight*(pixelRatio-1)/2+'px'}"></canvas>
  4. <!--#endif-->
  5. <!--#ifndef MP-TOUTIAO-->
  6. <canvas v-if="canvasId" :id="canvasId" :canvasId="canvasId" :style="{'width':cWidth*pixelRatio+'px','height':cHeight*pixelRatio+'px', 'transform': 'scale('+(1/pixelRatio)+')','margin-left':-cWidth*(pixelRatio-1)/2+'px','margin-top':-cHeight*(pixelRatio-1)/2+'px'}" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd" @error="error"></canvas>
  7. <!--#endif-->
  8. </template>
  9. <script>
  10. import uCharts from './u-charts.min.js';
  11. let canvases = {};
  12. export default {
  13. props: {
  14. chartType: {
  15. required: true,
  16. type: String,
  17. default: 'column'
  18. },
  19. opts: {
  20. required: true,
  21. type: Object,
  22. default () {
  23. return null;
  24. },
  25. },
  26. canvasId: {
  27. type: String,
  28. default: 'u-canvas',
  29. },
  30. cWidth: {
  31. default: 375,
  32. },
  33. cHeight: {
  34. default: 250,
  35. },
  36. pixelRatio: {
  37. type: Number,
  38. default: 1,
  39. },
  40. },
  41. mounted() {
  42. this.init();
  43. },
  44. methods: {
  45. init() {
  46. switch (this.chartType) {
  47. case 'column':
  48. this.initColumnChart();
  49. break;
  50. case 'line':
  51. this.initLineChart();
  52. break;
  53. default:
  54. break;
  55. }
  56. },
  57. initColumnChart() {
  58. canvases[this.canvasId] = new uCharts({
  59. $this: this,
  60. canvasId: this.canvasId,
  61. type: 'column',
  62. legend: true,
  63. fontSize: 11,
  64. background: '#FFFFFF',
  65. pixelRatio: this.pixelRatio,
  66. animation: true,
  67. categories: this.opts.categories,
  68. series: this.opts.series,
  69. enableScroll: true,
  70. xAxis: {
  71. disableGrid: true,
  72. itemCount: 4,
  73. scrollShow: true
  74. },
  75. yAxis: {
  76. //disabled:true
  77. },
  78. dataLabel: true,
  79. width: this.cWidth * this.pixelRatio,
  80. height: this.cHeight * this.pixelRatio,
  81. extra: {
  82. column: {
  83. type: 'group',
  84. }
  85. }
  86. });
  87. },
  88. initLineChart() {
  89. canvases[this.canvasId] = new uCharts({
  90. $this: this,
  91. canvasId: this.canvasId,
  92. type: 'line',
  93. fontSize: 11,
  94. legend: true,
  95. dataLabel: false,
  96. dataPointShape: true,
  97. background: '#FFFFFF',
  98. pixelRatio: this.pixelRatio,
  99. categories: this.opts.categories,
  100. series: this.opts.series,
  101. animation: true,
  102. enableScroll: true,
  103. xAxis: {
  104. type: 'grid',
  105. gridColor: '#CCCCCC',
  106. gridType: 'dash',
  107. dashLength: 8,
  108. itemCount: 4,
  109. scrollShow: true
  110. },
  111. yAxis: {
  112. gridType: 'dash',
  113. gridColor: '#CCCCCC',
  114. dashLength: 8,
  115. splitNumber: 5,
  116. min: 10,
  117. max: 180,
  118. format: (val) => {
  119. return val.toFixed(0) + '元'
  120. }
  121. },
  122. width: this.cWidth * this.pixelRatio,
  123. height: this.cHeight * this.pixelRatio,
  124. extra: {
  125. line: {
  126. type: 'straight'
  127. }
  128. }
  129. });
  130. },
  131. // 这里仅作为示例传入两个参数,cid为canvas-id,newdata为更新的数据,需要更多参数请自行修改
  132. changeData(cid,newdata) {
  133. canvases[cid].updateData({
  134. series: newdata.series,
  135. categories: newdata.categories
  136. });
  137. },
  138. touchStart(e) {
  139. canvases[this.canvasId].showToolTip(e, {
  140. format: function(item, category) {
  141. return category + ' ' + item.name + ':' + item.data
  142. }
  143. });
  144. canvases[this.canvasId].scrollStart(e);
  145. },
  146. // #ifndef MP-TOUTIAO
  147. touchMove(e) {
  148. canvases[this.canvasId].scroll(e);
  149. },
  150. touchEnd(e) {
  151. canvases[this.canvasId].scrollEnd(e);
  152. },
  153. error(e) {
  154. uni.showToast({
  155. title: e.errMsg,
  156. icon: 'none'
  157. })
  158. }
  159. // #endif
  160. },
  161. };
  162. </script>
  163. <style scoped>
  164. .charts {
  165. width: 100%;
  166. height: 100%;
  167. flex: 1;
  168. background-color: #FFFFFF;
  169. }
  170. </style>