TabMain.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <template>
  2. <view class="">
  3. <view class="tab-title">
  4. <view
  5. v-for="(item, index) in tabList"
  6. :key="index"
  7. class="tab-item"
  8. v-text="item"
  9. :class="index === pickTabIndex ? 'active' : ''"
  10. @click="changeChartsTab(index)"
  11. ></view>
  12. </view>
  13. <view class="charts">
  14. <canvas
  15. class="charts-box"
  16. canvas-id="canvasA"
  17. id="canvasA"
  18. :class="pickTabIndex === 0 ? 'z99' : ''"
  19. @touchstart="e => canvasFn(e, 'canvasA')"
  20. ></canvas>
  21. <canvas
  22. class="charts-box"
  23. canvas-id="canvasB"
  24. id="canvasB"
  25. :class="pickTabIndex === 1 ? 'z99' : ''"
  26. @touchstart="e => canvasFn(e, 'canvasB')"
  27. ></canvas>
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. import uCharts from '@/plugins/stan-ucharts/u-charts/u-charts-2.js'
  33. let uChartsInstance = {}
  34. export default {
  35. name: 'TabMain',
  36. props: {
  37. tabList: {
  38. type: Array,
  39. default: false
  40. }
  41. },
  42. data () {
  43. return {
  44. pickTabIndex: 0,
  45. cWidth: 750,
  46. cHeight: 400
  47. }
  48. },
  49. mounted () {
  50. this.cWidth = uni.upx2px(750)
  51. //这里的 500 对应 css .charts 的 height
  52. this.cHeight = uni.upx2px(400)
  53. let resA = {
  54. categories: ['1', '2', '3', '4', '5', '6', '7'],
  55. series: [
  56. {
  57. name: '日产量',
  58. color: '#488bff',
  59. data: [845, 224, 345, 485, 556, 235, 742]
  60. }
  61. ]
  62. }
  63. let resB = {
  64. categories: ['1', '2', '3', '4', '5', '6', '7'],
  65. series: [
  66. {
  67. name: '不良率',
  68. color: '#488bff',
  69. data: [300, 224, 345, 235, 556, 235, 556]
  70. }
  71. ]
  72. }
  73. this.showDraw('canvasA', resA)
  74. this.showDraw('canvasB', resB)
  75. },
  76. methods: {
  77. changeChartsTab (index) {
  78. this.pickTabIndex = index
  79. },
  80. showDraw (id, data) {
  81. const ctx = uni.createCanvasContext(id, this)
  82. const newuCharts = new uCharts({
  83. type: 'column',
  84. context: ctx,
  85. //canvasId: id,
  86. width: this.cWidth,
  87. height: this.cHeight,
  88. categories: data.categories,
  89. series: data.series,
  90. animation: true,
  91. background: '#FFFFFF',
  92. padding: [15, 15, 0, 5],
  93. touchMoveLimit: 24,
  94. legend: {
  95. show: false
  96. },
  97. dataLabel: false,
  98. xAxis: {
  99. disableGrid: true,
  100. scrollShow: true,
  101. itemCount: 4
  102. },
  103. yAxis: {
  104. splitNumber: 3,
  105. gridColor: '#eee',
  106. data: [
  107. {
  108. min: 0,
  109. max: 900,
  110. axisLine: false
  111. }
  112. ]
  113. },
  114. extra: {
  115. column: {
  116. type: 'group',
  117. width: 30,
  118. activeBgColor: '#000000',
  119. activeBgOpacity: 0.08
  120. }
  121. }
  122. })
  123. this.$set(uChartsInstance, id, newuCharts)
  124. },
  125. canvasFn (e, name) {
  126. uChartsInstance[name].showToolTip(e, {
  127. format: function (item, category) {
  128. return item.data
  129. }
  130. })
  131. }
  132. }
  133. }
  134. </script>
  135. <style lang="scss" scoped>
  136. .tab-title {
  137. padding: 0 160rpx;
  138. display: flex;
  139. height: 82rpx;
  140. line-height: 82rpx;
  141. background-color: #ffffff;
  142. border-bottom: 1px solid #f2f2f2;
  143. .tab-item {
  144. flex: 1;
  145. text-align: center;
  146. font-size: 32rpx;
  147. color: $uni-text-color-grey;
  148. }
  149. .tab-item.active {
  150. color: $j-primary-border-green;
  151. border-bottom: 1px solid $j-primary-border-green;
  152. }
  153. }
  154. .charts {
  155. position: relative;
  156. width: 750rpx;
  157. height: 400rpx;
  158. .charts-box {
  159. position: absolute;
  160. top: 0;
  161. left: 0;
  162. right: 0;
  163. bottom: 0;
  164. background-color: #ffffff;
  165. }
  166. canvas {
  167. width: 750rpx;
  168. height: 400rpx;
  169. background-color: #ffffff;
  170. }
  171. .z99 {
  172. z-index: 99;
  173. }
  174. }
  175. </style>