타입스크립트로 d3 라인차트를 구현할 때, 시간 축에 tickFormat을 쓰려다가 아래와 같은 에러를 만났다.
function timeFormat(specifier: string): (date: Date) => string Returns a new formatter for the given string specifier. The returned function formats a specified date, returning the corresponding string.
An alias for locale.format (TimeLocaleObject.format) on the default locale.
@param specifier — A specifier string for the date format.
TypeScript error when using d3.timeFormat in axis.tickFormat()
This code works in JavaScript: var timeFormat = d3.timeFormat("%M:%S"); var yAxis = d3.axisLeft(y).tickFormat(timeFormat) But this code in TypeScript does not work: const yAxis = d3.axisLeft(y).
stackoverflow.com
이는 해당 axis(축)에 어떤 타입인지 명시하지 않아 생기는 에러였다. 아래처럼 작성하면 해결이 가능하다.
const xAxis = d3
.axisBottom<Date>(xScale)
.tickFormat(d3.timeFormat('%y-%m'))