echarts x轴 axisLabel 超出两行或多行省略显示...

文章介绍了在echarts中处理x轴标签超出两行的场景,由于不能使用HTML模板,故通过canvas的context.measureText()方法来精确计算文字宽度,进而动态计算最后一个能完全显示的字符索引,实现文本的截断和省略号添加。提供了完整的JavaScript代码示例,包括计算文本宽度、找到截断点、以及生成显示文本的函数。
摘要由CSDN通过智能技术生成

需求背景   

     echarts x轴标签超出两行后显示...

需求分析

     1.配置项不能使用html模板,因此不能通过css去实现

     2.文字、字母、数字符号占位宽度不一样,也不能简单通过字符长度去计算要显示的内容

效果展示

实现方案

    通过canvas中context.measureText()方法准确计算文字的占位宽度

代码解释

1.测量文本宽度核心方法

   // 返回一个能够计算文本像素的方法  防止重复创建canvas
const useContext = (fontStyle) => {
    const canvas = document.createElement('canvas')
    const context = canvas.getContext('2d')
    context.font = fontStyle
    const getTextWidth = (text) => {
        const width = context.measureText(text).width
        return width
    }
    return getTextWidth
}

const getTextWidth = useContext(fontStyle)

2.给定文字和宽度 计算最后一个不能完整显示字符的索引

 // 计算一段文本能够容纳文本的最后一个索引
 const getClipIndex = (text, maxWidth) => {
    const textLenth = text.length
    const width = getTextWidth(text)
    if (width <= maxWidth) {
        return -1 // 没有超出
    }
    if (maxWidth < fontSize) {
        return 0 // 给定宽度比字号还小 不计算
    }

    let resultIndex // 查找停止的表示  返回出去的索引
    const indexs = [0, textLenth - 1]  //被切断文字的索引范围
    while (!resultIndex) {
        const index = indexs[0] + Math.floor((indexs[1] - indexs[0]) / 2) //二分法中点
        const currentW = getTextWidth(text.slice(0, index + 1)) // 中点文字开始像素
        const lastW = getTextWidth(text.slice(0, index)) //中点文字结束像素
        if (currentW > maxWidth && lastW <= maxWidth) { // 给定宽度在文字所占像素范围内
            resultIndex = index //第一个不能完整展示的文字索引
        } else if (lastW > maxWidth && currentW > maxWidth) { //中点文字在给定宽度右侧(之外) 
            indexs[1] = index
        } else if (lastW < maxWidth && currentW < maxWidth) { // 中点文字在给定宽度左侧(需往右查找)
            indexs[0] = index + 1
        }

    }
    return resultIndex
}

3.用...代替隐藏的文本,也可以换成其他字符代替

 // 用...代替被隐藏的文字
 const toEllipsis = (text) => {
    const threeDotWidth = getTextWidth("...")
    let letterNum = 1

    while (threeDotWidth > getTextWidth(text.slice(-letterNum))) {
        letterNum++
    }
    return text.slice(0, -letterNum) + "..."
}

4.返回实际显示文本,可设定行数

 // 给出行宽和最大行数  输出要展示的文本
 const getLastClipByRow = (text, rows, maxWidth) => {
    let startIndex = 0
    for (let index = 0; index < rows; index++) {
        const rowText = text.slice(startIndex)
        const currentRowLastIndex = getClipIndex(rowText, maxWidth)
        if (currentRowLastIndex > 0) {
            startIndex += currentRowLastIndex
        } else {
            break
        }
    }

    if (startIndex > text.lenth) {
        return text
    } else {
    }

    const rowsText = text.slice(0, startIndex)
    return toEllipsis(rowsText)
}

5.整体demo代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>


<style>
    .text1,
    .text2 {
        font-size: 14px;
        width: fit-content;
        max-width: 100px;
        word-break: break-all;
        background-color: greenyellow;
    }

    .text1 {
        display: -webkit-box;
        text-overflow: ellipsis;
        overflow: hidden;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 3;
    }
</style>

