当前位置:   article > 正文

VUE3:统计分析页面布局+自适应页面参考_vue3自适应布局

vue3自适应布局

一、布局

  1. <template>
  2. <div class="container1">
  3. <div class="form white">
  4. <el-form :inline="true" :rules="rules" :model="queryParams" label-width="80px" ref="querParmRef">
  5. <el-form-item label="城市" prop="cityId">
  6. <el-select v-model="queryParams.cityId" clearable filterable placeholder="请选择城市" style="width: 169px">
  7. <el-option v-for="item in citylists" :key="item.english" :label="item.english"
  8. :value="item.chinese">
  9. </el-option> </el-select>
  10. </el-form-item>
  11. <el-form-item label="公司" prop="companyId">
  12. <el-select v-model="queryParams.companyId" clearable filterable placeholder="请选择公司"
  13. style="width: 169px">
  14. <el-option v-for="item in citylists" :key="item.english" :label="item.english"
  15. :value="item.chinese">
  16. </el-option> </el-select>
  17. </el-form-item>
  18. <el-form-item label="日期" prop="date">
  19. <el-date-picker v-model="queryParams.date" value-format="YYYY-MM-DD" style="width: 169px" type="date"
  20. placeholder="" />
  21. </el-form-item>
  22. <el-form-item label="对比日期" prop="date">
  23. <el-date-picker v-model="queryParams.date" value-format="YYYY-MM-DD" style="width: 169px" type="date"
  24. placeholder="" />
  25. </el-form-item>
  26. <el-form-item>
  27. <el-button @click="resetQuery">重置</el-button>
  28. <el-button type="primary" @click="btntop()" class="btnClick">搜索</el-button>
  29. </el-form-item>
  30. <!-- <el-form-item label="起止时间">
  31. <el-date-picker style="width: 358px" v-model="queryParams.timeValue" type="datetimerange"
  32. range-separator="-" value-format="YYYY-MM-DD HH:mm:ss" start-placeholder="开始时间"
  33. end-placeholder="结束时间" :size="size" />
  34. </el-form-item> -->
  35. <div>
  36. </div>
  37. </el-form>
  38. </div>
  39. <div class="echartsBot white">
  40. <el-tabs v-model="activeName" class="tabs">
  41. <el-tab-pane label="1" name="first"></el-tab-pane>
  42. <el-tab-pane label="2" name="second">2</el-tab-pane>
  43. <el-tab-pane label="3" name="third">3</el-tab-pane>
  44. </el-tabs>
  45. <!-- 上部分 -->
  46. <div class="top">
  47. <!-- 左边 -->
  48. <div class="left">
  49. <div class="chart chart1"></div>
  50. <div class="chart chart2"></div>
  51. </div>
  52. <!-- 中间 -->
  53. <div class="middle">
  54. <div class="chart chart3"></div>
  55. </div>
  56. <!-- 右边 -->
  57. <div class="right">
  58. <div class="chart chart4"></div>
  59. </div>
  60. </div>
  61. <!-- 下部分 -->
  62. <div class="bottom">
  63. <div class="chart chart5"></div>
  64. <div class="chart chart6"></div>
  65. <div class="chart chart7"></div>
  66. <div class="chart chart8"></div>
  67. </div>
  68. </div>
  69. </div>
  70. </template>
  71. <style scoped lang="scss">
  72. .container1 {
  73. padding: calc(100vw * 15 / 1920);
  74. display: flex;
  75. flex-direction: column;
  76. /* 设置垂直方向排列 */
  77. height: 100%;
  78. width: 100%;
  79. background-color: #F7F8FA;
  80. }
  81. .white {
  82. background-color: #fff;
  83. }
  84. .echartsBot {
  85. flex: 1;
  86. width: 100%;
  87. padding: 0 calc(100vw * 15 / 1920) calc(100vw * 15 / 1920);
  88. margin-top: calc(100vw * 10 / 1920);
  89. }
  90. .titles1 {
  91. width: 100%;
  92. height: calc(100vw * 20 / 1920);
  93. font-size: calc(100vw * 14 / 1920);
  94. font-weight: 600;
  95. color: #1d2129;
  96. line-height: calc(100vw * 20 / 1920);
  97. padding-left: 1vh;
  98. }
  99. /* 上部分 */
  100. .echartsBot .top {
  101. display: flex;
  102. height: calc(65% - 50px)
  103. }
  104. /* 左边 */
  105. .echartsBot .left {
  106. width: 46%;
  107. display: flex;
  108. flex-direction: column;
  109. margin-right: calc(100vw * 15 / 1920);
  110. }
  111. /* 中间 */
  112. .echartsBot .middle {
  113. width: 27%;
  114. background-color: #5be948;
  115. margin-right: calc(100vw * 15 / 1920);
  116. }
  117. /* 右边 */
  118. .echartsBot .right {
  119. width: 27%;
  120. background-color: #1e7f97;
  121. }
  122. /* 柱状图 */
  123. .echartsBot .chart {
  124. /* 修改为需要的宽度和高度 */
  125. // width: 300px;
  126. // height: 200px;
  127. }
  128. /* 左边的柱状图 */
  129. .echartsBot .left .chart1 {
  130. /* 样式设置 */
  131. flex:1;
  132. margin-bottom: calc(100vw * 15 / 1920);
  133. background-color: #64b959;
  134. }
  135. .echartsBot .left .chart2 {
  136. /* 样式设置 */
  137. flex:1;
  138. background-color: #8d2424;
  139. }
  140. /* 中间的柱状图 */
  141. .echartsBot .middle .chart3 {
  142. /* 样式设置 */
  143. }
  144. /* 右边的柱状图 */
  145. .echartsBot .right .chart4 {
  146. /* 样式设置 */
  147. }
  148. /* 下部分 */
  149. .echartsBot .bottom {
  150. display: flex;
  151. // height: 40%;
  152. margin-top: calc(100vw * 15 / 1920); // height: calc(50%- 10px);
  153. height: calc(35% - calc(100vw * 19 / 1920));
  154. }
  155. /* 底部柱状图 */
  156. .echartsBot .bottom .chart {
  157. /* 修改为需要的宽度和高度 */
  158. // width: 200px;
  159. // height: 150px;
  160. }
  161. .echartsBot .bottom .chart5 {
  162. /* 样式设置 */
  163. flex: 1;
  164. background-color: #ecd2d2;
  165. height: 100%;
  166. margin-right: calc(100vw * 15 / 1920);
  167. }
  168. .echartsBot .bottom .chart6 {
  169. /* 样式设置 */
  170. flex: 1;
  171. margin-right: calc(100vw * 15 / 1920);
  172. background-color: #b6c789;
  173. height: 100%;
  174. }
  175. .echartsBot .bottom .chart7 {
  176. /* 样式设置 */
  177. flex: 1;
  178. margin-right: calc(100vw * 15 / 1920);
  179. background-color: #a7a2e0;
  180. height: 100%;
  181. }
  182. .echartsBot .bottom .chart8 {
  183. /* 样式设置 */
  184. flex: 1;
  185. background-color: #dba6d0;
  186. height: 100%;
  187. }
  188. </style>

 上面效果图的代码,用的flex布局,宽高边距都用了动态的,浏览器窗口大小改变也会自适应

二、分完模块优化

最终效果图如上

完整版代码

 

  1. <template>
  2. <div class="container1">
  3. <div class="form white">
  4. <el-form :inline="true" :rules="rules" :model="queryParams" label-width="80px" ref="querParmRef">
  5. <el-form-item label="城市" prop="cityId">
  6. <el-select v-model="queryParams.cityId" clearable filterable placeholder="请选择城市" style="width: 169px">
  7. <el-option v-for="item in citylists" :key="item.english" :label="item.english"
  8. :value="item.chinese">
  9. </el-option> </el-select>
  10. </el-form-item>
  11. <el-form-item label="公司" prop="companyId">
  12. <el-select v-model="queryParams.companyId" clearable filterable placeholder="请选择公司"
  13. style="width: 169px">
  14. <el-option v-for="item in citylists" :key="item.english" :label="item.english"
  15. :value="item.chinese">
  16. </el-option> </el-select>
  17. </el-form-item>
  18. <el-form-item label="日期" prop="date">
  19. <el-date-picker v-model="queryParams.date" value-format="YYYY-MM-DD" style="width: 169px" type="date"
  20. placeholder="" />
  21. </el-form-item>
  22. <el-form-item label="对比日期" prop="date">
  23. <el-date-picker v-model="queryParams.date" value-format="YYYY-MM-DD" style="width: 169px" type="date"
  24. placeholder="" />
  25. </el-form-item>
  26. <el-form-item>
  27. <el-button @click="resetQuery">重置</el-button>
  28. <el-button type="primary" @click="btntop()" class="btnClick">搜索</el-button>
  29. </el-form-item>
  30. <!-- <el-form-item label="起止时间">
  31. <el-date-picker style="width: 358px" v-model="queryParams.timeValue" type="datetimerange"
  32. range-separator="-" value-format="YYYY-MM-DD HH:mm:ss" start-placeholder="开始时间"
  33. end-placeholder="结束时间" :size="size" />
  34. </el-form-item> -->
  35. <div>
  36. </div>
  37. </el-form>
  38. </div>
  39. <div class="echartsBot white">
  40. <el-tabs v-model="activeName" class="tabs">
  41. <el-tab-pane label="1" name="first"></el-tab-pane>
  42. <!-- <el-tab-pane label="冷热能效耗能排名总览" name="first"></el-tab-pane> -->
  43. <el-tab-pane label="2" name="second">2</el-tab-pane>
  44. <el-tab-pane label="3" name="third">3</el-tab-pane>
  45. </el-tabs>
  46. <!-- 上部分 -->
  47. <div class="top">
  48. <!-- 左边 -->
  49. <div class="left">
  50. <div class="chart chart1">
  51. <div class="titles1">面积排名</div>
  52. <Echarts1 :echartList="echartList1" />
  53. </div>
  54. <div class="chart chart2">
  55. <div class="titles1">运营天数排名</div>
  56. <Echarts2 :echartList="echartList2" />
  57. </div>
  58. </div>
  59. <!-- 中间 -->
  60. <div class="middle">
  61. <div class="titles1">COP排名</div>
  62. <Echarts3 :echartList="echartList3" />
  63. <!-- <div class="chart chart3"></div> -->
  64. </div>
  65. <!-- 右边 -->
  66. <div class="right">
  67. <div class="titles1">SCOP排名</div>
  68. <Echarts4 :echartList="echartList4" />
  69. </div>
  70. </div>
  71. <!-- 下部分 -->
  72. <div class="bottom">
  73. <div class="chart chart5">
  74. <div class="titles1">热单耗排名</div>
  75. <Echarts5 :echartList="echartList5" />
  76. </div>
  77. <div class="chart chart6">
  78. <div class="titles1">热单耗排名</div>
  79. <Echarts6 :echartList="echartList6" />
  80. </div>
  81. <div class="chart chart7">
  82. <div class="titles1">热单耗排名</div>
  83. <Echarts7 :echartList="echartList7" />
  84. </div>
  85. <div class="chart chart8">
  86. <div class="titles1">热单耗排名</div>
  87. <Echarts8 :echartList="echartList8" />
  88. </div>
  89. </div>
  90. </div>
  91. </div>
  92. </template>
  93. <script setup>
  94. import { ElMessageBox, ElMessage, ElLoading } from "element-plus";
  95. import Echarts1 from "./echarts/echarts1.vue";
  96. import Echarts2 from "./echarts/echarts2.vue";
  97. import Echarts3 from "./echarts/echarts3.vue";
  98. import Echarts4 from "./echarts/echarts4.vue";
  99. import Echarts5 from "./echarts/echarts5.vue";
  100. import Echarts6 from "./echarts/echarts6.vue";
  101. import Echarts7 from "./echarts/echarts7.vue";
  102. import Echarts8 from "./echarts/echarts8.vue";
  103. const { proxy } = getCurrentInstance();
  104. import { parseTime } from "@/utils/ruoyi";
  105. import { useRoute } from "vue-router";
  106. const echartList1 = ref([
  107. { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 },
  108. { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 },
  109. { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 },
  110. { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 },
  111. { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 },
  112. { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 },
  113. { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 },
  114. ]);
  115. const echartList2 = ref([
  116. { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 },
  117. { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 },
  118. ]);
  119. const echartList3 = ref([
  120. { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 },
  121. { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 },
  122. ]);
  123. const echartList4 = ref([
  124. { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }
  125. ])
  126. const echartList5 = ref([
  127. { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 },
  128. { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }
  129. ])
  130. const echartList6 = ref([
  131. { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }
  132. ])
  133. const echartList7 = ref([
  134. { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }
  135. ])
  136. const echartList8 = ref([
  137. { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }, { cityName: '济南', parkArea: 12 }
  138. ])
  139. const activeName = ref('first')
  140. </script>
  141. <style scoped lang="scss">
  142. .container1 {
  143. padding: calc(100vw * 15 / 1920);
  144. display: flex;
  145. flex-direction: column;
  146. /* 设置垂直方向排列 */
  147. height: 100%;
  148. width: 100%;
  149. background-color: #F7F8FA;
  150. }
  151. .white {
  152. background-color: #fff;
  153. }
  154. .echartsBot {
  155. flex: 1;
  156. width: 100%;
  157. padding: 0 calc(100vw * 15 / 1920) ;
  158. margin-top: calc(100vw * 10 / 1920);
  159. }
  160. .titles1 {
  161. width: 100%;
  162. height: calc(100vw * 20 / 1920);
  163. font-size: calc(100vw * 14 / 1920);
  164. font-weight: 600;
  165. color: #1d2129;
  166. line-height: calc(100vw * 20 / 1920);
  167. padding-left: 1vh;
  168. }
  169. /* 上部分 */
  170. .echartsBot .top {
  171. display: flex;
  172. height: calc(63% - 50px)
  173. }
  174. /* 左边 */
  175. .echartsBot .left {
  176. width: 46%;
  177. display: flex;
  178. flex-direction: column;
  179. margin-right: calc(100vw * 15 / 1920);
  180. }
  181. /* 中间 */
  182. .echartsBot .middle {
  183. width: 27%;
  184. // padding: calc(100vw * 15 / 1920);
  185. margin-right: calc(100vw * 15 / 1920);
  186. border-radius: 4px 4px 4px 4px;
  187. border: 1px solid rgba(61, 61, 61, 0.1);
  188. }
  189. /* 右边 */
  190. .echartsBot .right {
  191. width: 27%;
  192. // padding: calc(100vw * 15 / 1920);
  193. border-radius: 4px 4px 4px 4px;
  194. border: 1px solid rgba(61, 61, 61, 0.1);
  195. }
  196. /* 柱状图 */
  197. .chart {
  198. /* 修改为需要的宽度和高度 */
  199. // width: 300px;
  200. // height: 200px;
  201. // padding: calc(100vw * 15 / 1920);
  202. }
  203. /* 左边的柱状图 */
  204. .echartsBot .left .chart1 {
  205. /* 样式设置 */
  206. height: calc(50% - calc(100vw * 16 / 1920));
  207. margin-bottom: calc(100vw * 10 / 1920);
  208. border-radius: 4px 4px 4px 4px;
  209. border: 1px solid rgba(61, 61, 61, 0.1);
  210. }
  211. .echartsBot .left .chart2 {
  212. /* 样式设置 */
  213. flex: 1;
  214. border-radius: 4px 4px 4px 4px;
  215. border: 1px solid rgba(61, 61, 61, 0.1);
  216. }
  217. /* 中间的柱状图 */
  218. .echartsBot .middle .chart3 {
  219. /* 样式设置 */
  220. }
  221. /* 右边的柱状图 */
  222. .echartsBot .right .chart4 {
  223. /* 样式设置 */
  224. }
  225. /* 下部分 */
  226. .echartsBot .bottom {
  227. display: flex;
  228. // height: 40%;
  229. margin-top: calc(100vw * 15 / 1920); // height: calc(50%- 10px);
  230. height: calc(35% - calc(100vw * 19 / 1920));
  231. }
  232. /* 底部柱状图 */
  233. .echartsBot .bottom .chart {
  234. /* 修改为需要的宽度和高度 */
  235. // width: 200px;
  236. // height: 150px;
  237. }
  238. .echartsBot .bottom .chart5 {
  239. /* 样式设置 */
  240. flex: 1;
  241. height: 100%;
  242. margin-right: calc(100vw * 15 / 1920);
  243. border-radius: 4px 4px 4px 4px;
  244. border: 1px solid rgba(61, 61, 61, 0.1);
  245. }
  246. .echartsBot .bottom .chart6 {
  247. /* 样式设置 */
  248. flex: 1;
  249. margin-right: calc(100vw * 15 / 1920);
  250. height: 100%;
  251. border-radius: 4px 4px 4px 4px;
  252. border: 1px solid rgba(61, 61, 61, 0.1);
  253. }
  254. .echartsBot .bottom .chart7 {
  255. /* 样式设置 */
  256. flex: 1;
  257. margin-right: calc(100vw * 15 / 1920);
  258. height: 100%;
  259. border-radius: 4px 4px 4px 4px;
  260. border: 1px solid rgba(61, 61, 61, 0.1);
  261. }
  262. .echartsBot .bottom .chart8 {
  263. /* 样式设置 */
  264. flex: 1;
  265. height: 100%;
  266. border-radius: 4px 4px 4px 4px;
  267. border: 1px solid rgba(61, 61, 61, 0.1);
  268. }
  269. </style>

