You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React中如何去除ECharts坐标轴的小数数值?

问题

当前我的图表Y轴显示小数数值,希望仅显示1、2、3这类整数。以下是我的ECharts配置代码:

const option: EChartsOption = {
    dataset: {
      source: data()
    },
    grid: {
      left: "3%",
      right: "4%",
      bottom: "3%",
      containLabel: true
    },
    xAxis: {
      type: "category",
      splitLine: {
        show: true,
        lineStyle: {
          color: "#272A30",
          width: 1
        }
      }
    },
    yAxis: {
      type: "value",
      axisLabel: {
        color: "white",
        fontSize: 11
      },
      splitLine: {
        lineStyle: {
          color: "#272A30",
          width: 1
        }
      }
    },
    series: [
      {
        type: "line",
        smooth: true,
        name: "Incidents",
        showSymbol: false,
        color: "#E24D42",
        symbolSize: 0,
        lineStyle: {
          color: "#E24D42"
        },
        label: {
          precision: 2
        }
      }
    ]
  };

请问在React中使用echarts库时,该如何去除坐标轴的小数数值?

解决方案

要让ECharts Y轴仅显示整数,只需修改以下几处配置:

  • 格式化Y轴标签
    在yAxis.axisLabel中添加格式化规则,强制将数值转为整数:
yAxis: {
  type: "value",
  axisLabel: {
    color: "white",
    fontSize: 11,
    // 直接格式化显示整数
    formatter: '{value}'
  },
  splitLine: {
    lineStyle: {
      color: "#272A30",
      width: 1
    }
  }
}

如果需要严格避免类似2.0的显示,可使用函数格式化:

formatter: (value) => Math.round(value).toString()
  • 强制整数刻度间隔(可选)
    如果Y轴刻度本身仍出现小数,可强制设置刻度间隔为1:
yAxis: {
  type: "value",
  interval: 1, // 仅生成整数刻度
  axisLabel: {
    color: "white",
    fontSize: 11,
    formatter: '{value}'
  },
  // ... 其他原有配置
}
  • 修正系列标签精度
    你的系列配置中label.precision: 2会让线上标签保留两位小数,若不需要可改为0:
series: [
  {
    // ... 其他原有配置
    label: {
      precision: 0 // 仅显示整数
    }
  }
]

内容的提问来源于stack exchange,提问作者great programmer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 22:10:23