当前位置: 首页 > news >正文

echarts图表 实现高度按照 内容撑起来或者超出部分滚动展示效果

背景:因为数据不固定 高度写死导致数据显示不全,所以图表高度要根据内容计算
实现代码如下:

 <divv-if="showCharts"id="business-bars"class="chart":style="{ height: chartHeight + 'px' }"></div>``````data() {return {chartHeight: 0,deptData: {},yAxisData: [], // 用于y轴的数据.......//其它数据}}``````watch: {yAxisData(newData) {this.chartHeight = newData.length * 30 + 20;this.$nextTick(() => {this.drawChart();const chart = echarts.init(document.getElementById("business-bars"));chart.resize()});},},
 mounted() {this.init(); // 初始化图表数据this.$nextTick(() => {//this.drawChart(); // 绘制图表});
},

思路:通过监听’yAxisData’的变化,来动态地计算和调整图表的整体高度
重点:1、在 Watch 属性的回调函数内调用了 drawChart() 方法,确保图表的高度在动态计算后被正确应用,
在 Watch 属性中通过 this.$nextTick() 来调用 drawChart() 方法,以确保在图表高度更新后再进行绘制操作

watch: {yAxisData(newData) {this.chartHeight = newData.length * 17 + 20;this.$nextTick(() => {this.drawChart();const chart = echarts.init(document.getElementById("business-bars"));chart.resize()});}
},

2、在 drawChart() 方法中,确保在绘制图表之前,已经将图表容器的实际高度设置为计算后的高度

methods: {drawChart() {const chart = echarts.init(document.getElementById("business-bars"));const chartContainer = document.getElementById("business-bars");chartContainer.style.height = this.chartHeight + "px";const option = {barWidth: 20,barGap: "30%", // 调整同一类目上的柱子间隔barCategoryGap: "20%", // 调整不同类目的柱子间隔// dataZoom: [ //   {//     type: 'slider',//     yAxisIndex: 0, // y轴索引//     startValue: 0, // 起始数据索引//     endValue: 5,  // 结束数据索引//     zoomLock: true // 禁用滑动条//   }// ],color: ["rgba(27, 158, 255, 1)","rgba(0, 210, 233, 1)","rgba(253, 200, 79, 1)",],tooltip: {trigger: "axis",axisPointer: {type: "shadow",},},legend: {bottom: 20,itemGap: 15,itemWidth: 10,itemHeight: 10,},grid: {left: "2%",right: "4%",top: "2%",containLabel: true,},xAxis: {type: "value",splitLine: {show: false,},},yAxis: {type: "category",data: this.yAxisData,axisLine: {show: false,},axisTick: {show: false,interval: 0,},axisLabel: {interval: 0,formatter: function (value) {if (value.length > 5) {return `${value.substr(0, 5)}...`;}return value;},},},series: this.seriesData,};const that = this;chart.off("click");chart.on("click", function (param) {const index = PROJECT_STATE_ENUM.findIndex((item) => item.name === param.seriesName);that.value = param.seriesName;that.dept = param.name;if (index > -1) {let time;if (that.current === "all") {time = 0;} else if (that.current === "week") {time = 1;} else if (that.current === "month") {time = 2;} else if (that.current === "year") {time = 3;}that.$emit("query", {value: PROJECT_STATE_ENUM[index].code,time: time,dept: param.name,});}});chart.setOption(option);// 自适应窗口大小window.addEventListener("resize", function () {chart.resize();});},
}

实现效果如下:按照内容高度 撑开全部展示
在这里插入图片描述
在这里插入图片描述
如果左侧高度想固定 超出部分右侧滚动条上下滚动的形式展示 则在echarts配置中添加datazoom属性

  // dataZoom: [//   {//     type: 'slider',//     yAxisIndex: 0, // y轴索引//     startValue: 0, // 起始数据索引//     endValue: 5,  // 结束数据索引//     zoomLock: true // 禁用滑动条//   }// ],

展示效果如下:
在这里插入图片描述

http://www.lryc.cn/news/172667.html

相关文章:

  • 【论文阅读】检索增强发展历程及相关文章总结
  • 【漏洞复现系列】二、weblogic-cve_2020_2883(RCE/反序列化)
  • 算法通关村-----LRU的设计与实现
  • 王江涛十天搞定考研词汇
  • 算法(二)——数组章节和链表章节
  • Android:ListView在Fragment中的使用
  • BIGEMAP在土地规划中的应用
  • 软件测试常见术语和名词解释
  • prometheus+process_exporter进程监控
  • 四川玖璨电子商务有限公司专注抖音电商运营
  • python LeetCode 刷题记录 83
  • Grom 如何解决 SQL 注入问题
  • 腾讯mini项目-【指标监控服务重构】2023-07-19
  • 抖音矩阵系统源代码开发部署--SaaS开源技术开发文档
  • CLIP模型资料学习
  • 【c语言】贪吃蛇
  • 【Node.js】定时任务cron:
  • vue3 引入element-plus
  • 数据通信——传输层TCP(超时时间选择)
  • 【数据库索引优化】
  • WebGL 选中物体
  • 科目二倒车入库
  • PostgreSQL如何支持PL/Python过程语言
  • 【C++】STL之适配器---用deque实现栈和队列
  • PHY6230低成本遥控灯控芯片国产蓝牙BLE5.2 2.4G SoC
  • OceanBase杨传辉传递亚运火炬:国产数据库为“智能亚运”提供稳稳支持
  • 分布式锁实现方法
  • 软件测试缺陷报告详解
  • pytorch冻结参数训练的坑
  • P1827 [USACO3.4] 美国血统 American Heritage(前序 + 中序 生成后序)