component.vue 3.4 KB

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