chartContract.vue 6.2 KB

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