From 3e9d420384e8c076b03be439b6410878f1f6f373 Mon Sep 17 00:00:00 2001
From: ZhangXianQiang <1135831638@qq.com>
Date: 星期四, 29 二月 2024 15:18:56 +0800
Subject: [PATCH] feat:折线图表选择切换数据

---
 src/views/daoAnOffice/right/analysis/index.vue |  161 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 152 insertions(+), 9 deletions(-)

diff --git a/src/views/daoAnOffice/right/analysis/index.vue b/src/views/daoAnOffice/right/analysis/index.vue
index 2b282f2..00af929 100644
--- a/src/views/daoAnOffice/right/analysis/index.vue
+++ b/src/views/daoAnOffice/right/analysis/index.vue
@@ -3,7 +3,8 @@
   <RightTitle title="浜ら�氬畨鍏ㄥ舰鍔跨爺鍒ゅ垎鏋�">
     <template #top>
       <div class="select-container flex">
-        <div class="item whitespace-no-wrap cursor-pointer" v-for="item in selectItems" :key="item.itemIndex">
+        <div class="item whitespace-no-wrap cursor-pointer" :class="{'select-active': item.isActive}" v-for="item in selectItems" :key="item.itemIndex"
+          @click="changeChart(item)">
           {{ item.name }}
         </div>
       </div>
@@ -11,7 +12,11 @@
     <template #content>
       <div class="charts-container">
         <div id="analysisChart" ref="analysisChart"></div>
-
+        <div class="type-select-container" v-show="isShowSelect">
+          <el-select v-model="typeValue" class="select-style" @change="setConfig">
+            <el-option v-for="item in testData1" :key="item.name" :label="item.name" :value="item.name" />
+          </el-select>
+        </div>
       </div>
     </template>
   </RightTitle>
@@ -21,7 +26,21 @@
 import RightTitle from "@/components/right-title";
 import * as echarts from 'echarts';
 import { ref, onMounted } from 'vue';
+
+// dom
 const analysisChart = ref(null);
+
+// echart瀹炰緥
+let echartObject = null;
+
+// type閫夋嫨
+const typeValue = ref('');
+
+// 鏄惁鏄剧ず涓嬫媺妗�
+const isShowSelect = ref(true);
+
+
+
 const selectItems = ref([
   { itemIndex: 1, name: '鏃堕棿缁熻', isActive: false },
   { itemIndex: 2, name: '琛楅亾缁熻', isActive: true },
@@ -39,8 +58,19 @@
   { name: '鐏屽彛琛楅亾', state1: 972, state2: 2124, state3: 1500 },
 ]);
 
+const testData1 = ref([
+  { name: '閮芥睙鍫板競', state: { '2023-1': 1000, '2023-2': 2000, '2023-3': 1233 }, state2: { '2023-1': 12312, '2023-2': 23123, '2023-3': 2111 } },
+  { name: '濂庡厜濉旇閬�', state: { '2023-1': 1000, '2023-2': 6000, '2023-3': 1233 }, state2: { '2023-1': 12312, '2023-2': 23123, '2023-3': 2111 } },
+  { name: '钂查槼琛楅亾', state: { '2023-1': 1000, '2023-2': 2000, '2023-3': 1233 }, state2: { '2023-1': 12312, '2023-2': 23123, '2023-3': 2111 } },
+  { name: '鑱氭簮闀�', state: { '2023-1': 1000, '2023-2': 8000, '2023-3': 12233 }, state2: { '2023-1': 12312, '2023-2': 23123, '2023-3': 2111 } },
+  { name: '閾舵潖琛楅亾', state: { '2023-1': 1000, '2023-2': 2000, '2023-3': 1233 }, state2: { '2023-1': 12312, '2023-2': 23123, '2023-3': 2111 } },
+  { name: '鐭崇緤闀�', state: { '2023-1': 1000, '2023-2': 2000, '2023-3': 12313 }, state2: { '2023-1': 12312, '2023-2': 23123, '2023-3': 2111 } },
+]);
+let acitveData = ref(testData1.value[0]);
 
