The continuous.tickFormat() function is used to change the format of the tick values. It returns a number format function that is suitable for displaying a tick value.
Syntax:
continuous.tickFormat( count, specifier )
Parameters: This function accepts two parameters as mentioned above and described below.
- count: It is the number of tick values to be used. It is an optional parameter.
- specifier: It is a string which specifies the format to be used. It is an optional parameter.
Return Values: This function does not return anything.
The program below illustrates the continuous.tickFormat() function in D3.js:
Example 1:
<!DOCTYPE html>
<html>
<head>
<script src="https://d3js.org/d3.v4.min.js">
</script>
<script src=
"https://d3js.org/d3-color.v1.min.js">
</script>
<script src=
"https://d3js.org/d3-interpolate.v1.min.js">
</script>
<script src=
"https://d3js.org/d3-scale-chromatic.v1.min.js">
</script>
</head>
<body>
<h1 style="color: green">
GeeksforGeeks
</h1>
<p>continuous.tickFormat() Function </p>
<script>
var x = d3.scaleLinear()
.domain([0, 1])
.range([1, 2, 3, 4, 5, 6]);
var ticks = x.ticks(3)
var tickFormat = x.tickFormat(3, " %");
document.write("<h3>" +
ticks.map(tickFormat) +
"</h3>");
</script>
</body>
</html>
Output:
Example 2:
<!DOCTYPE html>
<html>
<head>
<script src="https://d3js.org/d3.v4.min.js">
</script>
<script src=
"https://d3js.org/d3-color.v1.min.js">
</script>
<script src=
"https://d3js.org/d3-interpolate.v1.min.js">
</script>
<script src=
"https://d3js.org/d3-scale-chromatic.v1.min.js">
</script>
</head>
<body>
<h1 style="color: green">
GeeksforGeeks
</h1>
<p>continuous.tickFormat() Function </p>
<script>
var x = d3.scaleLinear()
.domain([0, 1])
.range(["red", "blue", "green", "orange"]);
var ticks = x.ticks(5)
var tickFormat = x.tickFormat(5, " $");
document.write("<h3>" +
ticks.map(tickFormat) +
"</h3>")
</script>
</body>
</html>
Output:
