0


vue2结合echarts实现数据排名列表——前端柱状进度条排行榜

写在前面,博主是个在北京打拼的码农,工作多年做过各类项目,最近心血来潮在这儿写点东西,欢迎大家多多指教。

数据排名列表——图表开发,动态柱状图表,排名图

UI

3debe8ac928c47f5b666fe9afae805ed.png

直接搜到类似在线代码(数据列表--bar - category-work,grid直角坐标,legend,series-bar柱状图,toolbox工具栏,tooltip提示框 - makeapie echarts社区图表可视化案例),准备在此基础上向UI靠拢

全部代码:

  1. enableChart() {
  2. let that = this;
  3. // 启用表格----重组数据格式
  4. this.tableData = JSON.parse(JSON.stringify(this.chartData));
  5. if (this.tableData && this.tableData.length) {
  6. // this.tableData.push(this.allNumObj);
  7. this.tableData = [...this.tableData, this.benjiObj, this.allNumObj];
  8. }
  9. this.$nextTick(function() {
  10. const elPart = document.querySelectorAll("tr");
  11. elPart.forEach(a => {
  12. a.children[2].style.color = "#fd5702";
  13. });
  14. });
  15. this.$forceUpdate();
  16. // if (["舆情办结率"].includes(this.typeName)) {
  17. // return;
  18. // // 舆情办结率没图表所以不刷新
  19. // }
  20. // if (["舆情办结率", "转办时效"].includes(this.typeName)) {
  21. // this.titleStr = "均值";
  22. // }else{
  23. // this.titleStr = "合计";
  24. // }
  25. // color: #fd5702;
  26. var myChart = echarts.init(document.getElementById("chartPart1"));
  27. var colorList = ["#FC0033", "#FD5702", "#FEB507", "#BBBBBB"];
  28. var datas = this.chartData.map(a => {
  29. return { value: a.nowInfoNum, name: a.addressName };
  30. });
  31. let junZhi = (
  32. this.chartData[0].nowInfoAllNum /
  33. (this.addressOption.length - 2)
  34. ).toFixed(2);
  35. if (this.loginUl.orgParentId != 0) {
  36. junZhi = (
  37. this.chartData[0].nowInfoAllNum /
  38. (this.addressOption.length - 1)
  39. ).toFixed(2);
  40. }
  41. if (this.loginUl.orgParentId != 7) {
  42. junZhi = (
  43. this.chartData[0].nowInfoAllNum / this.addressOption.length
  44. ).toFixed(2);
  45. }
  46. let maxArr = new Array(datas.length).fill(
  47. this.chartData[0] ? this.chartData[0].nowInfoAllNum : "暂无"
  48. );
  49. //合计值字段;
  50. let option = {
  51. tooltip: {
  52. trigger: "axis",
  53. axisPointer: {
  54. type: "none"
  55. },
  56. formatter: function(params, index) {
  57. return (
  58. params[0].name +
  59. ",位列第" +
  60. (params[0].dataIndex + 1) +
  61. "," +
  62. params[0].seriesName +
  63. params[0].value +
  64. "<br>均值:" +
  65. junZhi
  66. );
  67. }
  68. },
  69. legend: {
  70. show: false
  71. },
  72. grid: {
  73. top: 30, //图表距离容器下面多少距离
  74. containLabel: true
  75. },
  76. xAxis: {
  77. show: false,
  78. type: "value"
  79. },
  80. yAxis: [
  81. {
  82. type: "category",
  83. inverse: true,
  84. axisLine: {
  85. show: false
  86. },
  87. axisTick: {
  88. show: false
  89. },
  90. axisPointer: {
  91. label: {
  92. show: false,
  93. margin: 20
  94. }
  95. },
  96. // itemGap: 10,
  97. data: datas.map(item => item.name),
  98. axisLabel: {
  99. // width:2000, //网格宽度
  100. // height:'auto', //网格高度
  101. margin: 140,
  102. fontSize: 16,
  103. align: "left",
  104. color: "#333",
  105. // offset: 100, // 调整标签与刻度值的距离
  106. rich: {
  107. a1: {
  108. color: "#fff",
  109. backgroundColor: colorList[0],
  110. width: 30,
  111. height: 30,
  112. align: "center",
  113. borderRadius: 2
  114. },
  115. a2: {
  116. color: "#fff",
  117. backgroundColor: colorList[1],
  118. width: 30,
  119. height: 30,
  120. align: "center",
  121. borderRadius: 2
  122. },
  123. a3: {
  124. color: "#fff",
  125. backgroundColor: colorList[2],
  126. width: 30,
  127. height: 30,
  128. align: "center",
  129. borderRadius: 2
  130. },
  131. b: {
  132. color: "#fff",
  133. backgroundColor: colorList[3],
  134. width: 30,
  135. height: 30,
  136. align: "center",
  137. borderRadius: 2
  138. }
  139. },
  140. formatter: function(params) {
  141. var index = datas.map(item => item.name).indexOf(params);
  142. index = index + 1;
  143. let disposeText = params.length > 6 ? params + "..." : params;
  144. if (index - 1 < 3) {
  145. return [
  146. "{a" + index + "|" + index + "}" + " " + disposeText
  147. ].join("\n");
  148. } else {
  149. return ["{b|" + index + "}" + " " + disposeText].join("\n");
  150. }
  151. }
  152. }
  153. },
  154. {
  155. type: "category",
  156. inverse: true,
  157. axisTick: "none",
  158. axisLine: "none",
  159. show: true,
  160. data: datas.map(item => item.value),
  161. axisLabel: {
  162. show: false, //右侧百分比
  163. fontSize: 14,
  164. color: "#333",
  165. formatter: "{value}%"
  166. }
  167. }
  168. ],
  169. series: [
  170. {
  171. // left: 100,
  172. // right: 0,
  173. // top:0,
  174. // bottom:0,
  175. z: 2,
  176. name: this.typeName,
  177. type: "bar",
  178. barWidth: 20,
  179. zlevel: 1,
  180. data: datas.map((item, i) => {
  181. let colorStr = "";
  182. if (that.checkName === item.name) {
  183. colorStr = "#009b63";
  184. } else {
  185. if (i > 3) {
  186. colorStr = colorList[3];
  187. } else {
  188. colorStr = colorList[i];
  189. }
  190. }
  191. let itemStyle = {
  192. color: colorStr,
  193. barBorderRadius: 30
  194. };
  195. //设置选中的柱子颜色
  196. return {
  197. value: item.value,
  198. itemStyle: itemStyle
  199. };
  200. }),
  201. label: {
  202. show: false,
  203. position: "right",
  204. color: "#333333",
  205. fontSize: 14,
  206. offset: [10, 0]
  207. }
  208. },
  209. {
  210. name: "合计",
  211. type: "bar",
  212. barWidth: 20,
  213. barGap: "-100%",
  214. itemStyle: {
  215. normal: {
  216. color: "#EEEEEE",
  217. barBorderRadius: 30
  218. },
  219. emphasis: {
  220. // 高亮状态下的样式
  221. color: "#EEEEEE"
  222. }
  223. },
  224. data: maxArr
  225. }
  226. ]
  227. };
  228. var worldMapContainer = document.getElementById("chartPart1");
  229. //用于使chart自适应高度和宽度,通过外部元素高宽计算容器高宽 - 50
  230. worldMapContainer.style.width =
  231. document.getElementsByClassName("left")[0].clientWidth + 100 + "px";
  232. let H = 400;
  233. if (60 * this.chartData.length > 400) {
  234. H = 60 * this.chartData.length;
  235. }
  236. document.getElementsByClassName("left")[0].style.height = H + "px";
  237. worldMapContainer.style.height = H + "px";
  238. //设置容器高宽
  239. myChart.setOption(option);
  240. myChart.resize();
  241. this.$forceUpdate();
  242. },

调整echarts图的缩放大小

echarts如何改变图形的大小

echarts 放大与缩放的功能

echarts图表的大小调整的解决方案

echarts如何改变图形的大小

echarts的图表撑满整个容器 echarts图大小设置

那么怎么真真正正的解决上述几个问题呢

当我们直接拿别人二次开发过的echarts图,发现有点无从下手,搜索后也没发现有啥好属性,某处边距就是很宽看着难受

我的解决方法是

父元素相对定位,子元素绝对定位,直接调整子元素Echarts图的宽高来缩放图表,通过定位,让框与框的间距消失

改动前:

75b3e77ae884469c971bdfe2021d4d85.png

改动后:

fc726a470a6148f79c17097c180ac248.png

  1. .left {
  2. position: relative;
  3. .chartPartEl {
  4. position: absolute !important;
  5. top: 0;
  6. left: -90px;
  7. }
  8. }
  9. <div class="left" v-show="isUnfold">
  10. <template
  11. v-if="chartData && chartData.length && chartData[0].nowInfoAllNum"
  12. >
  13. <p><button class="green" @click="handleOk()">下载图片</button></p>
  14. <div
  15. class="chartPartEl"
  16. id="chartPart1"
  17. ref="chartPart1"
  18. style="width: 500px;height: 500px;"
  19. ></div>
  20. </template>
  21. <p v-else class="notData">暂无数据</p>
  22. </div>

