The d3.axis.tickFormat() Function in D3.js is used to control which ticks are labelled. This function is used to implement your own tick format function.
Syntax:
axis.tickFormat([format])
Parameters: This function accepts the following parameter.
- format: These parameters are format to set the tick format function.
Return Value: This function returns the currently set tick format function, which defaults to null.
Below programs illustrate the d3.axis.tickFormat() function in D3.js:
Example 1:
<!DOCTYPE html> <html>   <head>     <title>         D3.js | D3.axis.tickFormat() Function     </title>     <script type="text/javascript"    </script>           <style>         svg text {             fill: green;             font: 15px sans-serif;             text-anchor: center;         }     </style> </head>   <body>     <script>         var width = 400, height = 400;         var svg = d3.select("body")             .append("svg")             .attr("width", width)             .attr("height", height);           var yscale = d3.scaleLinear()             .domain([0, 1])             .range([height - 50, 0]);           var y_axis = d3.axisLeft().scale(yscale)         .tickValues([0, .2, .5, .70, 1])         .tickFormat((d, i) => ['a', 'e', 'i', 'o', 'u'][i]);             svg.append("g")             .attr("transform", "translate(100, 20)")             .call(y_axis)     </script> </body> </html> |
Output:
Example 2:
<!DOCTYPE html> <html>   <head>     <title>         D3.js | D3.axis.tickFormat() Function     </title>     <script type="text/javascript"    </script>           <style>         svg text {             fill: green;             font: 15px sans-serif;             text-anchor: center;         }     </style> </head>   <body>     <script>         var width = 600, height = 400;         var svg = d3.select("body")             .append("svg")             .attr("width", width)             .attr("height", height);           var xscale = d3.scaleLinear()             .domain([0, 10])             .range([0, width - 100]);           var x_axis = d3.axisBottom(xscale).ticks(4)         .tickFormat(x => `(${x.toFixed(1)})`);           var xAxisTranslate = height / 2;           svg.append("g")             .attr("transform", "translate(50, "             + xAxisTranslate + ")")             .call(x_axis)     </script> </body> </html> |
Output:

