flow-chart.uvue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <template>
  2. <view class="flow-chart">
  3. <view class="chart-container">
  4. <tui-xechars class="chart-canvas" @initFinished="onChartInit" />
  5. <view class="chart-legend">
  6. <view v-for="(item, index) in flowLegendData" :key="index" class="legend-item">
  7. <view class="legend-left">
  8. <view class="legend-color" :style="{ backgroundColor: item.color }"></view>
  9. <text class="legend-name">{{ item.name }}</text>
  10. </view>
  11. <view class="legend-right">
  12. <text class="legend-value">{{ item.value }}</text>
  13. </view>
  14. </view>
  15. </view>
  16. </view>
  17. </view>
  18. </template>
  19. <script setup lang="uts">
  20. import { ref } from 'vue'
  21. import { TuiCharts } from '@/uni_modules/tui-xechars'
  22. // 流量数据类型
  23. type FlowDataItem = {
  24. name: string
  25. value: number
  26. color: string
  27. }
  28. // 支流出库流量数据(用于图例显示)
  29. const flowLegendData = ref<FlowDataItem[]>([
  30. { name: '汉江', value: 195, color: '#5B9BF3' },
  31. { name: '陶岔', value: 224, color: '#4DD4C0' },
  32. { name: '肖楼', value: 10.92, color: '#FF9966' },
  33. { name: '襄引', value: 15, color: '#FF6B96' },
  34. { name: '鄂北', value: 6, color: '#9B7FE8' }
  35. ])
  36. // 图表初始化
  37. const onChartInit = (charts: TuiCharts): void => {
  38. // 支流出库流量数据
  39. const flowData: FlowDataItem[] = flowLegendData.value
  40. // 计算总流量
  41. let totalFlow: number = 0
  42. flowData.forEach((item) => {
  43. totalFlow += item.value
  44. })
  45. // 创建多重整圆进度条配置
  46. const option: UTSJSONObject = {
  47. type: 'arcbar',
  48. animation: true,
  49. dataLabel: false,
  50. fontSize: 12,
  51. padding: [10, 200, 10, 10],
  52. series: flowData.map((item) => {
  53. return {
  54. name: `${item.name} ${item.value}`,
  55. color: item.color,
  56. data: [{ value: item.value / totalFlow }]
  57. }
  58. }),
  59. title: {
  60. name: '',
  61. fontSize: 0,
  62. color: '#333333'
  63. },
  64. subtitle: {
  65. name: '',
  66. fontSize: 0,
  67. color: '#666666'
  68. },
  69. legend: {
  70. show: false
  71. },
  72. extra: {
  73. arcbar: {
  74. type: 'circle',
  75. direction: 'ccw',
  76. width: 8,
  77. backgroundColor: '#C4D7E9',
  78. startAngle: 1.5,
  79. endAngle: 0.25,
  80. gap: 2
  81. }
  82. }
  83. }
  84. // 添加图表并设置坐标
  85. const arcbarChart = charts.add(0, option)
  86. arcbarChart.setCoordinates({ xOffset: 0, yOffset: 0 })
  87. arcbarChart.draw()
  88. }
  89. </script>
  90. <style lang="scss">
  91. .flow-chart {
  92. width: 100%;
  93. }
  94. .chart-container {
  95. flex-direction: row;
  96. align-items: center;
  97. justify-content: space-between;
  98. }
  99. .chart-canvas {
  100. width: 315rpx;
  101. height: 300rpx;
  102. }
  103. .chart-legend {
  104. flex: 1;
  105. padding-left: 20rpx;
  106. }
  107. .legend-item {
  108. flex-direction: row;
  109. align-items: center;
  110. justify-content: space-between;
  111. margin-bottom: 20rpx;
  112. &:last-child {
  113. margin-bottom: 0;
  114. }
  115. .legend-left {
  116. flex-direction: row;
  117. align-items: center;
  118. }
  119. .legend-right {
  120. flex-direction: row;
  121. align-items: center;
  122. }
  123. }
  124. .legend-color {
  125. width: 24rpx;
  126. height: 24rpx;
  127. border-radius: 4rpx;
  128. margin-right: 12rpx;
  129. }
  130. .legend-name {
  131. font-size: 26rpx;
  132. color: #333333;
  133. margin-right: 8rpx;
  134. }
  135. .legend-value {
  136. font-size: 26rpx;
  137. color: #666666;
  138. font-weight: bold;
  139. }
  140. </style>