ucharts-column.vue 3.6 KB

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