具体echarts图

第一个

  1. <template>
  2. <div id="echarts1"></div>
  3. </template>
  4. <script setup>
  5. import * as echarts from "echarts";
  6. const emit = defineEmits();
  7. const props = defineProps({
  8. echartList: {
  9. default: [],
  10. },
  11. });
  12. const initChart = () => {
  13. let xdata = [];
  14. let ydata1 = [];
  15. let xdataid = [];
  16. props.echartList.forEach((item) => {
  17. xdata.push(item.cityName);
  18. xdataid.push(item.cityId);
  19. ydata1.push(item.parkArea);
  20. });
  21. const machart = echarts.init(document.getElementById("echarts1"));
  22. var defaultShowNum = 10; // 默认显示的数据条数
  23. var start = 0;
  24. var end = Math.min(defaultShowNum, xdata.length) / xdata.length * 100;
  25. var option = {
  26. tooltip: {
  27. trigger: "axis",
  28. formatter: '{c}', // 修改为只显示名称和数值
  29. backgroundColor: 'rgba(255,255,255,0.8)', // 气泡框背景颜色
  30. textStyle: {
  31. color: '#333', // 文字颜色
  32. fontSize: 14 // 文字大小
  33. },
  34. position: function (pos, params, el, elRect, size) {
  35. return { left: pos[0], top: pos[1] };
  36. }, // 设置气泡框的位置在鼠标悬浮的地方
  37. padding: [10, 10, 10, 10], // 设置气泡框的内边距
  38. extraCssText: 'width: auto; height: auto; text-align: center; line-height: 30px;', // 设置气泡框的宽度、高度、文本居中显示
  39. },
  40. dataZoom: [
  41. {
  42. type: 'slider', // 缩放条类型为滑动条
  43. show: true, // 显示缩放条
  44. start: start, // 根据数据计算的开始位置
  45. end: end, // 根据数据计算的结束位置
  46. bottom: '5%', // 距离底部的距离
  47. height: 5, // 设置高度
  48. },
  49. {
  50. type: 'inside', // 内置缩放条
  51. start: start,
  52. end: end,
  53. },
  54. ],
  55. calculable: true,
  56. // legend: {
  57. // // data: ["xxx"],
  58. // left: "left",s
  59. // x: "left",
  60. // top: "0%",
  61. // left: "2%",
  62. // icon: "roundRect",
  63. // itemWidth: 10, // 设置宽度
  64. // itemHeight: 10, // 设置高度
  65. // },
  66. color: ["#579AFF", "#B1B1F0", "#F7BEBE", '#BDF6E6'],
  67. grid: {
  68. top: "14sss%",
  69. bottom: "10%",
  70. left: "1%",
  71. right: "1%",
  72. containLabel: true,
  73. },
  74. xAxis: [
  75. {
  76. type: "category",
  77. data: xdata,
  78. axisTick: {
  79. show: false, // 不显示刻度
  80. },
  81. },
  82. ],
  83. yAxis: [
  84. {
  85. type: "value",
  86. },
  87. ],
  88. series: [
  89. {
  90. // name: "城市占比",
  91. type: "bar",
  92. data: ydata1,
  93. barWidth: "35%",
  94. // label: { // 添加标签配置
  95. // show: true, // 显示标签
  96. // position: 'top', // 标签位置为柱子顶部
  97. // formatter: '{c} ', // 标签内容格式为数据加单位
  98. // },
  99. },
  100. ],
  101. };
  102. machart.setOption(option);
  103. setTimeout(() => {
  104. window.addEventListener("resize", resizeFn);
  105. }, 100);
  106. const resizeFn = () => {
  107. return machart.resize();
  108. };
  109. machart.on("click", (params) => {
  110. if (params.componentType === "series") {
  111. const dataIndex = params.dataIndex;
  112. const yValue = xdata[dataIndex]; // 获取对应柱子的y值
  113. const cidtyid = xdataid[dataIndex]; // 获取对应柱子的y值
  114. emit("changedi", yValue, cidtyid);
  115. // 在这里你可以对获取到的y值进行其他操作,比如弹窗显示等
  116. }
  117. });
  118. };
  119. setTimeout(() => {
  120. initChart();
  121. }, 800);
  122. defineExpose({
  123. initChart,
  124. });
  125. onBeforeUnmount(() => {
  126. // 离开页面必须进行移除,否则会造成内存泄漏,导致卡顿
  127. window.removeEventListener("resize", initChart);
  128. });
  129. </script>
  130. <style scoped>
  131. #echarts1 {
  132. width: 100%;
  133. height: 95%;
  134. /* height: calc(30vh- 10px); */
  135. }
  136. </style>

右边两个

  1. <template>
  2. <div id="echarts3"></div>
  3. </template>
  4. <script setup>
  5. import * as echarts from "echarts";
  6. const emit = defineEmits();
  7. const props = defineProps({
  8. echartList: {
  9. default: [],
  10. },
  11. });
  12. const initChart = () => {
  13. let xdata = [];
  14. let ydata1 = [];
  15. let xdataid = [];
  16. props.echartList.forEach((item) => {
  17. xdata.push(item.cityName);
  18. xdataid.push(item.cityId);
  19. ydata1.push(item.parkArea);
  20. });
  21. const machart = echarts.init(document.getElementById("echarts3"));
  22. var defaultShowNum = 10; // 默认显示的数据条数
  23. var start = 0;
  24. var end = Math.min(defaultShowNum, xdata.length) / xdata.length * 100;
  25. var option = {
  26. tooltip: {
  27. trigger: "axis",
  28. formatter: '{c}', // 修改为只显示名称和数值
  29. backgroundColor: 'rgba(255,255,255,0.8)', // 气泡框背景颜色
  30. textStyle: {
  31. color: '#333', // 文字颜色
  32. fontSize: 14 // 文字大小
  33. },
  34. position: function (pos, params, el, elRect, size) {
  35. return { left: pos[0], top: pos[1] };
  36. }, // 设置气泡框的位置在鼠标悬浮的地方
  37. padding: [10, 10, 10, 10], // 设置气泡框的内边距
  38. extraCssText: 'width: auto; height: auto; text-align: center; line-height: 30px;', // 设置气泡框的宽度、高度、文本居中显示
  39. },
  40. dataZoom: [
  41. {
  42. type: 'slider',
  43. orient: 'vertical', // 设置为垂直方向
  44. show: true,
  45. start: start,
  46. end: end,
  47. right: 0, // 距离右侧的距离
  48. width: 10, // 设置宽度
  49. },
  50. {
  51. type: 'inside', // 内置缩放条
  52. start: start,
  53. end: end,
  54. },
  55. ],
  56. calculable: true,
  57. // legend: {
  58. // // data: ["xxx"],
  59. // left: "left",s
  60. // x: "left",
  61. // top: "0%",
  62. // left: "2%",
  63. // icon: "roundRect",
  64. // itemWidth: 10, // 设置宽度
  65. // itemHeight: 10, // 设置高度
  66. // },
  67. // color: ["#579AFF", "#B1B1F0", "#F7BEBE", '#BDF6E6'],
  68. grid: {
  69. top: "5%",
  70. bottom: "1%",
  71. left: "1%",
  72. right: "8%",
  73. containLabel: true,
  74. },
  75. yAxis: [
  76. {
  77. type: "category",
  78. data: xdata.reverse(),
  79. axisLine: {
  80. show: false, // 隐藏坐标轴线
  81. },
  82. axisTick: {
  83. show: false, // 不显示刻度线
  84. },
  85. axisLabel: {
  86. show: true, // 显示坐标轴标签
  87. },
  88. },
  89. ],
  90. xAxis: [
  91. {
  92. type: "value",
  93. show: false, // 不显示x轴
  94. },
  95. ],
  96. series: [
  97. {
  98. // name: "城市占比",
  99. type: "bar",
  100. data: ydata1.reverse(),
  101. barWidth: "35%",
  102. itemStyle: {
  103. color: function (params) {
  104. if (params.dataIndex >= xdata.length - 3) {
  105. // 最后三条数据
  106. if (params.dataIndex === xdata.length - 1) {
  107. return '#E96163'; // 设置最后一条数据的颜色
  108. } else if (params.dataIndex === xdata.length - 2) {
  109. return '#FEA935'; // 设置倒数第二条数据的颜色
  110. } else {
  111. return '#F0DC5A'; // 设置倒数第三条数据的颜色
  112. }
  113. } else {
  114. return '#579AFF'; // 其他数据的颜色
  115. }
  116. }
  117. },
  118. },
  119. ],
  120. };
  121. machart.setOption(option);
  122. setTimeout(() => {
  123. window.addEventListener("resize", resizeFn);
  124. }, 100);
  125. const resizeFn = () => {
  126. return machart.resize();
  127. };
  128. machart.on("click", (params) => {
  129. if (params.componentType === "series") {
  130. const dataIndex = params.dataIndex;
  131. const yValue = xdata[dataIndex]; // 获取对应柱子的y值
  132. const cidtyid = xdataid[dataIndex]; // 获取对应柱子的y值
  133. emit("changedi", yValue, cidtyid);
  134. // 在这里你可以对获取到的y值进行其他操作,比如弹窗显示等
  135. }
  136. });
  137. };
  138. setTimeout(() => {
  139. initChart();
  140. }, 800);
  141. defineExpose({
  142. initChart,
  143. });
  144. onBeforeUnmount(() => {
  145. // 离开页面必须进行移除,否则会造成内存泄漏,导致卡顿
  146. window.removeEventListener("resize", initChart);
  147. });
  148. </script>
  149. <style scoped>
  150. #echarts3 {
  151. width: 100%;
  152. height: 95%;
  153. /* height: calc(30vh- 10px); */
  154. }
  155. </style>

最下面的

  1. <template>
  2. <div id="echarts8"></div>
  3. </template>
  4. <script setup>
  5. import * as echarts from "echarts";
  6. const emit = defineEmits();
  7. const props = defineProps({
  8. echartList: {
  9. default: [],
  10. },
  11. });
  12. const initChart = () => {
  13. let xdata = [];
  14. let ydata1 = [];
  15. let xdataid = [];
  16. props.echartList.forEach((item) => {
  17. xdata.push(item.cityName);
  18. xdataid.push(item.cityId);
  19. ydata1.push(item.parkArea);
  20. });
  21. const machart = echarts.init(document.getElementById("echarts8"));
  22. var defaultShowNum = 10; // 默认显示的数据条数
  23. var start = 0;
  24. var end = Math.min(defaultShowNum, xdata.length) / xdata.length * 100;
  25. var option = {
  26. tooltip: {
  27. trigger: "axis",
  28. formatter: '{c}', // 修改为只显示名称和数值
  29. backgroundColor: 'rgba(255,255,255,0.8)', // 气泡框背景颜色
  30. textStyle: {
  31. color: '#333', // 文字颜色
  32. fontSize: 14 // 文字大小
  33. },
  34. position: function (pos, params, el, elRect, size) {
  35. return { left: pos[0], top: pos[1] };
  36. }, // 设置气泡框的位置在鼠标悬浮的地方
  37. padding: [10, 10, 10, 10], // 设置气泡框的内边距
  38. extraCssText: 'width: auto; height: auto; text-align: center; line-height: 30px;', // 设置气泡框的宽度、高度、文本居中显示
  39. },
  40. dataZoom: [
  41. {
  42. type: 'slider', // 缩放条类型为滑动条
  43. show: true, // 显示缩放条
  44. start: start, // 根据数据计算的开始位置
  45. end: end, // 根据数据计算的结束位置
  46. bottom: 10, // 距离底部的距离
  47. height: 5, // 设置高度
  48. },
  49. {
  50. type: 'inside', // 内置缩放条
  51. start: start,
  52. end: end,
  53. },
  54. ],
  55. calculable: true,
  56. // legend: {
  57. // // data: ["xxx"],
  58. // left: "left",s
  59. // x: "left",
  60. // top: "0%",
  61. // left: "2%",
  62. // icon: "roundRect",
  63. // itemWidth: 10, // 设置宽度
  64. // itemHeight: 10, // 设置高度
  65. // },
  66. color: ["#579AFF", "#B1B1F0", "#F7BEBE", '#BDF6E6'],
  67. grid: {
  68. top: "8%",
  69. bottom: "8%",
  70. left: "1%",
  71. right: "1%",
  72. containLabel: true,
  73. },
  74. xAxis: [
  75. {
  76. type: "category",
  77. data: xdata,
  78. axisTick: {
  79. show: false, // 不显示刻度
  80. },
  81. },
  82. ],
  83. yAxis: [
  84. {
  85. type: "value",
  86. },
  87. ],
  88. series: [
  89. {
  90. // name: "城市占比",
  91. type: "bar",
  92. data: ydata1,
  93. barWidth: "35%",
  94. itemStyle: {
  95. color: function (params) {
  96. // 判断是否为最上面的三个柱子,分别设置为红色、橘色、黄色
  97. if (params.dataIndex <= 2) {
  98. return params.dataIndex === 0 ? '#E96163' : params.dataIndex === 1 ? '#FEA935' : '#F0DC5A';
  99. } else {
  100. return '#D9D9D9'; // 设置其他柱子的颜色
  101. }
  102. }
  103. },
  104. },
  105. ],
  106. };
  107. machart.setOption(option);
  108. setTimeout(() => {
  109. window.addEventListener("resize", resizeFn);
  110. }, 100);
  111. const resizeFn = () => {
  112. return machart.resize();
  113. };
  114. machart.on("click", (params) => {
  115. if (params.componentType === "series") {
  116. const dataIndex = params.dataIndex;
  117. const yValue = xdata[dataIndex]; // 获取对应柱子的y值
  118. const cidtyid = xdataid[dataIndex]; // 获取对应柱子的y值
  119. emit("changedi", yValue, cidtyid);
  120. // 在这里你可以对获取到的y值进行其他操作,比如弹窗显示等
  121. }
  122. });
  123. };
  124. setTimeout(() => {
  125. initChart();
  126. }, 800);
  127. defineExpose({
  128. initChart,
  129. });
  130. onBeforeUnmount(() => {
  131. // 离开页面必须进行移除,否则会造成内存泄漏,导致卡顿
  132. window.removeEventListener("resize", initChart);
  133. });
  134. </script>
  135. <style scoped>
  136. #echarts8 {
  137. width: 100%;
  138. height: 95%;
  139. /* height: calc(30vh- 10px); */
  140. }
  141. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/742266
推荐阅读
相关标签
  

闽ICP备14008679号