chartPerformance.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <template>
  2. <view class="chart-body">
  3. <!--#ifdef MP-ALIPAY -->
  4. <canvas
  5. id="performanceChart"
  6. canvas-id="performanceChart"
  7. type="2d"
  8. class="chart"
  9. :width="cWidth*pixelRatio"
  10. :height="cHeight*pixelRatio"
  11. :style="{'width':cWidth+'px','height':cHeight+'px'}" />
  12. <!--#endif-->
  13. <!--#ifndef MP-ALIPAY -->
  14. <canvas
  15. id="performanceChart"
  16. :style="{
  17. width: cWidth + 'px',
  18. height: cHeight + 'px'
  19. }"
  20. canvas-id="performanceChart"
  21. type="2d"
  22. class="chart" />
  23. <!--#endif-->
  24. <view class="info-box">
  25. <view class="box">
  26. <text class="mini-title">
  27. 目标金额
  28. </text>
  29. <text class="info">
  30. {{ chartData.achievementMoneys || '0.00' }}元
  31. </text>
  32. </view>
  33. <view v-if="dropVal === 1" class="box">
  34. <text class="mini-title">
  35. 合同金额
  36. </text>
  37. <text class="info">
  38. {{ chartData.contractMoneys || '0.00' }}元
  39. </text>
  40. </view>
  41. <view v-if="dropVal === 2" class="box">
  42. <text class="mini-title">
  43. 回款金额
  44. </text>
  45. <text class="info">
  46. {{ chartData.receivablesMoneys || '0.00' }}元
  47. </text>
  48. </view>
  49. </view>
  50. </view>
  51. </template>
  52. <script>
  53. import {
  54. crmInstrumentQueryPerformance
  55. } from 'API/crm/work'
  56. import uCharts from '@/components/u-charts/u-charts.js'
  57. import { multiOperation, divideOperation } from '@/utils/lib.js'
  58. let performanceChart = null
  59. // 业绩指标完成率(仪表盘)
  60. export default {
  61. name: 'ChartPerformance',
  62. props: {
  63. params: {
  64. type: Object,
  65. default: () => {}
  66. }
  67. },
  68. data() {
  69. return {
  70. cWidth: '',
  71. cHeight: '',
  72. pixelRatio: 1,
  73. serverData: '',
  74. loading: false,
  75. title: '',
  76. dropVal: null,
  77. chartData: {}
  78. }
  79. },
  80. watch: {
  81. params: {
  82. handler() {
  83. if (this.dropVal) {
  84. this.getData(this.dropVal)
  85. }
  86. },
  87. deep: true
  88. }
  89. },
  90. mounted() {
  91. const _self = this;
  92. // #ifdef MP-ALIPAY
  93. uni.getSystemInfo({
  94. success: function(res) {
  95. if (res.pixelRatio > 1) {
  96. // 正常这里给2就行,如果pixelRatio=3性能会降低一点
  97. // _self.pixelRatio =res.pixelRatio;
  98. _self.pixelRatio = 2;
  99. }
  100. }
  101. });
  102. // #endif
  103. this.cWidth = uni.upx2px(668);
  104. this.cHeight = uni.upx2px(450);
  105. this.gaugeWidth = uni.upx2px(30);
  106. },
  107. methods: {
  108. getData(status) {
  109. if (this.loading) return
  110. this.loading = true
  111. this.dropVal = status
  112. crmInstrumentQueryPerformance({
  113. ...this.params,
  114. type: status
  115. }).then(res => {
  116. this.loading = false
  117. if (res) {
  118. const num = Number(res.proportion) || 0
  119. const data = {
  120. series: [{
  121. name: '完成率',
  122. data: Number(divideOperation(num, 100).toFixed(4))
  123. }]
  124. }
  125. this.chartData = res
  126. this.initChart('performanceChart', data)
  127. }
  128. }).catch(() => {
  129. this.loading = false
  130. })
  131. },
  132. initChart(canvasId, chartData) {
  133. const _self = this
  134. // #ifdef MP-WEIXIN
  135. this.$nextTick(function() {
  136. const query = uni.createSelectorQuery().in(this)
  137. query.select('#' + canvasId)
  138. .fields({ node: true, size: true })
  139. .exec(res => {
  140. // console.log('res: ', res[0])
  141. const data = res[0]
  142. const canvas = res[0].node
  143. const dpr = wx.getSystemInfoSync().pixelRatio
  144. const ctx = canvas.getContext('2d')
  145. const options = _self.getOptions(dpr, canvasId, chartData)
  146. options.context = ctx
  147. options.canvas2d = true
  148. if (dpr === 3) {
  149. options.extra.labelRadius = 10
  150. options.extra.labelCenterPosition = 4
  151. }
  152. canvas.width = options.width
  153. canvas.height = options.height
  154. // console.log('options: ', options)
  155. // eslint-disable-next-line new-cap
  156. performanceChart = new uCharts(options)
  157. })
  158. })
  159. // #endif
  160. // #ifndef MP-WEIXIN
  161. const options = _self.getOptions(_self.pixelRatio, canvasId, chartData)
  162. if (_self.pixelRatio === 3) {
  163. options.extra.labelRadius = 10
  164. options.extra.labelCenterPosition = 5
  165. }
  166. console.log('data: opti', options)
  167. // eslint-disable-next-line new-cap
  168. performanceChart = new uCharts(options)
  169. // #endif
  170. },
  171. getOptions(dpr, canvasId, chartData) {
  172. const rate = multiOperation(chartData.series[0].data, 100)
  173. return {
  174. $this: this,
  175. canvasId: canvasId,
  176. type: 'gauge',
  177. fontSize: 11,
  178. padding: [0, 0, 0, 0],
  179. title: {
  180. name: rate + '%',
  181. color: '#666666',
  182. fontSize: 25 * dpr,
  183. offsetY: 50 * dpr, // 新增参数,自定义调整Y轴文案距离
  184. },
  185. subtitle: {
  186. name: '完成率',
  187. color: '#5F879D',
  188. fontSize: 14 * dpr,
  189. offsetY: -60 * dpr, // 新增参数,自定义调整Y轴文案距离
  190. },
  191. extra: {
  192. labelRadius: 0, // 自定义刻度label偏移
  193. labelCenterPosition: 0, // 自定义刻度label偏移
  194. gauge: {
  195. type: 'default',
  196. width: this.gaugeWidth * dpr, // 坐标轴(指示盘)线宽
  197. startAngle: 0.75,
  198. endAngle: 0.25,
  199. startNumber: 0,
  200. endNumber: 100,
  201. labelFormat: (d) => { return d },
  202. splitLine: {
  203. fixRadius: 0, // 刻度线径向偏移量
  204. splitNumber: 10, // 刻度线分段总数量
  205. width: this.gaugeWidth * dpr, // 仪表盘分割线长度
  206. color: '#FFFFFF',
  207. childNumber: 5, // 子刻度线数量
  208. childWidth: this.gaugeWidth * 0.4 * dpr // 仪表盘子刻度线长度
  209. },
  210. pointer: {
  211. width: this.gaugeWidth * 0.5 * dpr, // 指针宽度
  212. color: '#5F879D'
  213. }
  214. }
  215. },
  216. background: '#FFFFFF',
  217. pixelRatio: dpr,
  218. categories: [
  219. { value: 0.2, color: '#FF7474' },
  220. { value: 0.8, color: '#FECD51' },
  221. { value: 1, color: '#48E78D' },
  222. ],
  223. series: chartData.series.map(o => {
  224. return {
  225. ...o,
  226. data: Number(o.data > 1) ? 1 : o.data
  227. }
  228. }),
  229. animation: true,
  230. width: this.cWidth * dpr,
  231. height: this.cHeight * dpr,
  232. dataLabel: true
  233. }
  234. }
  235. }
  236. }
  237. </script>
  238. <style scoped lang="scss">
  239. .chart-body {
  240. position: relative;
  241. padding: 20px 0 50rpx;
  242. .chart {
  243. width: 100%;
  244. height: 450rpx;
  245. }
  246. .info-box {
  247. position: absolute;
  248. left: 0;
  249. bottom: 30rpx;
  250. width: 100%;
  251. @include center;
  252. .box {
  253. width: 40%;
  254. flex-direction: column;
  255. @include center;
  256. .mini-title {
  257. color: #999;
  258. font-size: 24rpx;
  259. }
  260. .info {
  261. font-size: 28rpx;
  262. margin-top: 5rpx;
  263. }
  264. }
  265. }
  266. }
  267. </style>