chart-column.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <view class="column-line"><canvas canvas-id="canvasColumn" id="canvasColumn" class="chart" @touchstart="touchColumn" :width="cWidth * pixelRatio"
  3. :height="cHeight * pixelRatio"
  4. :style="{ width: cWidth + 'px', height: cHeight + 'px' }"></canvas></view>
  5. </template>
  6. <script>
  7. import uCharts from './js/u-charts.js';
  8. var _self;
  9. var canvaColumn = null;
  10. export default {
  11. props: {
  12. unit: String,
  13. color: String,
  14. chartData: Object,
  15. background: String
  16. },
  17. data() {
  18. return {
  19. cWidth: '',
  20. cHeight: '',
  21. pixelRatio: 1,
  22. textarea: ''
  23. };
  24. },
  25. watch: {
  26. chartData() {
  27. this.showColumn('canvasColumn', this.chartData);
  28. }
  29. },
  30. onShareAppMessage() {
  31. //#ifdef MP-QQ
  32. qq.showShareMenu({ showShareItems: ['qq', 'qzone', 'wechatFriends', 'wechatMoment'] });
  33. //#endif
  34. },
  35. onLoad() {
  36. _self = this;
  37. //#ifdef MP-ALIPAY
  38. uni.getSystemInfo({
  39. success: function(res) {
  40. if (res.pixelRatio > 1) {
  41. //正常这里给2就行,如果pixelRatio=3性能会降低一点
  42. //_self.pixelRatio =res.pixelRatio;
  43. _self.pixelRatio = 2;
  44. }
  45. }
  46. });
  47. //#endif
  48. this.cWidth = uni.upx2px(750);
  49. this.cHeight = uni.upx2px(600);
  50. this.getServerData();
  51. },
  52. methods: {
  53. // getServerData() {
  54. // uni.request({
  55. // url: 'https://www.ucharts.cn/data.json',
  56. // data: {},
  57. // success: function(res) {
  58. // console.log(res.data.data);
  59. // let Column = { categories: [], series: [] };
  60. // //这里我后台返回的是数组,所以用等于,如果您后台返回的是单条数据,需要push进去
  61. // Column.categories = res.data.data.ColumnB.categories;
  62. // Column.series = res.data.data.ColumnB.series;
  63. // _self.textarea = JSON.stringify(res.data.data.ColumnB);
  64. // _self.showColumn('canvasColumn', Column);
  65. // },
  66. // fail: () => {
  67. // _self.tips = '网络错误,小程序端请检查合法域名';
  68. // }
  69. // });
  70. // },
  71. showColumn(canvasId, chartData) {
  72. let _self = this;
  73. canvaColumn = new uCharts({
  74. $this: _self,
  75. canvasId: canvasId,
  76. type: 'column',
  77. padding: [15, 5, 0, 15],
  78. legend: {
  79. show: true,
  80. padding: 5,
  81. lineHeight: 11,
  82. margin: 0
  83. },
  84. fontSize: 11,
  85. background: '#FFFFFF',
  86. pixelRatio: 1,
  87. animation: true,
  88. categories: chartData.categories,
  89. series: chartData.series,
  90. xAxis: {
  91. disableGrid: true
  92. },
  93. yAxis: {
  94. data: [
  95. {
  96. position: 'right',
  97. axisLine: false,
  98. format: val => {
  99. return val.toFixed(0) + '元';
  100. }
  101. }
  102. ]
  103. },
  104. dataLabel: true,
  105. width: _self.cWidth * _self.pixelRatio,
  106. height: _self.cHeight * _self.pixelRatio,
  107. extra: {
  108. column: {
  109. type: 'group',
  110. // width: (_self.cWidth * _self.pixelRatio * 0.45) / chartData.categories.length
  111. }
  112. }
  113. });
  114. },
  115. touchColumn(e) {
  116. canvaColumn.showToolTip(e, {
  117. format: function(item, category) {
  118. if (typeof item.data === 'object') {
  119. return category + ' ' + item.name + ':' + item.data.value;
  120. } else {
  121. return category + ' ' + item.name + ':' + item.data;
  122. }
  123. }
  124. });
  125. canvaColumn.touchLegend(e, { animation: true });
  126. },
  127. changeData() {
  128. if (isJSON(_self.textarea)) {
  129. let newdata = JSON.parse(_self.textarea);
  130. canvaColumn.updateData({
  131. series: newdata.series,
  132. categories: newdata.categories,
  133. animation: true
  134. });
  135. } else {
  136. uni.showToast({
  137. title: '数据格式错误',
  138. image: '../../../static/images/alert-warning.png'
  139. });
  140. }
  141. }
  142. }
  143. };
  144. </script>
  145. <style>
  146. /*样式的width和height一定要与定义的cWidth和cHeight相对应*/
  147. .qiun-charts {
  148. width: 750upx;
  149. height: 500upx;
  150. background-color: #ffffff;
  151. }
  152. .charts {
  153. width: 750upx;
  154. height: 500upx;
  155. background-color: #ffffff;
  156. }
  157. </style>