component.vue 4.0 KB

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