-const echartsConfig = {
+
+// 鍦嗘煴鍥捐〃
+const barConfig = ref({
   legend: {
     right: '0',
     icon: 'circle',
@@ -83,8 +113,6 @@
   },
   xAxis: { type: 'category', axisTick: false },
   yAxis: {},
-  // Declare several bar series, each will be mapped
-  // to a column of dataset.source by default.
   series: [
     {
       type: 'bar',
@@ -153,13 +181,105 @@
       },
     },
   ]
+});
+
+
+// line閰嶇疆
+const lineConfig = ref(null);
+
+
+
+// 鍥捐〃鏇存崲
+const changeChart = (selectItem) => {
+  selectItems.value.map(item => item.isActive = false);
+  selectItem.isActive = true;
+  if (selectItem.itemIndex == 1) {
+    echartObject.setOption(lineConfig.value, true);
+    isShowSelect.value = true;
+  } else {
+    echartObject.setOption(barConfig.value, true);
+    isShowSelect.value = false;
+  }
+}
+
+// 閰嶇疆鏁版嵁
+const setConfig = () => {
+  acitveData.value = testData1.value.find((item) => item.name === typeValue.value);
+  lineConfig.value = {
+    legend: {
+      right: '12%',
+      icon: 'circle',
+      textStyle: {
+        color: 'rgba(77, 118, 176, 1)'
+      },
+      data: [
+        {
+
+          name: '閰掗┚',
+          itemStyle: {
+            color: 'rgba(207, 178, 73, 1)'
+          }
+        },
+        {
+          name: '鍏朵粬',
+          itemStyle: {
+            color: 'rgba(43, 176, 109, 1)'
+          }
+        },
+      ],
+    },
+    tooltip: {
+      show: true,
+      trigger: 'axis',
+      axisPointer: {
+        type: 'shadow',
+
+      }
+    },
+    grid: {
+      left: 0,
+      right: 0,
+      bottom: 0,
+      top: '15%',
+      containLabel: true
+    },
+    xAxis: {
+      type: 'category',
+      data: Object.keys(acitveData.value.state)
+    },
+    yAxis: {
+      type: 'value'
+    },
+    series: [
+      {
+        name: '閰掗┚',
+        data: Object.entries(acitveData.value.state).map(([key, value]) => value),
+        type: 'line',
+        smooth: true,
+        itemStyle: {
+          color: 'rgba(207, 178, 73, 1)'
+        }
+      },
+      {
+        name: '鍏朵粬',
+        data: Object.entries(acitveData.value.state2).map(([key, value]) => value),
+        type: 'line',
+        smooth: true,
+        itemStyle: {
+          color: 'rgba(43, 176, 109, 1)'
+        }
+      }
+    ]
+  }
+  echartObject.setOption(lineConfig.value, true);
 }
 
 
-
+// 鎸傝浇
 onMounted(() => {
-  const myChart = echarts.init(analysisChart.value);
-  myChart.setOption(echartsConfig);
+  echartObject = echarts.init(analysisChart.value);
+  typeValue.value = testData1.value[0].name;
+  setConfig();
 })
 </script>
 
@@ -175,8 +295,11 @@
   background: rgba(67, 102, 155, 0.4);
   border: 1px solid rgba(47, 91, 157, 0.8);
   flex-shrink: 0;
+  color: #5B83BD;
 }
-
+.select-active {
+ color: #fff; 
+}
 .item:last-child {
   margin-right: 0;
 }
@@ -187,6 +310,15 @@
   padding: 20px;
   background-color: rgba(17, 34, 58, 0.6);
   border: 1px solid rgba(47, 91, 157, 0.8);
+  position: relative;
+}
+
+.type-select-container {
+  position: absolute;
+  right: 20px;
+  top: 14px;
+  width: 120px;
+  height: 23px;
 }
 
 #analysisChart {
@@ -194,4 +326,15 @@
   height: 100%;
   box-sizing: border-box;
 }
+
+::v-deep .el-input.el-input--suffix {
+  .el-input__wrapper {
+    background: rgba(67, 102, 155, 0.4) !important;
+    border: 1px solid rgba(47, 91, 157, 0.8);
+  }
+
+  .el-input__inner {
+    color: #4481DD;
+  }
+}
 </style>
\ No newline at end of file

--
Gitblit v1.8.0