// Prep data
/* var x = [new Date(2020, 10, 10), new Date(2020, 10, 11), new Date(2020, 10, 12), new Date(2020, 10, 13)]
var y = [5,6,8,4] */
var x = d3.timeDays(new Date(2020, 06, 01), new Date(2020, 10, 30));
var y = Array.from({length: x.length}, Math.random).map(n => Math.floor(n * 10) + 5);
var data = x.map((v, i) => {
return {
"x": v,
"y": y[i]
}
});
// Use the margin convention practice
var margin = {top: 50, right: 50, bottom: 50, left: 50}
var width = 600 - margin.left - margin.right // Use the window's width
var height = 400 - margin.top - margin.bottom; // Use the window's height
// zoom function for d3v6
// adapt to v6 from this v5 block https://bl.ocks.org/LemoNode/7ac1d41fe75fe7d2d9cb85e78aad6303
var zoom = d3.zoom()
.on('zoom', (event) => {
xScale
.domain(event.transform.rescaleX(xScale2).domain())
.range([0, width].map(d => event.transform.applyX(d))); //
svg.select(".line")
.attr("d", line); // zooms line
svg.select(".x-axis")
.call(d3.axisBottom(xScale)
.tickSizeOuter(0)); // zooms axis
})
.scaleExtent([1, 32]); // adjust 32 to less zoom less
// X scale - use min and max of scale
var xScale = d3.scaleUtc()
.domain([d3.min(x), d3.max(x)]) // input
.range([0, width]); // output
// constant reference point whilst zooming
var xScale2 = d3.scaleUtc()
.domain([d3.min(x), d3.max(x)]) // input
.range([0, width]); // output
// Y scale - add 5 for bit of whitespace at top of graph
var yScale = d3.scaleLinear()
.domain([0, d3.max(y) + 5]) // input
.range([height, 0]); // output
// d3's line generator
var line = d3.line()
.x(function(d) { return xScale(d.x); }) // set the x values for the line generator
.y(function(d) { return yScale(d.y); }) // set the y values for the line generator
// Add the SVG to the page and employ #2
var svg = d3.select("#my_chart").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.call(zoom) // call the zoom
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// clippath to stop line and x-axis 'spilling over'
svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("x", 0)
.attr("width", width)
.attr("height", height);
// call x-axis and apply the clip from the defs
svg.append("g")
.attr("class", "x-axis")
.attr("clip-path", "url(#clip)") // add the clip path!
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(xScale)); // Create an axis component with d3.axisBottom
// Call the y-axis
svg.append("g")
.attr("class", "y-axis")
.call(d3.axisLeft(yScale)); // Create an axis component with d3.axisLeft
// Append the path, bind the data, and call the line generator
svg.append("path")
.datum(data) // 10. Binds data to the line
.attr("class", "line") // Assign a class for styling
.attr("clip-path", "url(#clip)") // add the clip path!
.attr("d", line); // 11. Calls the line generator
.line {
fill: none;
stroke: #ffab00;
stroke-width: 3;
}
.overlay {
fill: none;
pointer-events: all;
}
/* Style the dots by assigning a fill and stroke */
.dot {
fill: #ffab00;
stroke: #fff;
}
.focus circle {
fill: none;
stroke: steelblue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/6.0.0/d3.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
</head>
<body>
<div id="my_chart"></div>
</body>
</html>