Skip to content

Instantly share code, notes, and snippets.

@laxmikanta415
Last active January 7, 2018 15:00
Show Gist options
  • Save laxmikanta415/0fd68da7482cfd2fbe8659fd4c6d138d to your computer and use it in GitHub Desktop.
Save laxmikanta415/0fd68da7482cfd2fbe8659fd4c6d138d to your computer and use it in GitHub Desktop.
Brush and zoom in d3 v4
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
.area {
fill: none;
clip-path: url(#clip);
stroke: #444;
stroke-width: 2px;
}
.zoom {
cursor: move;
fill: none;
pointer-events: all;
}
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<svg width="960" height="500"></svg>
<script>
var dataJSON = {
"columns": [
"date",
"value"
],
"values": [
[
"1986-06-02",
"0.40200000000000000000"
],
[
"1986-06-03",
"0.39300000000000000000"
],
[
"1986-06-04",
"0.37800000000000000000"
],
[
"1986-06-05",
"0.39000000000000000000"
],
[
"1986-06-06",
"0.38500000000000000000"
],
[
"1986-06-09",
"0.37300000000000000000"
],
[
"1986-06-10",
"0.36500000000000000000"
],
[
"1986-06-11",
"0.38900000000000000000"
],
[
"1986-06-12",
"0.39400000000000000000"
],
[
"1986-06-13",
"0.39800000000000000000"
],
[
"1986-06-16",
"0.37900000000000000000"
],
[
"1986-06-17",
"0.37400000000000000000"
],
[
"1986-06-18",
"0.37600000000000000000"
],
[
"1986-06-19",
"0.38200000000000000000"
],
[
"1986-06-20",
"0.36500000000000000000"
],
[
"1986-06-23",
"0.36900000000000000000"
],
[
"1986-06-24",
"0.36900000000000000000"
],
[
"1986-06-25",
"0.37500000000000000000"
],
[
"1986-06-26",
"0.37600000000000000000"
],
[
"1986-06-27",
"0.37900000000000000000"
],
[
"1986-06-30",
"0.37400000000000000000"
],
[
"1986-07-01",
"0.36900000000000000000"
],
[
"1986-07-02",
"0.35900000000000000000"
],
[
"1986-07-03",
"0.34900000000000000000"
]
]
};
var testData = [];
var parseDate = d3.timeParse("%Y-%m-%d");
var parseDate2 = d3.timeParse("%b %Y");
var formatDate = d3.timeFormat('%b %Y');
dataJSON.values.forEach(function(d){
var item = {};
item['date'] = parseDate2(formatDate(parseDate(d[0])));
item['price'] = d[1];
testData.push(item);
});
console.log(testData);
var svg = d3.select("svg");
var margin = {top: 20, right: 20, bottom: 110, left: 40};
var margin2 = {top: 430, right: 20, bottom: 30, left: 40};
var width = +svg.attr("width") - margin.left - margin.right;
var height = +svg.attr("height") - margin.top - margin.bottom;
var height2 = +svg.attr("height") - margin2.top - margin2.bottom;
var x = d3.scaleTime().range([0, width]),
x2 = d3.scaleTime().range([0, width]),
y = d3.scaleLinear().range([height, 0]),
y2 = d3.scaleLinear().range([height2, 0]);
var xAxis = d3.axisBottom(x),
xAxis2 = d3.axisBottom(x2),
yAxis = d3.axisLeft(y);
var brush = d3.brushX()
.extent([[0, 0], [width, height2]])
.on("brush end", brushed);
var zoom = d3.zoom()
.scaleExtent([1, Infinity])
.translateExtent([[0, 0], [width, height]])
.extent([[0, 0], [width, height]])
.on("zoom", zoomed);
var area = d3.line()
//.curve(d3.curveMonotoneX)
.x(function(d) { return x(d.date); })
//.y0(height)
.y(function(d) { return y(d.price); });
var area2 = d3.area()
.curve(d3.curveMonotoneX)
.x(function(d) { return x2(d.date); })
.y0(height2)
.y1(function(d) { return y2(d.price); });
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 + ")");
x.domain(d3.extent(testData, function(d) { return d.date; }));
y.domain([0, d3.max(testData, function(d) { return d.price; })]);
x2.domain(x.domain());
y2.domain(y.domain());
focus.append("path")
.datum(testData)
.attr("class", "area")
.attr("d", area);
focus.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
focus.append("g")
.attr("class", "axis axis--y")
.call(yAxis);
context.append("path")
.datum(testData)
.attr("class", "area")
.attr("d", area2);
context.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height2 + ")")
.call(xAxis2);
context.append("g")
.attr("class", "brush")
.call(brush)
.call(brush.move, x.range());
svg.append("rect")
.attr("class", "zoom")
.attr("width", width)
.attr("height", height)
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.call(zoom);
function brushed() {
if (d3.event.sourceEvent && d3.event.sourceEvent.type === "zoom") return; // ignore brush-by-zoom
var s = d3.event.selection || x2.range();
x.domain(s.map(x2.invert, x2));
focus.select(".area").attr("d", area);
focus.select(".axis--x").call(xAxis);
svg.select(".zoom").call(zoom.transform, d3.zoomIdentity
.scale(width / (s[1] - s[0]))
.translate(-s[0], 0));
}
function zoomed() {
if (d3.event.sourceEvent && d3.event.sourceEvent.type === "brush") return; // ignore zoom-by-brush
var t = d3.event.transform;
x.domain(t.rescaleX(x2).domain());
focus.select(".area").attr("d", area);
focus.select(".axis--x").call(xAxis);
context.select(".brush").call(brush.move, x.range().map(t.invertX, t));
}
function type(d) {
d.date = parseDate(d.date);
d.price = +d.price;
return d;
}
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment