ucharts-pie.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <template>
  2. <view class="ucharts-pie">
  3. <view class="title">第{{ season }}赛季产品销售量统计</view>
  4. <view class="total">
  5. <view class="text">产品总销量</view>
  6. <view class="num"> {{ total }} 套</view>
  7. </view>
  8. <canvas @touchstart="touchPie" :canvas-id="id" :id="id" :style="{'width':cWidth+'px','height':cHeight+'px'}"></canvas>
  9. </view>
  10. </template>
  11. <script>
  12. import uCharts from '../common/js/u-charts.js'
  13. let canvaPie = null
  14. export default {
  15. props: {
  16. chartData: Object,
  17. season: String
  18. },
  19. data() {
  20. return {
  21. id: 'ucharts-pie',
  22. cWidth:'',
  23. cHeight:'',
  24. color: ['#88D8C9', '#EF7D65', '#7ACDEA', '#0088ff', '#FED600'],
  25. total: 0
  26. }
  27. },
  28. methods: {
  29. touchPie (e) {
  30. canvaPie.showToolTip(e, {
  31. format: function (item) {
  32. return item.name + ' : ' + item.data
  33. }
  34. });
  35. },
  36. updateData (data) {
  37. let total = 0
  38. data.series.forEach(item => {
  39. console.log(item.data)
  40. item.textColor = '#FA6342'
  41. total += item.data
  42. })
  43. this.total = total
  44. canvaPie.updateData(data)
  45. }
  46. },
  47. created () {
  48. this.cWidth= uni.upx2px(750);
  49. this.cHeight= uni.upx2px(370);
  50. this.chartData.series.forEach(item => {
  51. item.textColor = '#FA6342'
  52. this.total += +item.data
  53. })
  54. },
  55. mounted () {
  56. canvaPie = new uCharts({
  57. $this: this, //传入this
  58. canvasId: this.id, //传入id
  59. colors: this.color, //柱状图颜色
  60. type: 'pie', //类型柱状图
  61. fontSize: uni.upx2px(24), //字体大小
  62. background:'#FFFFFF', //背景色
  63. pixelRatio: 1, //像素比,默认为1,仅支付宝小程序需要大于1,其他平台必须为1
  64. animation: true, //展示动画
  65. series: this.chartData.series, //数据
  66. extra: {
  67. pie: {
  68. offsetAngle: 90, //饼图旋转角度
  69. labelWidth: uni.upx2px(8),
  70. activeRadius: uni.upx2px(6),
  71. }
  72. },
  73. width: this.cWidth, //图表宽
  74. height: this.cHeight, //图表高
  75. })
  76. }
  77. }
  78. </script>
  79. <style lang="scss" scoped>
  80. .ucharts-pie {
  81. width: 100%;
  82. height: 410rpx;
  83. position: relative;
  84. .title {
  85. position: absolute;
  86. width: 100%;
  87. height: 85rpx;
  88. line-height: 85rpx;
  89. text-align: center;
  90. font-size: 26rpx;
  91. color: rgba(42,42,42,1);
  92. }
  93. .total {
  94. position: absolute;
  95. z-index: 1;
  96. left: 60rpx;
  97. top: 60rpx;
  98. width: 130rpx;
  99. text-align: center;
  100. .text {
  101. height: 24rpx;
  102. line-height: 24rpx;
  103. font-size: 24rpx;
  104. color: rgba(153,153,153,1);
  105. margin-bottom: 12rpx;
  106. }
  107. .num {
  108. height: 30rpx;
  109. font-size: 30rpx;
  110. line-height: 30rpx;
  111. color: rgba(51,51,51,1);
  112. }
  113. }
  114. canvas {
  115. position: absolute;
  116. bottom: 0;
  117. }
  118. }
  119. </style>