atmcompanyStatics.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <template>
  2. <div class="dashboard">
  3. <div class="chart-container">
  4. <div class="chart-header">
  5. <div class="title-group">
  6. <div class="chart-title">大气污染企业各地区颗粒物排放量统计</div>
  7. <p class="sample-subtitle">样本来源:{{ totalPoints }}个数据</p>
  8. </div>
  9. <div class="method-selector">
  10. <button
  11. class="method-btn"
  12. :class="{active: currentMethod === 'max'}"
  13. @click="currentMethod = 'max'"
  14. >
  15. 最大值
  16. </button>
  17. <button
  18. class="method-btn"
  19. :class="{active: currentMethod === 'avg'}"
  20. @click="currentMethod = 'avg'"
  21. >
  22. 平均值
  23. </button>
  24. </div>
  25. </div>
  26. <div class="chart-wrapper">
  27. <div v-if="loading" class="loading">
  28. <div class="spinner"></div>
  29. <div class="loading-text">数据加载中...</div>
  30. </div>
  31. <div v-else-if="error" class="error">
  32. {{ error }}
  33. </div>
  34. <div v-else-if="!processedData.length" class="no-data">
  35. 无有效数据可展示
  36. </div>
  37. <div v-else ref="mainChart" style="height:100%;width:100%"></div>
  38. </div>
  39. </div>
  40. </div>
  41. </template>
  42. <script>
  43. import * as echarts from 'echarts'
  44. import { api8000 } from '@/utils/request'
  45. export default {
  46. data() {
  47. return {
  48. loading: true,
  49. error: null,
  50. processedData: [],
  51. currentMethod: 'max',
  52. apiEndpoint: '/api/vector/export/all?table_name=atmo_company',
  53. totalPoints: 0,
  54. originalFeatures: [] // 添加原始数据备份用于调试
  55. };
  56. },
  57. mounted() {
  58. this.fetchData();
  59. },
  60. watch: {
  61. currentMethod() {
  62. if (this.processedData.length) {
  63. this.renderChart();
  64. }
  65. }
  66. },
  67. methods: {
  68. async fetchData() {
  69. try {
  70. this.loading = true;
  71. this.error = null;
  72. const response = await api8000.get(this.apiEndpoint);
  73. // 处理NaN问题
  74. let data = response.data;
  75. // 如果返回的是字符串,尝试解析为JSON
  76. if (typeof data === 'string') {
  77. try {
  78. // 替换NaN为null
  79. const cleanedData = data.replace(/\bNaN\b/g, 'null');
  80. data = JSON.parse(cleanedData);
  81. } catch (parseError) {
  82. this.loading = false;
  83. this.error = `数据解析失败: ${parseError.message}`;
  84. console.error("JSON解析错误:", parseError);
  85. return;
  86. }
  87. }
  88. // 检查处理后的数据结构
  89. console.log('处理后的API数据:', data);
  90. // 情况1:返回的是FeatureCollection对象
  91. if (data && data.type === "FeatureCollection" && Array.isArray(data.features)) {
  92. this.originalFeatures = data.features;
  93. this.processData(data.features);
  94. }
  95. // 情况2:返回的是features数组
  96. else if (Array.isArray(data)) {
  97. this.originalFeatures = data;
  98. this.processData(data);
  99. }
  100. // 其他情况
  101. else {
  102. this.loading = false;
  103. this.error = "接口返回数据格式不正确";
  104. console.error("无效的数据结构:", data);
  105. }
  106. } catch (err) {
  107. this.loading = false;
  108. this.error = `数据加载失败: ${err.message}`;
  109. console.error("API请求错误:", err);
  110. }
  111. },
  112. processData(features) {
  113. try {
  114. const countyMap = new Map();
  115. features.forEach(feature => {
  116. const props = feature.properties || {};
  117. let county = props.county || '';
  118. // 标准化地区名称 - 增强处理
  119. county = this.standardizeCountyName(county);
  120. if (county) {
  121. // 提取并校验排放数据
  122. const rawEmission = props.particulate_emission;
  123. let emissionValue = parseFloat(rawEmission);
  124. // 处理可能的字符串值(如"15.836")
  125. if (isNaN(emissionValue) && typeof rawEmission === 'string') {
  126. emissionValue = parseFloat(rawEmission.replace(/[^0-9.]/g, ''));
  127. }
  128. // 过滤无效值
  129. if (isNaN(emissionValue) || emissionValue === null) {
  130. console.warn('无效的排放量值:', rawEmission, '县区:', county);
  131. return;
  132. }
  133. // 统计有效数据
  134. if (!countyMap.has(county)) {
  135. countyMap.set(county, {
  136. county,
  137. values: [emissionValue],
  138. maxEmission: emissionValue,
  139. sumEmission: emissionValue,
  140. pointCount: 1
  141. });
  142. } else {
  143. const countyData = countyMap.get(county);
  144. countyData.values.push(emissionValue);
  145. countyData.pointCount++;
  146. countyData.sumEmission += emissionValue;
  147. if (emissionValue > countyData.maxEmission) {
  148. countyData.maxEmission = emissionValue;
  149. }
  150. }
  151. } else {
  152. console.warn('缺少县区名称:', props);
  153. }
  154. });
  155. // 计算平均值
  156. countyMap.forEach(data => {
  157. data.avgEmission = data.sumEmission / data.values.length;
  158. });
  159. // 转换为数组并排序
  160. this.processedData = Array.from(countyMap.values())
  161. .sort((a, b) => b.maxEmission - a.maxEmission);
  162. // 计算有效样本总数
  163. this.totalPoints = this.processedData.reduce(
  164. (sum, item) => sum + item.pointCount, 0
  165. );
  166. console.log('处理后数据:', this.processedData);
  167. this.loading = false;
  168. if (this.processedData.length === 0) {
  169. this.error = "无有效数据可展示";
  170. console.warn('无有效数据原因:', {
  171. originalCount: features.length,
  172. filteredCount: this.processedData.length,
  173. sampleData: features.slice(0, 3)
  174. });
  175. } else {
  176. this.$nextTick(this.renderChart);
  177. }
  178. } catch (err) {
  179. this.loading = false;
  180. this.error = `数据处理错误: ${err.message}`;
  181. console.error("数据处理异常:", err);
  182. }
  183. },
  184. // 标准化地区名称
  185. standardizeCountyName(county) {
  186. if (!county) return '';
  187. // 特殊处理曲江地区
  188. if (county === '曲江' || county === '曲江新' || county === '曲江开发区') {
  189. return '曲江区';
  190. }
  191. return county;
  192. },
  193. renderChart() {
  194. const chart = echarts.init(this.$refs.mainChart);
  195. // 根据选择的方法确定数据
  196. const method = this.currentMethod;
  197. const valueField = method === 'max' ? 'maxEmission' : 'avgEmission';
  198. const methodLabel = method === 'max' ? '最高值' : '平均值';
  199. const data = this.processedData.map(item => ({
  200. name: item.county,
  201. value: item[valueField],
  202. pointCount: item.pointCount,
  203. maxEmission: item.maxEmission,
  204. avgEmission: item.avgEmission
  205. }));
  206. const option = {
  207. tooltip: {
  208. trigger: 'axis',
  209. formatter: params => {
  210. const d = params[0].data;
  211. return `
  212. <div style="margin-bottom:5px;font-weight:bold">${d.name}</div>
  213. <div>最高排放量: ${d.maxEmission} t/a</div>
  214. <div>平均排放量: ${d.avgEmission} t/a</div>
  215. <div style="margin-top:5px">监测点数量: ${d.pointCount}个</div>
  216. `;
  217. }
  218. },
  219. grid: {
  220. left: 0,
  221. right: "15%",
  222. top: 20,
  223. bottom: "3%",
  224. containLabel: true
  225. },
  226. xAxis: {
  227. type: 'value',
  228. name: `颗粒物排放量`,
  229. nameLocation: 'end',
  230. nameTextStyle:{
  231. padding:-30,
  232. fontSize:12
  233. },
  234. axisLabel: {
  235. formatter: value => value + ' t/a',
  236. fontSize:11,//字体大小
  237. rotate: 30
  238. },
  239. splitLine: {
  240. lineStyle: {
  241. type: 'dashed'
  242. }
  243. }
  244. },
  245. yAxis: {
  246. type: 'category',
  247. data: data.map(d => d.name),
  248. axisLabel: {
  249. formatter: value => value,
  250. fontSize:11,//字体大小
  251. }
  252. },
  253. series: [{
  254. name: '颗粒物排放量',
  255. type: 'bar',
  256. data: data,
  257. label: {
  258. show: true,
  259. position: 'right',
  260. formatter: params => params.value + ' t/a',
  261. fontSize:10,//字体大小
  262. rotate: 15
  263. },
  264. barWidth: '60%',
  265. itemStyle: {
  266. color: '#3498db'
  267. }
  268. }]
  269. };
  270. chart.setOption(option);
  271. // 响应窗口大小变化
  272. window.addEventListener('resize', chart.resize);
  273. }
  274. }
  275. }
  276. </script>
  277. <style>
  278. .dashboard {
  279. height: 100%;
  280. width: 100%;
  281. max-width: 1000px;
  282. margin: 0 auto;
  283. }
  284. h1 {
  285. font-size: 24px;
  286. color: #2980b9;
  287. margin-bottom: 8px;
  288. }
  289. .subtitle {
  290. color: #7f8c8d;
  291. font-size: 14px;
  292. }
  293. .chart-container {
  294. background: white;
  295. border-radius: 12px;
  296. box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
  297. padding: 15px;
  298. height: 100%;
  299. display: flex;
  300. flex-direction:column;
  301. }
  302. .chart-header {
  303. display: flex;
  304. justify-content: space-between;
  305. align-items: flex-start;
  306. margin-bottom: 10px;
  307. }
  308. .chart-title {
  309. font-size: 16px;
  310. color: #2980b9;
  311. font-weight: 600;
  312. }
  313. .title-group {
  314. display: flex;
  315. flex-direction: column;
  316. align-items: flex-start;
  317. }
  318. .method-selector {
  319. background: #f1f8ff;
  320. border-radius: 8px;
  321. padding: 4px 8px;
  322. display: flex;
  323. gap: 6px;
  324. }
  325. .method-btn {
  326. padding: 3px 8px;
  327. border-radius: 6px;
  328. background: transparent;
  329. border: none;
  330. cursor: pointer;
  331. font-size: 12px;
  332. transition: all 0.3s;
  333. }
  334. .method-btn.active {
  335. background: #3498db;
  336. color: white;
  337. }
  338. .chart-wrapper {
  339. flex: 1;
  340. min-height: 0;
  341. height: 400px;
  342. }
  343. .loading, .no-data {
  344. display: flex;
  345. flex-direction: column;
  346. align-items: center;
  347. justify-content: center;
  348. height: 100%;
  349. width: 100%;
  350. }
  351. .spinner {
  352. width: 40px;
  353. height: 40px;
  354. border: 4px solid rgba(52, 152, 219, 0.2);
  355. border-radius: 50%;
  356. border-top-color: #3498db;
  357. animation: spin 1s linear infinite;
  358. margin-bottom: 10px;
  359. }
  360. .loading-text {
  361. font-size: 14px;
  362. color: #3498db;
  363. }
  364. .no-data {
  365. height: 300px;
  366. font-size: 18px;
  367. font-weight: bold;
  368. color: #dc3232;
  369. background-color: #fce4e4;
  370. border-radius: 8px;
  371. text-align: center;
  372. padding: 10px;
  373. }
  374. @keyframes spin {
  375. to { transform: rotate(360deg); }
  376. }
  377. .sample-subtitle {
  378. color: #7f8c8d;
  379. font-size: 12px;
  380. margin-top: 2px;
  381. }
  382. </style>