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

news2024/10/27 22:11:22

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

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

UI

3debe8ac928c47f5b666fe9afae805ed.png

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

全部代码:

 enableChart() {
      let that = this;
      // 启用表格----重组数据格式

      this.tableData = JSON.parse(JSON.stringify(this.chartData));
      if (this.tableData && this.tableData.length) {
        // this.tableData.push(this.allNumObj);
        this.tableData = [...this.tableData, this.benjiObj, this.allNumObj];
      }

      this.$nextTick(function() {
        const elPart = document.querySelectorAll("tr");
        elPart.forEach(a => {
          a.children[2].style.color = "#fd5702";
        });
      });

      this.$forceUpdate();
      //       if (["舆情办结率"].includes(this.typeName)) {
      // return;
      // // 舆情办结率没图表所以不刷新
      //             }
      //  if (["舆情办结率", "转办时效"].includes(this.typeName)) {
      //         this.titleStr = "均值";
      //       }else{
      //         this.titleStr = "合计";

      //       }
      // color: #fd5702;
      var myChart = echarts.init(document.getElementById("chartPart1"));
      var colorList = ["#FC0033", "#FD5702", "#FEB507", "#BBBBBB"];
      var datas = this.chartData.map(a => {
        return { value: a.nowInfoNum, name: a.addressName };
      });
      let junZhi = (
        this.chartData[0].nowInfoAllNum /
        (this.addressOption.length - 2)
      ).toFixed(2);
      if (this.loginUl.orgParentId != 0) {
        junZhi = (
          this.chartData[0].nowInfoAllNum /
          (this.addressOption.length - 1)
        ).toFixed(2);
      }
      if (this.loginUl.orgParentId != 7) {
        junZhi = (
          this.chartData[0].nowInfoAllNum / this.addressOption.length
        ).toFixed(2);
      }
      let maxArr = new Array(datas.length).fill(
        this.chartData[0] ? this.chartData[0].nowInfoAllNum : "暂无"
      );
      //合计值字段;

      let option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "none"
          },
          formatter: function(params, index) {
            return (
              params[0].name +
              ",位列第" +
              (params[0].dataIndex + 1) +
              "," +
              params[0].seriesName +
              params[0].value +
              "<br>均值:" +
              junZhi
            );
          }
        },
        legend: {
          show: false
        },
        grid: {
          top: 30, //图表距离容器下面多少距离

          containLabel: true
        },

        xAxis: {
          show: false,
          type: "value"
        },
        yAxis: [
          {
            type: "category",

            inverse: true,
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            axisPointer: {
              label: {
                show: false,
                margin: 20
              }
            },
            // itemGap: 10,
            data: datas.map(item => item.name),
            axisLabel: {
              // width:2000,    //网格宽度
              // height:'auto',   //网格高度
              margin: 140,
              fontSize: 16,
              align: "left",
              color: "#333",
              // offset: 100,  // 调整标签与刻度值的距离
              rich: {
                a1: {
                  color: "#fff",
                  backgroundColor: colorList[0],
                  width: 30,
                  height: 30,
                  align: "center",
                  borderRadius: 2
                },
                a2: {
                  color: "#fff",
                  backgroundColor: colorList[1],
                  width: 30,
                  height: 30,
                  align: "center",
                  borderRadius: 2
                },
                a3: {
                  color: "#fff",
                  backgroundColor: colorList[2],
                  width: 30,
                  height: 30,
                  align: "center",
                  borderRadius: 2
                },
                b: {
                  color: "#fff",
                  backgroundColor: colorList[3],
                  width: 30,
                  height: 30,
                  align: "center",
                  borderRadius: 2
                }
              },
              formatter: function(params) {
                var index = datas.map(item => item.name).indexOf(params);

                index = index + 1;

                let disposeText = params.length > 6 ? params + "..." : params;
                if (index - 1 < 3) {
                  return [
                    "{a" + index + "|" + index + "}" + "  " + disposeText
                  ].join("\n");
                } else {
                  return ["{b|" + index + "}" + "  " + disposeText].join("\n");
                }
              }
            }
          },
          {
            type: "category",
            inverse: true,
            axisTick: "none",
            axisLine: "none",
            show: true,
            data: datas.map(item => item.value),
            axisLabel: {
              show: false, //右侧百分比
              fontSize: 14,
              color: "#333",
              formatter: "{value}%"
            }
          }
        ],
        series: [
          {
            //   left: 100,
            // right: 0,
            // top:0,
            // bottom:0,
            z: 2,
            name: this.typeName,
            type: "bar",
            barWidth: 20,
            zlevel: 1,
            data: datas.map((item, i) => {
              let colorStr = "";
              if (that.checkName === item.name) {
                colorStr = "#009b63";
              } else {
                if (i > 3) {
                  colorStr = colorList[3];
                } else {
                  colorStr = colorList[i];
                }
              }
              let itemStyle = {
                color: colorStr,
                barBorderRadius: 30
              };
              //设置选中的柱子颜色
              return {
                value: item.value,
                itemStyle: itemStyle
              };
            }),
            label: {
              show: false,
              position: "right",
              color: "#333333",
              fontSize: 14,
              offset: [10, 0]
            }
          },
          {
            name: "合计",
            type: "bar",
            barWidth: 20,
            barGap: "-100%",
            itemStyle: {
              normal: {
                color: "#EEEEEE",
                barBorderRadius: 30
              },
              emphasis: {
                // 高亮状态下的样式
                color: "#EEEEEE"
              }
            },
            data: maxArr
          }
        ]
      };

      var worldMapContainer = document.getElementById("chartPart1");
      //用于使chart自适应高度和宽度,通过外部元素高宽计算容器高宽 - 50
      worldMapContainer.style.width =
        document.getElementsByClassName("left")[0].clientWidth + 100 + "px";
      let H = 400;
      if (60 * this.chartData.length > 400) {
        H = 60 * this.chartData.length;
      }
      document.getElementsByClassName("left")[0].style.height = H + "px";
      worldMapContainer.style.height = H + "px";
      //设置容器高宽
      myChart.setOption(option);
      myChart.resize();
      this.$forceUpdate();
    },

