其他分享
首页 > 其他分享> > 前端基础(二十八):ECharts为axisLabel标签过长添加提示功能

前端基础(二十八):ECharts为axisLabel标签过长添加提示功能

作者:互联网

展示

在这里插入图片描述

源码

<!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>
  <style>
    #container {
      width: 1000px;
      height: 500px;
      background-color: #fafafa;
    }

    #label-tooltip {
      display: none;
      position: absolute;
      top: 0;
      left: 0;
      padding: 3px 5px;
      color: #ffffff;
      border-radius: 3px;
      background-color: rgba(0, 0, 0, .6);
      font-size: 12px;
    }
  </style>
</head>

<body style="height: 100%; margin: 0">

  <div id="container"></div>

  <div id="label-tooltip"></div>

  <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts@5.2.1/dist/echarts.min.js"></script>
  <script type="text/javascript">
    var dom = document.getElementById("container");
    var labelTooltip = document.getElementById("label-tooltip");
    var myChart = echarts.init(dom);
    var option = {
      tooltip: {
        trigger: 'axis',
      },
      xAxis: {
        type: 'value',
      },
      yAxis: {
        type: 'category',
        data: ['诉不完人生的悲凉', '道不尽世间的沧桑', 'You瞧谁不起'],
        axisLabel: {
          width: 80,
          // overflow: 'breakAll',  // 换行
          overflow: 'truncate',     // 超出打点
        },
        triggerEvent: true,			// 必须
      },
      series: [
        {
          data: [100, 200, 300],
          type: 'bar',
        }
      ]
    };
    myChart.on('mousemove', (params) => {
      if (params.componentType === "yAxis") {
        labelTooltip.style.display = 'block';
        labelTooltip.style.left = params.event.offsetX + 20 + 'px';
        labelTooltip.style.top = params.event.offsetY + 'px';
        labelTooltip.innerText = params.value;
      }
    })
    myChart.on('mouseout', (params) => {
      labelTooltip.style.display = 'none';
    })
    if (option && typeof option === 'object') {
      myChart.setOption(option);
    }
  </script>
</body>

</html>

标签:style,option,二十八,axisLabel,labelTooltip,myChart,params,var,ECharts
来源: https://blog.csdn.net/weixin_43526371/article/details/120846292