ucharts-column.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <view class="ucharts-column">
  3. <view class="title">第 {{ season }} 赛季销售情况变化表</view>
  4. <view class="legend">
  5. <view class="legend-item" v-for="(item, index) in color" :key="index">
  6. <text class="bg" :style="{ background: item }"></text>
  7. <text class="text"> {{ chartData.series[index].name }} </text>
  8. </view>
  9. </view>
  10. <canvas :canvas-id="id" :id="id" :style="{'width':cWidth+'px','height':cHeight+'px'}" disable-scroll=true @touchstart="touchColumn" @touchmove="moveColumn" @touchend="touchEndColumn"></canvas>
  11. </view>
  12. </template>
  13. <script>
  14. import uCharts from '../common/js/u-charts.js'
  15. let canvaColumn = null
  16. export default {
  17. props: {
  18. chartData: Object,
  19. season: String
  20. },
  21. data() {
  22. return {
  23. id: 'ucharts-column',
  24. cWidth:'',
  25. cHeight:'',
  26. color: ['#88D8C9', '#EF7D65', '#7ACDEA', '#0088ff', '#FED600'],
  27. moved: false,
  28. maxY: ''
  29. }
  30. },
  31. created () {
  32. this.cWidth= uni.upx2px(750);
  33. this.cHeight= uni.upx2px(360);
  34. this.maxY = Math.max.apply(Math, Array.prototype.concat.apply([], this.chartData.series.map(item => item.data))) || 200
  35. },
  36. methods: {
  37. touchColumn(e){
  38. canvaColumn.scrollStart(e);
  39. },
  40. moveColumn(e) {
  41. this.moved = true
  42. canvaColumn.scroll(e);
  43. },
  44. touchEndColumn(e) {
  45. canvaColumn.scrollEnd(e);
  46. canvaColumn.touchLegend(e, {
  47. animation:true,
  48. });
  49. if (!this.moved) {
  50. canvaColumn.showToolTip(e, {
  51. format: function (item, category) {
  52. return category + ' ' + item.name + ':' + item.data
  53. }
  54. });
  55. } else {
  56. this.moved = false
  57. }
  58. }
  59. },
  60. mounted () {
  61. canvaColumn = new uCharts({
  62. $this: this, //传入this
  63. canvasId: this.id, //传入id
  64. colors: this.color, //柱状图颜色
  65. type: 'column', //类型柱状图
  66. legend:{
  67. show: false, //不显示图例。这里采用的是自定义
  68. },
  69. fontSize: uni.upx2px(20), //字体大小
  70. background:'#FFFFFF', //背景色
  71. dataLabel: false, //不在图表中显示数据标签内容值
  72. pixelRatio: 1, //像素比,默认为1,仅支付宝小程序需要大于1,其他平台必须为1
  73. animation: true, //展示动画
  74. padding: [4, uni.upx2px(40), 0, uni.upx2px(40)],
  75. enableScroll: true,//开启图表拖拽功能
  76. categories: this.chartData.categories, //横坐标数据
  77. series: this.chartData.series, //纵坐标数据
  78. xAxis: {
  79. disableGrid:true, //不绘制X轴网格
  80. itemCount: 7,
  81. scrollShow: true,
  82. scrollAlign: 'right',
  83. scrollBackgroundColor:'#F7F7FF',//可不填写,配合enableScroll图表拖拽功能使用,X轴滚动条背景颜色,默认为 #EFEBEF
  84. scrollColor:'#FA6342',//可不填写,配合enableScroll图表拖拽功能使用,X轴滚动条颜色,默认为 #A6A6A6
  85. },
  86. yAxis: {
  87. disableGrid: true, //不绘制Y轴网格
  88. data:[{ ////Y轴刻度相关
  89. min: 0,
  90. max: this.maxY % 5 === 0 ? this.maxY : Math.ceil(this.maxY / 5) * 5,
  91. calibration: true
  92. }]
  93. },
  94. extra: {
  95. column: {
  96. width: uni.upx2px(18) //柱状图宽度
  97. }
  98. },
  99. width: this.cWidth, //图表宽
  100. height: this.cHeight - 50, //图表高
  101. })
  102. }
  103. }
  104. </script>
  105. <style lang="scss" scoped>
  106. .ucharts-column {
  107. width: 100%;
  108. height: 490rpx;
  109. position: relative;
  110. .title {
  111. position: absolute;
  112. width: 100%;
  113. height: 85rpx;
  114. line-height: 85rpx;
  115. text-align: center;
  116. font-size: 26rpx;
  117. font-weight: 400;
  118. color: rgba(42,42,42,1);
  119. }
  120. .legend {
  121. position: absolute;
  122. right: 64rpx;
  123. top: 112rpx;
  124. width: 76rpx;
  125. height: auto;
  126. .legend-item {
  127. height: 15rpx;
  128. font-size: 15rpx;
  129. line-height: 15rpx;
  130. margin-bottom: 3rpx;
  131. .bg {
  132. height: 100%;
  133. width: 19rpx;
  134. float: left;
  135. }
  136. .text {
  137. height: 100%;
  138. float: right;
  139. }
  140. }
  141. }
  142. canvas {
  143. position: absolute;
  144. bottom: 0;
  145. }
  146. }
  147. </style>