调整echarts图的缩放大小

echarts如何改变图形的大小

echarts 放大与缩放的功能

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

echarts如何改变图形的大小

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

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

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

我的解决方法是

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

改动前:

75b3e77ae884469c971bdfe2021d4d85.png

 

改动后:

fc726a470a6148f79c17097c180ac248.png



  .left {
   
    position: relative;
.chartPartEl {
  position: absolute !important;
  top: 0;
  left: -90px;
}
  }



 <div class="left" v-show="isUnfold">
          <template
            v-if="chartData && chartData.length && chartData[0].nowInfoAllNum"
          >
            <p><button class="green" @click="handleOk()">下载图片</button></p>
            <div
              class="chartPartEl"
              id="chartPart1"
              ref="chartPart1"
              style="width: 500px;height: 500px;"
            ></div>
          </template>
          <p v-else class="notData">暂无数据</p>
        </div>

 

最终效果:

b84c5fd631b841ad9dd539b3b0e2b4cd.png

5ee6cdc4241441d981e5559a7866abf5.png

 

 

 

 

 

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

 

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

  var worldMapContainer = document.getElementById("chartPart1");

      //用于使chart自适应高度和宽度,通过外部元素高宽计算容器高宽
      var resizeWorldMapContainer = function() {
        worldMapContainer.style.width =
          document.getElementsByClassName("left")[0].clientWidth - 100 + "px";
        worldMapContainer.style.height =
          document.getElementsByClassName("content")[0].clientWidth + "px";
      }; //设置容器高宽
      resizeWorldMapContainer();
      myChart.setOption(option);
      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 解决前代码:根据父元素可视高度实现


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

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

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

