The time.tickFormat() function in D3.js is used to return a time format function suitable for displaying tick values. It returns the default time format when the specifier is not specified.
Syntax:
time.tickFormat( count, specifier )
Parameters: This function accepts two parameters as given above and described below:
- count: It specifies the number of tick values.
- specifier: It is the string that will be used as the specifier. It is of the format type āsā.
Return Values: This function does not return anything.
Below programs illustrate the time.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>time.tickFormat() Function </p>
<script>
var time = d3.scaleTime()
// Specify the domain and range
.domain([2001 - 01 - 01, 2005 - 01 - 02])
.range([1, 100]);
var ticks = time.ticks(1);
// Specify the format of the ticks
var tickFormat = time.tickFormat(1, "%Y-%m-%d ");
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>time.tickFormat() Function </p>
<script>
var time = d3.scaleTime()
// Specify the domain and range
.domain([2001 - 01 - 01, 2005 - 01 - 02])
.range([1, 100]);
var ticks = time.ticks(1);
// Specify the format of the ticks
var tickFormat = time.tickFormat(0, "%I %p");
document.write("<h3>" +
ticks.map(tickFormat) + "</h3>");
</script>
</body>
</html>
Output:
