I am working with a test data
set to create a viz
.
The full code is below and a notebook
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<script type="text/javascript" src="https://d3js.org/d3.v7.min.js"></script>
<body>
<link rel="stylesheet" href="style.css"></link>
<div id="container" class="svg-container"></div>
<svg>
</svg>
<!--d3 script-->
<script type="text/javascript">
async function draw() {
const data = await d3.json("https://raw.githubusercontent.com/smpa01/d3data/main/test.json");
//namespace
const svgns = 'http://www.w3.org/2000/svg'
const svg = d3.select('svg')
//define dimension
const width = 1280;
const height = 720;
svg
//.attr('xmlns', svgns)
.attr('viewBox', `0 0 ${width} ${height}`)
svg.append('rect')
.attr('class', 'vBoxRect')
.attr('width', `${width}`)
.attr('height', `${height}`)
.attr('fill', 'none')
.attr('stroke', 'red')
const padding = {
top: 70,
bottom: 50,
left: 70,
right: 50
}
const boundHeight = height - padding.top - padding.bottom;
const boundWidth = width - padding.right - padding.left;
//create BOUND rect -- to be removed later
svg.append('rect')
.attr('class', 'boundRect')
.attr('x', `${padding.left}`)
.attr('y', `${padding.top}`)
.attr('width', `${boundWidth}`)
.attr('height', `${boundHeight}`)
.attr('fill', 'none')
.attr('stroke', 'green')
//create bound element
const bound = svg.append('g')
.attr('class', 'bound')
.style('transform', `translate(${padding.left}px,${padding.top}px)`)
//scale converts a domain (data) to range (pixel)
const scaleX = d3.scaleLinear()
.range([0, boundWidth])
.domain(d3.extent(data, d => d.Year))
bound.append('g')
.attr('class', 'xAxis')
.append('g')
.attr('class', 'xAxisBottom')
.call(d3.axisBottom(scaleX).ticks().tickFormat(d3.format("d")))
.style('transform', `translateY(${boundHeight}px)`)
}
draw();
</script>
</body>
</html>
The dataset min=1997
and max=2021
. But d3 is generating this
I am not sure how to fix this. But I want d3 to return the first and last tick labels.
Also, is it possible to fix this issue dynamically rather than manually cause I desire the code to work flawlessly with the production
data (dynamically controlled by the end-user).
Thank you in advance