最终效果:

b84c5fd631b841ad9dd539b3b0e2b4cd.png

5ee6cdc4241441d981e5559a7866abf5.png

后期改造:

加入均值线显示平均值

  1. // markLine: {
  2. // data: [
  3. // {
  4. // name: "平均值",
  5. // xAxis: junZhi
  6. // }
  7. // ]
  8. // }

全部代码:

  1. enableChart() {
  2. let that = this;
  3. // 启用表格----重组数据格式
  4. this.tableData = [];
  5. this.tableData = JSON.parse(JSON.stringify(this.chartData)) || [];
  6. if (
  7. (this.tableData && this.tableData.length) ||
  8. this.addressId == "0" ||
  9. this.addressId == 666 ||
  10. this.addressId == 123456
  11. ) {
  12. if (this.benjiObj) {
  13. this.tableData = [...this.tableData, this.benjiObj];
  14. }
  15. this.tableData = [...this.tableData, this.allNumObj];
  16. }
  17. this.$nextTick(function() {
  18. const elPart = document.querySelectorAll("tr");
  19. elPart.forEach(a => {
  20. a.children[2].style.color = "#fd5702";
  21. });
  22. });
  23. this.$forceUpdate();
  24. // if (["舆情办结率"].includes(this.typeName)) {
  25. // return;
  26. // // 舆情办结率没图表所以不刷新
  27. // }
  28. // if (["舆情办结率", "转办时效"].includes(this.typeName)) {
  29. // this.titleStr = "均值";
  30. // }else{
  31. // this.titleStr = "合计";
  32. // }
  33. // color: #fd5702;
  34. let myChart = "";
  35. if (!document.getElementById("chartPart1")) {
  36. return;
  37. }
  38. myChart = echarts.init(document.getElementById("chartPart1"));
  39. var colorList = ["#FC0033", "#FD5702", "#FEB507", "#BBBBBB"];
  40. var datas = this.chartData.map(a => {
  41. return { value: a.nowInfoNum, name: a.addressName };
  42. });
  43. let junZhi = (
  44. this.chartData[0].nowInfoAllNum /
  45. (this.addressOption.length - 2)
  46. ).toFixed(2);
  47. if (this.loginUl.orgParentId != 0) {
  48. // junZhi = (
  49. // this.chartData[0].nowInfoAllNum /
  50. // (this.addressOption.length - 1)
  51. // ).toFixed(2);
  52. junZhi = (
  53. this.chartData[0].nowInfoAllNum / this.chartData.length
  54. ).toFixed(2);
  55. }
  56. if (this.loginUl.orgParentId != 7) {
  57. // junZhi = (
  58. // this.chartData[0].nowInfoAllNum / this.addressOption.length
  59. // ).toFixed(2);
  60. junZhi = (
  61. this.chartData[0].nowInfoAllNum / this.chartData.length
  62. ).toFixed(2);
  63. }
  64. //特殊情况
  65. if (this.typeName == "舆情办结率" || this.typeName.includes("时效")) {
  66. junZhi = this.chartData[0].junZhi;
  67. }
  68. let maxArr = new Array(datas.length).fill(
  69. this.chartData[0] ? this.chartData[0].nowInfoAllNum : "暂无"
  70. );
  71. //合计值字段;
  72. let option = {
  73. tooltip: {
  74. trigger: "axis",
  75. axisPointer: {
  76. type: "none"
  77. },
  78. formatter: function(params, index) {
  79. return (
  80. params[0].name +
  81. ",位列第" +
  82. (params[0].dataIndex + 1) +
  83. "," +
  84. params[0].seriesName +
  85. params[0].value +
  86. "<br>均值:" +
  87. junZhi
  88. );
  89. }
  90. },
  91. legend: {
  92. show: false
  93. },
  94. grid: {
  95. top: 30, //图表距离容器下面多少距离
  96. containLabel: true
  97. },
  98. xAxis: {
  99. show: false,
  100. type: "value"
  101. },
  102. yAxis: [
  103. {
  104. type: "category",
  105. inverse: true,
  106. axisLine: {
  107. show: false
  108. },
  109. axisTick: {
  110. show: false
  111. },
  112. axisPointer: {
  113. label: {
  114. show: false,
  115. margin: 20
  116. }
  117. },
  118. // itemGap: 10,
  119. data: datas.map(item => item.name),
  120. axisLabel: {
  121. // width:2000, //网格宽度
  122. // height:'auto', //网格高度
  123. margin: 140,
  124. fontSize: 16,
  125. align: "left",
  126. color: "#333",
  127. // offset: 100, // 调整标签与刻度值的距离
  128. rich: {
  129. a1: {
  130. color: "#fff",
  131. backgroundColor: colorList[0],
  132. width: 30,
  133. height: 30,
  134. align: "center",
  135. borderRadius: 2
  136. },
  137. a2: {
  138. color: "#fff",
  139. backgroundColor: colorList[1],
  140. width: 30,
  141. height: 30,
  142. align: "center",
  143. borderRadius: 2
  144. },
  145. a3: {
  146. color: "#fff",
  147. backgroundColor: colorList[2],
  148. width: 30,
  149. height: 30,
  150. align: "center",
  151. borderRadius: 2
  152. },
  153. b: {
  154. color: "#fff",
  155. backgroundColor: colorList[3],
  156. width: 30,
  157. height: 30,
  158. align: "center",
  159. borderRadius: 2
  160. }
  161. },
  162. formatter: function(params) {
  163. var index = datas.map(item => item.name).indexOf(params);
  164. index = index + 1;
  165. let disposeText =
  166. params.length > 5 ? params.slice(0, 5) + "..." : params;
  167. if (index - 1 < 3) {
  168. return [
  169. "{a" + index + "|" + index + "}" + " " + disposeText
  170. ].join("\n");
  171. } else {
  172. return ["{b|" + index + "}" + " " + disposeText].join("\n");
  173. }
  174. }
  175. }
  176. },
  177. {
  178. type: "category",
  179. inverse: true,
  180. axisTick: "none",
  181. axisLine: "none",
  182. show: true,
  183. data: datas.map(item => item.value),
  184. axisLabel: {
  185. show: false, //右侧百分比
  186. fontSize: 14,
  187. color: "#333",
  188. formatter: "{value}%"
  189. }
  190. }
  191. ],
  192. series: [
  193. {
  194. // left: 100,
  195. // right: 0,
  196. // top:0,
  197. // bottom:0,
  198. z: 2,
  199. name: this.typeName,
  200. type: "bar",
  201. barWidth: 20,
  202. zlevel: 1,
  203. data: datas.map((item, i) => {
  204. let colorStr = "";
  205. if (that.checkName === item.name) {
  206. colorStr = "#009b63";
  207. } else {
  208. if (i > 3) {
  209. colorStr = colorList[3];
  210. } else {
  211. colorStr = colorList[i];
  212. }
  213. }
  214. let itemStyle = {
  215. color: colorStr,
  216. barBorderRadius: 30
  217. };
  218. //设置选中的柱子颜色
  219. return {
  220. value: item.value,
  221. itemStyle: itemStyle
  222. };
  223. }),
  224. label: {
  225. show: false,
  226. position: "right",
  227. color: "#333333",
  228. fontSize: 14,
  229. offset: [10, 0]
  230. },
  231. // markLine: {
  232. // data: [
  233. // {
  234. // name: "平均值",
  235. // xAxis: junZhi
  236. // }
  237. // ]
  238. // }
  239. },
  240. {
  241. name: "合计",
  242. type: "bar",
  243. barWidth: 20,
  244. barGap: "-100%",
  245. itemStyle: {
  246. normal: {
  247. color: "#EEEEEE",
  248. barBorderRadius: 30
  249. },
  250. emphasis: {
  251. // 高亮状态下的样式
  252. color: "#EEEEEE"
  253. }
  254. },
  255. data: maxArr,
  256. // markLine: {
  257. // data: [
  258. // {
  259. // name: "平均值",
  260. // xAxis: junZhi
  261. // }
  262. // ]
  263. // }
  264. }
  265. ]
  266. };
  267. var worldMapContainer = document.getElementById("chartPart1");
  268. //用于使chart自适应高度和宽度,通过外部元素高宽计算容器高宽 - 50
  269. worldMapContainer.style.width =
  270. document.getElementsByClassName("left")[0].clientWidth + 100 + "px";
  271. let H = 400;
  272. if (60 * this.chartData.length > 400) {
  273. H = 60 * this.chartData.length;
  274. }
  275. document.getElementsByClassName("left")[0].style.height = H + "px";
  276. worldMapContainer.style.height = H + "px";
  277. //设置容器高宽
  278. myChart.setOption(option);
  279. myChart.resize();
  280. this.$forceUpdate();
  281. },

