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

使用d3移除X轴刻度逗号时调用tickFormat方法报错如何解决

报错原因
  • tickFormat()是D3轴生成器(d3.axisBottom()返回的实例对象)的方法,你当前的写法把它放在了call()之后,call()的返回值是你选中的SVG的<g>元素对象,DOM元素本身没有tickFormat方法,因此抛出类型错误。
修正方法
  • 把tickFormat()作为轴生成器的配置项,链式调用在d3.axisBottom(xScale)之后,再传入call()即可。

修正后的相关代码如下:

const xScale = d3.scaleLinear()
  .domain([1947, 2015])
  .range([0, 961]);

svg.append('g')
  .attr('transform', 'translate(50, 638)')
  .attr('id', 'x-axis')
  // 原代码的classed('tick', true)可删除,轴生成器会自动给单个刻度元素添加tick类,给外层g加该类无意义还可能冲突
  .call(d3.axisBottom(xScale).tickFormat(d3.format("d")))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 18:36:04