irrigationstatistics.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <template>
  2. <div class="boxplot-container">
  3. <div class="chart-container">
  4. <div class="header">
  5. <div class="chart-title">{{ $t('DetectionStatistics.irrigationWaterHeavyMetal') }}</div>
  6. <p>{{ $t('DetectionStatistics.distributionDescription') }}</p>
  7. <p class="sample-subtitle">{{ $t('DetectionStatistics.sampleSource') }}{{ totalPoints }}{{ $t('DetectionStatistics.sampleDataCount') }}</p>
  8. </div>
  9. <div v-if="isLoading" class="loading-state">
  10. <span class="spinner"></span> {{ $t('DetectionStatistics.dataLoading') }}
  11. </div>
  12. <div v-else-if="error" class="error-state">
  13. ❌ {{ $t('DetectionStatistics.loadingFailed') }}{{ error.message }}
  14. </div>
  15. <div v-else>
  16. <div class="chart-wrapper">
  17. <v-chart :option="chartOption" autoresize />
  18. </div>
  19. </div>
  20. </div>
  21. </div>
  22. </template>
  23. <script>
  24. import * as echarts from 'echarts'
  25. import VChart from 'vue-echarts'
  26. import { api8000 } from '@/utils/request'
  27. import { ref, onMounted, computed, watch } from 'vue'
  28. import { useI18n } from 'vue-i18n'
  29. export default {
  30. components: { VChart },
  31. setup() {
  32. const { t } = useI18n()
  33. const { locale } = useI18n()
  34. // -------- 基本状态 --------
  35. const apiUrl = ref('/api/vector/stats/water_sampling_data')
  36. const apiTimestamp = ref(null)
  37. const statsData = ref({})
  38. const chartOption = ref({})
  39. const isLoading = ref(true)
  40. const error = ref(null)
  41. // 样本数统计
  42. const totalPoints = computed(() => {
  43. const firstMetalKey = getHeavyMetals()[0]?.key
  44. return statsData.value[firstMetalKey]?.count || 0
  45. })
  46. // 缓存每个品类的统计量
  47. const statsByIndex = ref([])
  48. // -------- 配置:金属字段 - 改为函数形式 --------
  49. const getHeavyMetals = () => [
  50. { key: 'cr_concentration', name: t('DetectionStatistics.chromium'), color: '#FF9800' },
  51. { key: 'as_concentration', name: t('DetectionStatistics.arsenic'), color: '#4CAF50' },
  52. { key: 'cd_concentration', name: t('DetectionStatistics.cadmium'), color: '#9C27B0' },
  53. { key: 'hg_concentration', name: t('DetectionStatistics.mercury'), color: '#2196F3' },
  54. { key: 'pb_concentration', name: t('DetectionStatistics.lead'), color: '#F44336' }
  55. ]
  56. // -------- 构建箱线数据 --------
  57. const buildBoxplotData = () => {
  58. const heavyMetals = getHeavyMetals()
  59. const xAxisData = heavyMetals.map(m => m.name)
  60. statsByIndex.value = heavyMetals.map(metal => {
  61. const stat = statsData.value[metal.key] || {}
  62. return {
  63. key: metal.key,
  64. name: metal.name,
  65. min: stat.min,
  66. q1: stat.q1,
  67. median: stat.median,
  68. q3: stat.q3,
  69. max: stat.max,
  70. color: metal.color
  71. }
  72. })
  73. const data = statsByIndex.value.map(s => {
  74. if (s.min === undefined || s.min === null) {
  75. return [null, null, null, null, null]
  76. }
  77. return [s.min, s.q1, s.median, s.q3, s.max]
  78. })
  79. return { xAxisData, data }
  80. }
  81. // -------- 初始化图表 --------
  82. const initChart = () => {
  83. const { xAxisData, data } = buildBoxplotData()
  84. chartOption.value = {
  85. tooltip: {
  86. trigger: 'item',
  87. formatter: (params) => {
  88. const s = statsByIndex.value[params.dataIndex]
  89. if (!s || s.min === null) {
  90. return `<div style="font-weight:bold;color:#f56c6c">${xAxisData[params.dataIndex]}</div><div>${t('DetectionStatistics.noValidData')}</div>`
  91. }
  92. return `<div style="font-weight:bold">${xAxisData[params.dataIndex]}</div>
  93. <div style="margin-top:8px">
  94. <div>${t('DetectionStatistics.minValue')}:<span style="color:#5a5;">${s.min.toFixed(4)}</span></div>
  95. <div>${t('DetectionStatistics.q1Value')}:<span style="color:#d87a80;">${s.q1.toFixed(4)}</span></div>
  96. <div>${t('DetectionStatistics.medianValue')}:<span style="color:#f56c6c;font-weight:bold;">${s.median.toFixed(4)}</span></div>
  97. <div>${t('DetectionStatistics.q3Value')}:<span style="color:#d87a80;">${s.q3.toFixed(4)}</span></div>
  98. <div>${t('DetectionStatistics.maxValue')}:<span style="color:#5a5;">${s.max.toFixed(4)}</span></div>
  99. </div>`
  100. },
  101. },
  102. xAxis: {
  103. type: 'category',
  104. data: xAxisData,
  105. name: t('DetectionStatistics.heavyMetalType'),
  106. nameLocation: 'middle',
  107. nameGap: 30,
  108. axisLabel: { color: '#555', rotate: 0, fontWeight: 'bold', fontSize: 11 }
  109. },
  110. yAxis: {
  111. type: 'value',
  112. name: 'ug/L',
  113. nameTextStyle: { fontSize: 12 },
  114. nameLocation: 'end',
  115. nameGap: 8,
  116. axisLabel: { color: '#555', fontWeight: 'bold', fontSize: 11 },
  117. splitLine: { lineStyle: { color: '#f0f0f0' } }
  118. },
  119. series: [{
  120. name: t('DetectionStatistics.concentrationDistribution'),
  121. type: 'boxplot',
  122. data,
  123. itemStyle: {
  124. color: (p) => (getHeavyMetals()[p.dataIndex]?.color || '#1890ff'),
  125. borderWidth: 2
  126. },
  127. emphasis: {
  128. itemStyle: { shadowBlur: 10, shadowColor: 'rgba(0,0,0,0.2)', borderWidth: 3 }
  129. }
  130. }],
  131. grid: { top: '10%', right: '3%', left: '6%', bottom: '10%' }
  132. }
  133. isLoading.value = false
  134. }
  135. // -------- 拉取接口并绘图 --------
  136. onMounted(async () => {
  137. try {
  138. const response = await api8000.get(apiUrl.value)
  139. statsData.value = response.data.data
  140. apiTimestamp.value = new Date().toLocaleString()
  141. initChart()
  142. } catch (err) {
  143. error.value = err
  144. isLoading.value = false
  145. console.error('接口请求失败:', err)
  146. }
  147. })
  148. // 监听语言变化,重新初始化图表
  149. watch(locale, () => {
  150. console.log('语言切换,重新初始化图表')
  151. initChart()
  152. })
  153. return {
  154. apiUrl,
  155. apiTimestamp,
  156. chartOption,
  157. isLoading,
  158. error,
  159. totalPoints
  160. }
  161. }
  162. }
  163. </script>
  164. <style scoped>
  165. .boxplot-container {
  166. width: 100%;
  167. height: 300px;
  168. max-width: 1000px;
  169. margin: 0 auto;
  170. }
  171. .header {
  172. text-align: left;
  173. margin-bottom: 10px;
  174. }
  175. .header h2 {
  176. font-size: 0.6rem;
  177. color: #333;
  178. margin-bottom: 4px;
  179. }
  180. .header p {
  181. font-size: 0.6rem;
  182. color: #666;
  183. margin: 0;
  184. }
  185. .loading-state {
  186. text-align: center;
  187. padding: 40px 0;
  188. color: #666;
  189. }
  190. .loading-state .spinner {
  191. display: inline-block; width: 24px; height: 24px; margin-right: 8px;
  192. border: 3px solid #ccc; border-top-color: #1890ff; border-radius: 50%;
  193. animation: spin 1s linear infinite;
  194. }
  195. @keyframes spin { to { transform: rotate(360deg); } }
  196. .error-state { text-align: center; padding: 40px 0; color: #f56c6c; }
  197. .chart-wrapper { width: 100%; height: 220px; }
  198. .chart-container {
  199. background: white;
  200. border-radius: 12px;
  201. box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
  202. height: 100%;
  203. box-sizing: border-box;
  204. }
  205. .chart-header {
  206. display: flex;
  207. justify-content: space-between;
  208. align-items: flex-start;
  209. margin-bottom: 15px;
  210. }
  211. .chart-title {
  212. font-size: 14px;
  213. color: #2980b9;
  214. font-weight: 600;
  215. }
  216. .title-group {
  217. display: flex;
  218. align-items: left;
  219. }
  220. .sample-subtitle {
  221. font-size: 0.85rem;
  222. color: #888;
  223. margin-top: 4px;
  224. }
  225. </style>