Thank you to mbostock for his block: Focus+Context via Brushing
This was forked and adapted from that starting point.
xxxxxxxxxx
<meta charset="utf-8">
<link href='https://fonts.googleapis.com/css?family=Raleway:400,700' rel='stylesheet' type='text/css'>
<style>
svg {
font-family: 'Raleway', sans-serif;
font-size: 16px;
}
.area {
fill: none;
stroke: steelblue;
stroke-width: 1px;
clip-path: url(#clip);
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.tick {
font-size: 12px;
}
.brush .extent {
stroke: #fff;
fill-opacity: .125;
shape-rendering: crispEdges;
}
.chartTitle {
font-size: 22px;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
/*
var title = {
'x': 'Date',
'y': 'C02: parts per million',
'chart': 'C02 Concentration measured at Mauna Loa: 1974 to 2014'
}
var file = "c02Daily.csv";
*/
var title = {
'x': 'Date',
'y': 'Number of Babies Born',
'chart': 'Number of Babies in the United States: 1969 to 1988'
}
var file = "dailyUSBirths.csv";
var min = 'zero';
var totalHeight = 700,
bottomChartHeight = 200,
margin = {top: 30, right: 10, bottom: bottomChartHeight + 30, left: 130},
margin2 = {top: totalHeight - bottomChartHeight, right: margin.right, bottom: 60, left: margin.left},
width = 960 - margin.left - margin.right,
height = totalHeight - margin.top - margin.bottom,
height2 = totalHeight - margin2.top - margin2.bottom;
var parseDate = d3.time.format("%Y-%m-%d").parse;
var x = d3.time.scale().range([0, width]),
x2 = d3.time.scale().range([0, width]),
y = d3.scale.linear().range([height, 0]),
y2 = d3.scale.linear().range([height2, 0]);
var xAxis = d3.svg.axis().scale(x).orient("bottom"),
xAxis2 = d3.svg.axis().scale(x2).orient("bottom"),
yAxis = d3.svg.axis().scale(y).orient("left");
var brush = d3.svg.brush()
.x(x2)
.on("brush", brushed);
var area = d3.svg.line()
.interpolate("monotone")
.x(function(d) { return x(d.date); })
//.y0(height)
.y(function(d) { return y(d.price); });
var area2 = d3.svg.line()
.interpolate("monotone")
.x(function(d) { return x2(d.date); })
.y(function(d) { return y2(d.price); });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
var focus = svg.append("g")
.attr("class", "focus")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var context = svg.append("g")
.attr("class", "context")
.attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");
d3.csv(file, type, function(error, data) {
x.domain(d3.extent(data.map(function(d) { return d.date; })));
y.domain(
[ min == 'zero' ? 0 : d3.min(data.map(function(d) { return d.price; })) * .95,
1.05 * d3.max(data.map(function(d) { return d.price; }))
]);
x2.domain(x.domain());
y2.domain(y.domain());
focus.append("path")
.datum(data)
.attr("class", "area")
.attr("d", area);
focus.append("text")
.attr("x", width / 2)
.attr("y", -10)
.attr("dy", ".71em")
.style("text-anchor", "middle")
.attr("class", 'chartTitle')
.text(title.chart);
focus.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
focus.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -(totalHeight - bottomChartHeight)/2)
.attr("y", -90)
.attr("dy", ".71em")
.style("text-anchor", "middle")
.text(title.y);
context.append("path")
.datum(data)
.attr("class", "area")
.attr("d", area2);
context.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height2 + ")")
.call(xAxis2)
.append("text")
.attr("x", width / 2)
.attr("y", 40)
.attr("dy", ".71em")
.style("text-anchor", "middle")
.text(title.x);
context.append("g")
.attr("class", "x brush")
.call(brush)
.selectAll("rect")
.attr("y", -6)
.attr("height", height2 + 7);
});
function brushed() {
x.domain(brush.empty() ? x2.domain() : brush.extent());
focus.select(".area").attr("d", area);
focus.select(".x.axis").call(xAxis);
}
function type(d) {
d.date = parseDate(d.date);
d.price = +d.value;
return d;
}
</script>
https://d3js.org/d3.v3.min.js