Rename tick labels

I have a dataset where information is stored as a number (1 - 4) but I want to represent that data as the corresponding string label. I’ve already have it working with an if/else statement using tickFormat but somehow I feel that there’s a smarter way. What would be the correct way to achieve this?

I usually inline arrays or objects, e.g.

tickFormat: d => [ , "one", "two", "three", "four"][d]

or

tickFormat: d => ({1: "one", 2: "two", 3: "three", 4: "four"})[d]

If the instantiation is more expensive, I like to use IIFEs and closures instead:

tickFormat: (map => d => map.get(d))(new Map([
  [1, "one"],
  [2, "two"],
  [3, "three"],
  [4, "four"]
]))
3 Likes

Thanks @mootari this works great.