6.b4c48ba754d04681a383bfe26c079dce.png

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2224966.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

事务的原理、MVCC的原理

事务特性 数据库事务具有以下四个基本特性&#xff0c;通常被称为 ACID 特性&#xff1a; 原子性&#xff08;Atomicity&#xff09;&#xff1a;事务被视为不可分割的最小工作单元&#xff0c;要么全部执行成功&#xff0c;要么全部失败回滚。这意味着如果事务执行过程中发生…

交换机:端口安全与访问控制指南

为了实现端口安全和访问控制&#xff0c;交换机通常通过以下几种机制和配置来保护网络&#xff0c;防止未经授权的访问和恶意攻击。 01-端口安全 定义及功能 端口安全功能允许管理员限制每个交换机端口可以学习的MAC地址数量。 通过绑定特定的MAC地址到交换机的某一端口上&a…

二十二、Python基础语法(模块)

模块(module)&#xff1a;在python中&#xff0c;每个代码文件就是一个模块&#xff0c;在模块中定义的变量、函数、类别人都可以直接使用&#xff0c;如果想要使用别人写好的模块&#xff0c;就必须先导入别人的模块&#xff0c;模块名须满足标识符规则&#xff08;由字母、数…

MFC七段码显示实例

在MFC中添加iSenvenSegmentAnalogX控件&#xff0c;添加编辑框和按钮实现在编辑框中输入数字点击按钮后数字用七段码显示 1、在对话框中点击右键如下图添加控件和变量 2、在sevenDlg.h中添加代码 public: void ShowInd(int,double);3、在sevenDlg.cpp中添加代码 void CSe…

将 el-date-picker获取的时间数据转换成时间戳

在Vue.js中使用Element UI的el-date-picker组件时&#xff0c;你可以获取用户选择的日期并将其转换为时间戳。el-date-picker通常返回的是一个Date对象或一个格式化后的字符串&#xff08;取决于你如何配置它&#xff09;。下面是一个示例&#xff0c;展示了如何将el-date-pick…

攻防世界的新手web题解

攻防世界引导模式 1、disabled_button 好&#xff0c;给了一个按钮&#xff0c;第一道题目就不会做 看的wp<input disabled class"btn btn-default" style"height:50px;width:200px;" type"submit" value"flag" name"auth&q…

来源爬虫程序调研报告

来源爬虫程序调研报告 一、什么是爬虫 爬虫&#xff1a;就是抓取网页数据的程序。从网站某一个页面&#xff08;通常是首页&#xff09;开始&#xff0c;读取网页的内容&#xff0c;找到在网页中的其它链接地址&#xff0c;然后通过这些链接地址寻找下一个网页&#xff0c;这…

植物健康,Spring Boot来保障

5系统详细实现 5.1 系统首页 植物健康系统需要登录才可以看到首页。具体界面的展示如图5.1所示。 图5.1 系统首页界面 5.2 咨询专家 可以在咨询专家栏目发布消息。具体界面如图5.2所示。 图5.2 咨询专家界面 5.3 普通植物检查登记 普通员工可以对普通植物检查登记信息进行添…

07 设计模式-结构型模式-桥接模式

桥接&#xff08;Bridge&#xff09;是用于把抽象化与实现化解耦&#xff0c;使得二者可以独立变化。这种类型的设计模式属于结构型模式&#xff0c;它通过提供抽象化和实现化之间的桥接结构&#xff0c;来实现二者的解耦。 这种模式涉及到一个作为桥接的接口&#xff0c;使得…

入门 | Prometheus+Grafana 普罗米修斯

#1024程序员节&#xff5c;征文# 一、prometheus介绍 1、监控系统组成 一个完整的监控系统需要包括如下功能&#xff1a;数据产生、数据采集、数据存储、数据处理、数据展示、分析、告警等。 &#xff08;1&#xff09;、数据来源 数据来源&#xff0c;也就是需要监控的数据…

VS Code 自动生成代码

1. 在vs code中的左下角&#xff0c;点击设置中的snippets。 2. 输入你需要生成的代码种类&#xff0c;这边以JS为例 打开后可以看到下面这样 从Example看起&#xff0c; 1.Print to console 这个是提升信息&#xff0c;就是当你输入代码的时候的提升。 2.prefix是缩写。 3.b…

python爬虫——Selenium的基本使用

目录 一、Selenium的介绍 二、环境准备 1.安装Selenium 2.安装WebDriver 三、元素定位 1.常用定位元素的方法 2. 通过指定方式定位元素 四、窗口操作 1.最大化浏览器窗口 2.设置浏览器窗口大小 3.切换窗口或标签页 切换回主窗口 4. 关闭窗口 关闭当前窗口 关闭所…

由于找不到mfc140u.dll,无法继续执行代码怎么办,总有6个解决方法

在软件开发和程序运行过程中&#xff0c;许多用户可能会遇到“找不到mfc140u.dll&#xff0c;无法继续执行代码”的问题。本文将对该问题进行详细解读&#xff0c;分析其产生原因&#xff0c;并提供相应的解决方案。 一、mfc140u.dll是什么 mfc140u.dll是Microsoft Visual C 2…

apache poi导出excel

简介 常见的使用场景 入门 导入maven依赖 <!-- poi --> <dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId> </dependency> <dependency><groupId>org.apache.poi</groupId><arti…

Java Web项目实战:从零基础到项目开发全流程

Java Web开发环境搭建 Java Web开发需要Java运行环境、IDE&#xff08;如IntelliJ IDEA或Eclipse&#xff09;以及Maven集成开发工具等。首先&#xff0c;确保电脑上安装有Java 8或更高版本&#xff0c;可以通过访问Java官网获取最新版本。其次&#xff0c;安装IDE&#xff0c…

jupyter notebook改变默认启动路径

安装好Anaconda 3以后&#xff0c;就可以使用Jupyter notebook了&#xff0c;但是我们打开Jupyter notebook后&#xff0c;发现界面是一个默认的目录&#xff0c;这个目录在哪里&#xff1f;如果想把自己写的程序文件保存在自己新建的一个文件夹里&#xff0c;修改默认目录到自…

vue elementui el-table实现增加行,行内编辑修改

需求&#xff1a; 前端进行新增表单时&#xff0c;同时增加表单的明细数据。明细数据部分&#xff0c;可进行行编辑。 效果图&#xff1a; <el-card><div slot"header"><span style"font-weight: bold">外来人员名单2</span><…

MySQL8.0.40编译安装

近期MySQL发布了8.0.40版本&#xff0c;与之前的版本相比&#xff0c;部分依赖包发生了变化&#xff0c;因此重新编译一版&#xff0c;也便于大家参考。 1. 下载源码 选择对应的版本、选择源码、操作系统 如果没有登录或者没有MySQL官网账号&#xff0c;可以选择只下载 2. 进…

Flutter仿京东商城APP实战 用户中心基础布局

用户中心界面 pages/tabs/user/user.dart import package:flutter/material.dart; import package:jdshop/utils/zdp_screen.dart; import package:provider/provider.dart;import ../../../store/counter_store.dart;class UserPage extends StatefulWidget {const UserPage…

自动化部署-02-jenkins部署微服务

文章目录 前言一、配置SSH-KEY1.1 操作jenkins所在服务器1.2 操作github1.3 验证 二、服务器安装git三、jenkins页面安装maven四、页面配置自动化任务4.1 新建任务4.2 选择4.3 配置参数4.4 配置脚本 五、执行任务5.1 点击执行按钮5.2 填写参数5.3 查看日志 六、查看服务器文件七…