Sunday, July 5, 2026
HomeLanguagesJavascriptD3.js axis.tickFormat() Function

D3.js axis.tickFormat() Function

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"
        src="https://d3js.org/d3.v4.min.js"> 
    </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"
        src="https://d3js.org/d3.v4.min.js"> 
    </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:

RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32519 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6900 POSTS0 COMMENTS
Nicole Veronica
12015 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12110 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7263 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6967 POSTS0 COMMENTS