Skip to content

Instantly share code, notes, and snippets.

@DeBraid
Created December 2, 2014 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DeBraid/943bc7bbdb532f0c4a96 to your computer and use it in GitHub Desktop.
Save DeBraid/943bc7bbdb532f0c4a96 to your computer and use it in GitHub Desktop.
1d overlap
{"description":"1d overlap","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"pingpong","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"ajax-caching":true,"thumbnail":"http://i.imgur.com/rCH779n.png"}
var svg = d3.select("svg");
var barHeight = 40;
var scale = d3.scale.linear()
.domain([0, 200])
.range([0, 500])
var brush1 = d3.svg.brush()
.x(scale)
.extent([10, 40])
var brush2 = d3.svg.brush()
.x(scale)
.extent([60, 100])
var b1 = svg.append("g").attr("transform", "translate(50, 50)")
var b2 = svg.append("g").attr("transform", "translate(50, 100)")
brush1(b1);
brush2(b2);
b1.selectAll("rect").attr({ height: barHeight })
b2.selectAll("rect").attr({ height: barHeight })
var txt = svg.append("text")
.attr({
x: 50,
y: 200
})
txt.text("not overlapping");
function recalc() {
var ext1 = brush1.extent();
var ext2 = brush2.extent();
// check if two lines are overlapping
if( (ext2[0] >= ext1[0] && ext2[0] <= ext1[1]) ||
(ext1[0] >= ext2[0] && ext1[0] <= ext2[1]))
{
txt.text("overlapping")
} else {
txt.text("not overlapping")
}
}
brush1.on("brush", recalc)
brush2.on("brush", recalc)
recalc()
.background {
visibility: visible !important;
fill: #c6c6c6;
}
.extent {
visibility: visible !important;
fill: #fea3f8;
}
.resize rect {
visibility: visible !important;
fill: #007cf9;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment