Built with blockbuilder.org
forked from rekaj3773's block: Brush and Zoom 2
license: mit |
Built with blockbuilder.org
forked from rekaj3773's block: Brush and Zoom 2
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://d3js.org/d3.v5.min.js"></script> | |
<style> | |
.line { | |
fill: none; | |
stroke: steelblue; | |
stroke-width: 1.5px; | |
} | |
.zoom { | |
cursor: move; | |
fill: none; | |
pointer-events: all; | |
} | |
</style> | |
</head> | |
<body> | |
<svg width="873" height="500"></svg> | |
<script> | |
//Brush and zoom based off: | |
//https://bl.ocks.org/EfratVil/92f894ac0ba265192411e73f633a3e2f | |
var data = []; | |
//var data = [{"depthNum":0,"depth":4},{"depthNum":1,"depth":8},{"depthNum":2,"depth":10},{"depthNum":3,"depth":11},{"depthNum":4,"depth":15},{"depthNum":5,"depth":19},{"depthNum":6,"depth":22},{"depthNum":7,"depth":23},{"depthNum":8,"depth":30}]; | |
var innerHeight=607; | |
var innerWidth=250; | |
var svg = d3.select("svg"); | |
var margin = {top: 20, right: 20, bottom: 110, left: 40}; | |
var margin2 = {top: 436, right: 20, bottom: 30, left: 40}; | |
var width = +svg.attr("width") - margin.left - margin.right; | |
var width2 = +svg.attr("width") - margin2.left - margin2.right; | |
var height = +svg.attr("height") - margin.top - margin.bottom; | |
var height2 = +svg.attr("height") - margin2.top - margin2.bottom; | |
//from developer.mozilla.org | |
function getRandomArbitrary(min, max) { | |
return Math.random() * (max - min) + min; | |
} | |
function genPoints(j){ | |
var data = []; | |
var ranMin = 0; | |
var ranMax = 2000; | |
for(i=0;i<j;i++) | |
{ | |
var ranDep = getRandomArbitrary(ranMin,ranMax); | |
data.push( | |
{ | |
"depthNum":i, | |
"depth":ranDep | |
} | |
); | |
} | |
render(data); | |
} | |
var xScale = d3.scaleLinear().range([0, width]); | |
var xScale2 = d3.scaleLinear().range([0, width]); | |
var yScale = d3.scaleLinear().range([height, 0]); | |
var yScale2 = d3.scaleLinear().range([height2, 0]); | |
//console.log(xScale); | |
var xAxis = d3.axisBottom(xScale), | |
xAxis2 = d3.axisBottom(xScale2), | |
yAxis = d3.axisLeft(yScale), | |
yAxis2 = d3.axisLeft(yScale2); | |
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 line = d3.line() | |
.x(function (d) { return xScale(d.depthNum); }) | |
.y(function (d) { return yScale(d.depth); }); | |
var line2 = d3.line() | |
.x(function (d) { return xScale2(d.depthNum); }) | |
.y(function (d) { return yScale2(d.depth); }); | |
var clip = svg.append("defs").append("svg:clipPath") | |
.attr("id", "clip") | |
.append("svg:rect") | |
.attr("width", width) | |
.attr("height", height) | |
.attr("x", 0) | |
.attr("y", 0); | |
var Line_chart = svg.append("g") | |
.attr("class", "focus") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")") | |
.attr("clip-path", "url(#clip)"); | |
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 + ")"); | |
function render(data){ | |
xScale.domain(d3.extent(data, function(d) { return d.depthNum; })); | |
yScale.domain([0, d3.max(data, function (d) { return d.depth; })]); | |
xScale2.domain(xScale.domain()); | |
yScale2.domain(yScale.domain()); | |
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); | |
Line_chart.append("path") | |
.datum(data) | |
.attr("class", "line") | |
.attr("d", line); | |
context.append("path") | |
.datum(data) | |
.attr("class", "line") | |
.attr("d", line2); | |
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, xScale.range()); | |
svg.append("rect") | |
.attr("class", "zoom") | |
.attr("width", width) | |
.attr("height", height) | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")") | |
.call(zoom); | |
} | |
genPoints(100); | |
function brushed() { | |
if (d3.event.sourceEvent && d3.event.sourceEvent.type === "zoom") return; // ignore brush-by-zoom | |
var s = d3.event.selection || xScale2.range(); | |
xScale.domain(s.map(xScale2.invert, xScale2)); | |
Line_chart.select(".line").attr("d", line); | |
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; | |
xScale.domain(t.rescaleX(xScale2).domain()); | |
Line_chart.select(".line").attr("d", line); | |
focus.select(".axis--x").call(xAxis); | |
context.select(".brush").call(brush.move, xScale.range().map(t.invertX, t)); | |
} | |
//genLine(10); | |
//console.log(data); | |
</script> | |
</body> |