component.vue 4.2 KB

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