chartRefund.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <template>
  2. <view class="chart-body">
  3. <view class="chart-core">
  4. <!--#ifdef MP-ALIPAY -->
  5. <canvas
  6. id="refundChart"
  7. ref="refundChart"
  8. canvas-id="refundChart"
  9. type="2d"
  10. class="charts"
  11. :width="cWidth*pixelRatio"
  12. :height="cHeight*pixelRatio"
  13. :style="{'width':cWidth+'px','height':cHeight+'px'}"
  14. @touchstart="touchMix"
  15. @touchmove="moveMix"
  16. @touchend="touchEndMix" />
  17. <!--#endif-->
  18. <!--#ifndef MP-ALIPAY -->
  19. <canvas
  20. id="refundChart"
  21. ref="refundChart"
  22. :style="{
  23. width: cWidth + 'px',
  24. height: cHeight + 'px'
  25. }"
  26. canvas-id="refundChart"
  27. type="2d"
  28. class="charts"
  29. disable-scroll="true"
  30. @touchstart="touchMix"
  31. @touchmove="moveMix"
  32. @touchend="touchEndMix" />
  33. <!--#endif-->
  34. </view>
  35. </view>
  36. </template>
  37. <script>
  38. import { crmInstrumentSalesTrend } from 'API/crm/work'
  39. import uCharts from '@/components/u-charts/u-charts.js'
  40. let refundChart = null
  41. export default {
  42. name: 'ChartRefund',
  43. props: {
  44. params: {
  45. type: Object,
  46. default: () => {}
  47. }
  48. },
  49. data() {
  50. return {
  51. loading: false,
  52. cWidth: '',
  53. cHeight: '',
  54. pixelRatio: 1,
  55. serverData: '',
  56. }
  57. },
  58. watch: {
  59. params: {
  60. handler() {
  61. this.getData()
  62. },
  63. deep: true
  64. }
  65. },
  66. mounted() {
  67. const _self = this;
  68. // #ifdef MP-ALIPAY
  69. uni.getSystemInfo({
  70. success: function(res) {
  71. if (res.pixelRatio > 1) {
  72. // 正常这里给2就行,如果pixelRatio=3性能会降低一点
  73. // _self.pixelRatio =res.pixelRatio;
  74. _self.pixelRatio = 2;
  75. }
  76. }
  77. });
  78. // #endif
  79. this.cWidth = uni.upx2px(646);
  80. this.cHeight = uni.upx2px(450);
  81. this.gaugeWidth = uni.upx2px(30);
  82. this.getData();
  83. },
  84. methods: {
  85. getData() {
  86. if (this.loading) return
  87. this.loading = true
  88. crmInstrumentSalesTrend({
  89. ...this.params,
  90. type: 2
  91. }).then(res => {
  92. this.loading = false
  93. console.log('回款金额目标及完成情况:', res.list)
  94. if (res[0].type.length >= 8) {
  95. this.forMatter(res)
  96. }
  97. const list = res || []
  98. this.initChart('refundChart', list)
  99. }).catch(() => {
  100. this.loading = false
  101. })
  102. },
  103. forMatter(res) {
  104. res.forEach(item => {
  105. item.type = item.type.split(' ')[0]
  106. })
  107. },
  108. initChart(canvasId, chartData) {
  109. const _self = this
  110. const options = {
  111. $this: _self,
  112. canvasId: canvasId,
  113. canvas2d: false,
  114. context: null,
  115. width: _self.cWidth * _self.pixelRatio,
  116. height: _self.cHeight * _self.pixelRatio,
  117. type: 'column',
  118. pixelRatio: _self.pixelRatio,
  119. background: '#FFFFFF',
  120. fontSize: 11,
  121. animation: true,
  122. dataLabel: false,
  123. padding: [15, 0, 0, 0],
  124. legend: { // 图例
  125. show: true,
  126. position: 'bottom',
  127. float: 'center',
  128. padding: 10,
  129. lineHeight: 11,
  130. margin: 0
  131. },
  132. xAxis: {
  133. labelCount: 5,
  134. axisLine: false,
  135. disableGrid: true,
  136. itemCount: 3,
  137. scrollShow: true,
  138. scrollAlign: 'left'
  139. },
  140. yAxis: {
  141. gridType: 'dash',
  142. min: 0
  143. },
  144. enableScroll: true,
  145. extra: {
  146. column: {
  147. type: 'group',
  148. width: 15
  149. }
  150. },
  151. categories: chartData.map(o => o.type),
  152. series: [
  153. {
  154. name: '回款金额',
  155. color: '#6ca2ff',
  156. data: chartData.map(o => o.money),
  157. type: 'column'
  158. }
  159. ]
  160. }
  161. if (
  162. this.params &&
  163. !this.$isEmpty(this.params.type) &&
  164. [
  165. 'quarter',
  166. 'lastQuarter',
  167. 'year',
  168. 'lastYear'
  169. ].includes(this.params.type)
  170. ) {
  171. options.type = 'mix'
  172. options.series.push({
  173. name: '目标回款金额',
  174. color: '#ff7474',
  175. data: chartData.map(o => o.achievement),
  176. type: 'line'
  177. })
  178. }
  179. // #ifdef MP-WEIXIN
  180. this.$nextTick(function() {
  181. const query = uni.createSelectorQuery().in(this)
  182. query.select('#' + canvasId)
  183. .fields({ node: true, size: true })
  184. .exec(res => {
  185. // console.log('res: ', res[0])
  186. const data = res[0]
  187. const canvas = res[0].node
  188. const ctx = canvas.getContext('2d')
  189. options.context = ctx
  190. options.canvas2d = true
  191. const dpr = wx.getSystemInfoSync().pixelRatio
  192. options.width = _self.cWidth * dpr
  193. options.height = _self.cHeight * dpr
  194. options.pixelRatio = dpr
  195. options.extra.column.width = options.extra.column.width * dpr
  196. canvas.width = options.width
  197. canvas.height = options.height
  198. // eslint-disable-next-line new-cap
  199. refundChart = new uCharts(options)
  200. })
  201. })
  202. // #endif
  203. // #ifndef MP-WEIXIN
  204. // eslint-disable-next-line new-cap
  205. refundChart = new uCharts(options)
  206. // #endif
  207. },
  208. touchMix(e) {
  209. refundChart.scrollStart(e);
  210. },
  211. moveMix(e) {
  212. refundChart.scroll(e);
  213. },
  214. touchEndMix(e) {
  215. refundChart.scrollEnd(e);
  216. refundChart.touchLegend(e);
  217. // 下面是toolTip事件,如果滚动后不需要显示,可不填写
  218. refundChart.showToolTip(e, {
  219. format: function(item, category) {
  220. // console.log(item)
  221. return category + ' ' + item.name + ':' + item.data + '元'
  222. }
  223. });
  224. }
  225. }
  226. }
  227. </script>
  228. <style scoped lang="scss">
  229. .chart-body {
  230. .charts {
  231. width: 100%;
  232. height: 450rpx;
  233. }
  234. }
  235. </style>