Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

d3.js, how can i create an axis with custom labels and customs ticks?

I have an array similar to this:

[{year: 1999, val: 5}, {year: 2002, val: 8}]

and I would like to add an axis where I have one tick for each year value (something I can do with tickValues and tickFormat) but where the tick label is not only the year but has a custom format, so the result could be something like "1999: 5" for the first array element.

To rephrase it:

Where I'm now

const xAxis = d3.axisTop(xScale)
        .tickValues(data.map(d => d.year);

This is not ok because only the year is visualized on the label of the tick and I would like to use a custom format.

Is this possible with d3?

like image 408
heapOverflow Avatar asked Nov 22 '25 09:11

heapOverflow


1 Answers

You could try like this

const xAxis = d3.axisTop(xScale)
        .tickValues(data)
        .tickFormat(d => (d.year +":"+ d.val));
like image 122
Dinesh undefined Avatar answered Nov 23 '25 22:11

Dinesh undefined