Echarts title过长bug解决方案

ECharts 中的标题(title)默认是居中显示在图表顶部,并且如果标题过长超出了图表宽度,会被裁剪掉而不会自动换行或滚动显示。这个功能实现的底层源码主要涉及到 ECharts 渲染引擎和布局算法的实现。

具体来说,ECharts 中的渲染引擎使用了 Canvas 和 SVG 两种技术进行图表绘制。对于标题的绘制,ECharts 会先根据标题文字的长度和字体大小等参数计算出标题所占用的宽度和高度,然后根据图表的宽度和高度以及标题的位置等参数进行布局。如果标题宽度超出了图表宽度,ECharts 会将超出部分直接裁剪掉,不会自动换行或滚动显示。

如果需要让标题自动换行或滚动显示,可以通过修改 ECharts 的源码来实现。可以在 ECharts 的 text.js 文件中的 Text.prototype.drawBackground 方法进行修改,在该方法中添加相应的逻辑来实现标题文本的换行或滚动显示。例如,可以使用 CSS3 的 text-overflow 属性实现标题文本的省略号显示和鼠标悬停提示,或者使用 JavaScript 实现标题文本的滚动显示。

具体情况如下图所示:

解决方案一:设置title超出长度的内容换行展示

设置textStyle属性中overflow为break或breakAll

arduino 复制代码
title: {

    text: 'Temperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming Week',

    textStyle: {

      width: 200,

      overflow: 'breakAll' // 或break

    }

  },

具体展示效果如下:

带来的新问题:如果当title过长时,无论怎么设置标题的展示宽度,换行展示的文本仍要多行才能展示开,给用户的体验很差。

解决方案二:设置title以省略号填充,当鼠标mouseover时,展示全部内容

  1. 省略号填充

设置textStyle属性中overflow为truncate

arduino 复制代码
title: {

    text: 'Temperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming WeekTemperature Change in the Coming Week',

    textStyle: {

      width: 200,

      overflow: 'truncate' // 超出width宽度时,truncate为以省略号填充

    }

  },

效果如下所示:

  1. 当鼠标mouseover时,展示全部内容。

由于title属性无formatter自定义事件,所以只能通过图表实例对象上的getZr函数获取mouseover事件,判断移入标题时,展示完整标题DOM。(对应的鼠标事件大致有mousemove、click、dbclick、mouseover、mousedown、mouseup、globalout等)注:需要开启title的triggerEvent:true,表示支持标题开启事件处理

javascript 复制代码
chart.getZr().on('mouseover', function(params: Object)) {

    // 通过target判断移入的是否为对应的标题,这里可以打印出params.target对象进行自己查阅

    if(params.target && params.target.xxx) {

            // 展示DOM时,可以根据title的坐标点(params.target.offsetX,params.target.offsetY),调整展示DOM的位置
            // 此处不能return,因为该函数是void类型
            控制展示    <div>对应的完整标题的DOM结构</div>  

    }

}
  1. 当鼠标mouseout时,隐藏对应DOM
javascript 复制代码
chart.getZr().on('mouseout', function(params: Object)) {

    // 执行相应的隐藏操作

}

具体效果类似ElementPlus中tooltip组件

解决方案三:将title提出来,单独作为一个DOM

将title提出来,单独作为一个DOM,不放入Echarts的canvas结构中,搭配ElementPlus的tooltip组件实现超出长度悬浮展示,也可以单独用div标签实现。以下是单独用div标签实现逻辑。

css 复制代码
<div class="text">

    超出宽度的内容默认是用省略号填充,鼠标悬浮时,展示全部的内容

</div>

.text {

    width: 100px;

    overflow: hidden;

    white-space: nowrap;

    text-overflow: ellipsis;

}

.text:hover::after {

    position: absolute;

    content: '超出宽度的内容默认是用省略号填充,鼠标悬浮时,展示全部的内容';

    left: 0px;

    top: 0px;

}

效果如下:

如果还有其他实现方案,欢迎大家补充!

相关推荐
景早2 小时前
vue 记事本案例详解
前端·javascript·vue.js
乔冠宇3 小时前
vue需要学习的点
前端·vue.js·学习
老华带你飞4 小时前
商城推荐系统|基于SprinBoot+vue的商城推荐系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·毕设·商城推荐系统
一 乐4 小时前
物业管理系统|小区物业管理|基于SprinBoot+vue的小区物业管理系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端
H_HX1265 小时前
vue3 - 图片放大镜效果实现
前端·vue.js·vue3·vueuse·图片放大镜
梵得儿SHI8 小时前
Vue 模板语法深度解析:从文本插值到 HTML 渲染的核心逻辑
前端·vue.js·html·模板语法·文本插值·v-text指令·v-html指令
listhi5209 小时前
Vue.js 3的组合式API
android·vue.js·flutter
WYiQIU10 小时前
高级Web前端开发工程师2025年面试题总结及参考答案【含刷题资源库】
前端·vue.js·面试·职场和发展·前端框架·reactjs·飞书
夏之小星星10 小时前
Springboot结合Vue实现分页功能
vue.js·spring boot·后端
韩立学长10 小时前
【开题答辩实录分享】以《自动售货机刷脸支付系统的设计与实现》为例进行答辩实录分享
vue.js·spring boot·后端