<body>
    <div id="app">
        <h3>使用css样式的超出隐藏</h3>
        <div class="text1">{{testText}}</div>
        <br>
        <h3>计算出来不用css样式的超出隐藏</h3>
        <div class="text2">{{showLetters}}</div>
    </div>
    <script>
        const { createApp, onMounted, ref } = Vue
        const app = createApp({

            setup() {
                const testText = "一a跨1(2境d电商m房,间|爱a了快点)解封价一a跨1(2境d电商m房,间|爱间啊"
                const fontSize = 14
                const fontStyle = `${fontSize}px Microsoft YaHei`

                // 返回一个能够计算文本像素的方法  防止重复创建canvas
                const useContext = (fontStyle) => {
                    const canvas = document.createElement('canvas')
                    const context = canvas.getContext('2d')
                    context.font = fontStyle
                    const getTextWidth = (text) => {
                        const width = context.measureText(text).width
                        return width
                    }
                    return getTextWidth
                }

                const getTextWidth = useContext(fontStyle)


                // 计算一段文本能够容纳文本的最后一个索引
                const getClipIndex = (text, maxWidth) => {
                    const textLenth = text.length
                    const width = getTextWidth(text)
                    if (width <= maxWidth) {
                        return -1 // 没有超出
                    }
                    if (maxWidth < fontSize) {
                        return 0 // 给定宽度比字号还小 不计算
                    }

                    let resultIndex // 查找停止的表示  返回出去的索引
                    const indexs = [0, textLenth - 1]  //被切断文字的索引范围
                    while (!resultIndex) {
                        const index = indexs[0] + Math.floor((indexs[1] - indexs[0]) / 2) //二分法中点
                        const currentW = getTextWidth(text.slice(0, index + 1)) // 中点文字开始像素
                        const lastW = getTextWidth(text.slice(0, index)) //中点文字结束像素
                        if (currentW > maxWidth && lastW <= maxWidth) { // 给定宽度在文字所占像素范围内
                            resultIndex = index //第一个不能完整展示的文字索引
                        } else if (lastW > maxWidth && currentW > maxWidth) { //中点文字在给定宽度右侧(之外) 
                            indexs[1] = index
                        } else if (lastW < maxWidth && currentW < maxWidth) { // 中点文字在给定宽度左侧(需往右查找)
                            indexs[0] = index + 1
                        }

                    }
                    return resultIndex
                }

                // 用...代替被隐藏的文字
                const toEllipsis = (text) => {
                    const threeDotWidth = getTextWidth("...")
                    let letterNum = 1

                    while (threeDotWidth > getTextWidth(text.slice(-letterNum))) {
                        letterNum++
                    }
                    return text.slice(0, -letterNum) + "..."
                }


                // 给出行宽和最大行数  输出要展示的文本
                const getLastClipByRow = (text, rows, maxWidth) => {
                    let startIndex = 0
                    for (let index = 0; index < rows; index++) {
                        const rowText = text.slice(startIndex)
                        const currentRowLastIndex = getClipIndex(rowText, maxWidth)
                        if (currentRowLastIndex > 0) {
                            startIndex += currentRowLastIndex
                        } else {
                            break
                        }
                    }

                    if (startIndex > text.lenth) {
                        return text
                    } else {
                    }

                    const rowsText = text.slice(0, startIndex)
                    return toEllipsis(rowsText)
                }


                const showLetters = ref('')
                onMounted(() => {
                    showLetters.value = getLastClipByRow(testText, 3, 100)

                })
                return {
                    testText,
                    showLetters
                }
            }

        })
        app.mount("#app")
    </script>
</body>

</html>

您可以通过设置 ECharts 图表的样式来解决文字超出盒子显示的问题。下面是一些可能有用的方法: 1. 调整容器大小:确保 ECharts 图表的容器大小足够大,以适应文字的显示。您可以通过设置容器的宽度和高度来调整大小。 2. 文字换行:使用 ECharts 提供的文本换行功能,可以在需要换行的位置添加 '\n' 或者使用 '\r\n' 进行强制换行。例如: ```javascript option = { // ... xAxis: { // ... axisLabel: { formatter: function (value) { // 每个字符之间加上换行符 return value.split('').join('\n'); } } }, // ... }; ``` 3. 文字截断:如果您希望在超出容器范围时截断文字并显示省略号,可以使用 CSS 的 `text-overflow` 属性。将 ECharts 的容器设置为具有固定宽度和高度,并使用以下 CSS 样式: ```css .echarts-container { width: 200px; /* 设置容器宽度 */ height: 100px; /* 设置容器高度 */ overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } ``` 这样超出容器范围的文字将被截断,并在末尾显示省略号。 4. 缩放文本:如果您希望文本自动缩放以适应容器大小,可以使用 ECharts 的 `textStyle` 属性中的 `fontSize` 设置。使用 `fontSize` 根据容器大小动态调整字体大小,确保文本适合容器。 ```javascript option = { // ... xAxis: { // ... axisLabel: { textStyle: { fontSize: 12 // 调整字体大小 } } }, // ... }; ``` 请根据您的具体需求选择适合的方法来解决文字超出盒子显示的问题。希望对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值