日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

Echarts x軸標簽太長解決方案

作者:ZionHH 更新時間: 2022-05-12 編程語言

有時候我們會遇到x軸標簽過于長的情況,會導(dǎo)致顯示不全
示例圖片

示例代碼

option: {
  xAxis: {
    type: 'category',
    data: ['長長的標簽', '長長長的標簽', '長長長長長的標簽', '長的標簽', '長長長長的標簽', '長長長的標簽', '長長長長長長的標簽']
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      type: 'bar',
      data: [80, 67, 42, 58, 40, 30, 56]
    }
  ]
}

方案1:文字旋轉(zhuǎn)45°

旋轉(zhuǎn)45°

xAxis: {
  type: 'category',
  data: ['長長的標簽', '長長長的標簽', '長長長長長的標簽', '長的標簽', '長長長長的標簽', '長長長的標簽', '長長長長長長的標簽'],
  axisLabel: {
    // 旋轉(zhuǎn)角度
    rotate: 45
  }
}

方案2:文字換行

在這里插入圖片描述

xAxis: {
  type: 'category',
  data: ['長長的標簽', '長長長的標簽', '長長長長長的標簽', '長的標簽', '長長長長的標簽', '長長長的標簽', '長長長長長長的標簽'],
  axisLabel: {
    formatter: val => {
      // 一行字數(shù)
      const max = 4
      // 標簽長度
      const valLength = val.length
      // 換行數(shù)
      const rowNum = valLength / 4
      if (valLength > 1) {
        let target = ''
        for (let i = 0; i < rowNum; i++) {
          const start = i * max
          const end = start + max
          target += val.substring(start, end) + '\n'
        }
        return target
      } else {
        return val
      }
    }
  }
}

方案3:在柱條中顯示

在這里插入圖片描述

xAxis: {
  show: false, // 隱藏x軸標簽
  type: 'category',
  data: ['長長的標簽', '長長長的標簽', '長長長長長的標簽', '長的標簽', '長長長長的標簽', '長長長的標簽', '長長長長長長的標簽']
}
series: [
  {
    type: 'bar',
    data: [80, 67, 42, 58, 40, 10, 56],
    label: {
      // 顯示label代替x軸標簽
      show: true,
      position: 'insideBottom',
      formatter: params => {
        return params['name'].split('').join('\n')
      }
    }
  }
]

原文鏈接:https://blog.csdn.net/z291493823/article/details/124609377

欄目分類
最近更新