本次开发中碰到的有意思的文章:

**1.**ECharts 高度宽度自适应(转载) - hao_1234_1234 - 博客园

  1. var worldMapContainer = document.getElementById("chartPart1");
  2. //用于使chart自适应高度和宽度,通过外部元素高宽计算容器高宽
  3. var resizeWorldMapContainer = function() {
  4. worldMapContainer.style.width =
  5. document.getElementsByClassName("left")[0].clientWidth - 100 + "px";
  6. worldMapContainer.style.height =
  7. document.getElementsByClassName("content")[0].clientWidth + "px";
  8. }; //设置容器高宽
  9. resizeWorldMapContainer();
  10. myChart.setOption(option);
  11. myChart.resize();

2.4361476db20642b9a701a8bbdfb6549e.png

3.javaScript实用方法第三篇(.fill .filter .find)_js .fill-CSDN博客

4.Error in callback for watcher “options“: “TypeError:Cannot read properties of null (reading ‘level‘)_error in callback for watcher "options": "typeerro-CSDN博客

5.4727e943c56b42489b7575efa74cd7e3.png 解决前代码:根据父元素可视高度实现

  1. //用于使chart自适应高度和宽度,通过外部元素高宽计算容器高宽
  2. var resizeWorldMapContainer = function() {
  3. worldMapContainer.style.width =
  4. document.getElementsByClassName("left")[0].clientWidth - 100 + "px";
  5. worldMapContainer.style.height =
  6. document.getElementsByClassName("content")[0].clientWidth + "px";
  7. }; //设置容器高宽
  8. resizeWorldMapContainer();

解决后代码:根据数据数量多少决定

  1. var worldMapContainer = document.getElementById("chartPart1");
  2. //用于使chart自适应高度和宽度,通过外部元素高宽计算容器高宽
  3. // var resizeWorldMapContainer = function() {
  4. worldMapContainer.style.width =
  5. document.getElementsByClassName("left")[0].clientWidth - 100 + "px";
  6. let H=400;
  7. if(60*this.chartData.length>400){
  8. H=60*this.chartData.length;
  9. }
  10. worldMapContainer.style.height =H+'px';
  11. // document.getElementsByClassName("left")[0].clientHeight + "px";
  12. // }; //设置容器高宽
  13. // resizeWorldMapContainer();

6.b4c48ba754d04681a383bfe26c079dce.png

7.官网搜索,很多二次开发的不符合我们要求的图表,我们引用后进行第三次改造,就要经常去官方文档搜索关键词了

标签: echarts vue.js 前端

本文转载自: https://blog.csdn.net/aZHANGJIANZHENa/article/details/142878003
版权归原作者 JianZhen✓ 所有, 如有侵权,请联系我们删除。

“vue2结合echarts实现数据排名列表——前端柱状进度条排行榜”的评论:

还没有评论