Skip to content

Instantly share code, notes, and snippets.

@wbuchanan
Last active June 23, 2020 12:16
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 wbuchanan/e93888a190791e8e6913 to your computer and use it in GitHub Desktop.
Save wbuchanan/e93888a190791e8e6913 to your computer and use it in GitHub Desktop.
Help showing school points after zoom to bounding box
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
<!DOCTYPE html>
<html>
<head>
<title>A Title</title>
<meta charset="utf-8">
<style>
.background {
fill: #eeeeee;
pointer-events: all;
}
.mesh {
fill: none;
stroke: #000000;
stroke-linecap: round;
stroke-linejoin: round;
}
</style>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
</head>
<body>
<div id="distmap">
<script>
var margins = {top: 25, right: 25, bottom: 25, left: 25},
width = 600,
height = 900,
active = d3.select(null);
var projection = d3.geo.albersUsa();
var zoom = d3.behavior.zoom()
.translate([0, 0])
.scale(1)
.scaleExtent([1, 8])
.on("zoom", zoomed);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("#distmap").append("svg")
.attr("width", width)
.attr("height", height)
.on("click", stopped, true);
svg.append("rect")
.attr("class", "background")
.attr("width", width)
.attr("height", height)
.on("click", reset);
var g = svg.append("g");
svg
.call(zoom) // delete this line to disable free zooming
.call(zoom.event);
d3.json("distgrades.json", function(error, districts) {
var distgrades = topojson.feature(districts, districts.objects.distgrades);
projection.scale(1).translate([0, 0]);
var b = path.bounds(distgrades),
s = .95 / Math.max((b[1][0] - b[0][0]) / width, (b[1][1] - b[0][1]) / height),
t = [(width - s * (b[1][0] + b[0][0])) / 2, (height - s * (b[1][1] + b[0][1])) / 2];
projection
.scale(s)
.translate(t);
g.selectAll("path")
.data(distgrades.features.filter(function(d) { return (d.id / 10000 | 0) % 100 !== 99; }))
.enter().append("path")
.attr("class", "distgrades")
.attr("d", path)
.on("click", clicked)
.style("fill", "orange");
g.append("path")
.datum(topojson.mesh(districts, districts.objects.distgrades, function(a, b) { return a !== b; }))
.attr("class", "mesh")
.attr("d", path);
});
d3.select(self.frameElement).style("height", height + "px");
function clicked(d) {
if (active.node() === this) return reset();
active.classed("active", false);
active = d3.select(this).classed("active", true);
var bounds = path.bounds(d),
dx = bounds[1][0] - bounds[0][0],
dy = bounds[1][1] - bounds[0][1],
x = (bounds[0][0] + bounds[1][0]) / 2,
y = (bounds[0][1] + bounds[1][1]) / 2,
scale = .9 / Math.max(dx / width, dy / height),
translate = [width / 2 - scale * x, height / 2 - scale * y];
var district = d.id;
console.log(district);
svg.transition()
.call(zoom.translate(translate).scale(scale).event)
.each("end", function(d) {
d3.json("schoolgrades.json", function(error, schools) {
dist = schools.objects.schools.geometries ;
for (var sch in schools.objects.schools.geometries) {
if (dist[sch].properties.distid == district) {
distSchData = dist[sch];
console.log(distSchData);
var schs = svg.selectAll("circle")
.data(schools.objects.schools.geometries.filter(function(d) {
return d.distid == district;
console.log("school file district id" + d.distid);
console.loc("District file district id" + district);}))
.enter()
.append("circle")
.attr("transform", function(d) { return "translate(" + projection([d.lon, d.lat]) + ")"; })
.attr("r", 5)
.style("fill", "blue")
.style("opacity", 0.85);
}}}) });
}
function reset() {
active.classed("active", false);
active = d3.select(null);
svg.transition()
.duration(750)
.call(zoom.translate([0, 0]).scale(1).event);
}
function zoomed() {
g.style("stroke-width", 1.5 / d3.event.scale + "px");
g.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
}
// If the drag behavior prevents the default click,
// also stop propagation so we don’t click-to-zoom.
function stopped() {
if (d3.event.defaultPrevented) d3.event.stopPropagation();
}
</script>
</div>
</body>
</html>
@wbuchanan
Copy link
Author

@coxdenis32
What are you talking about and how is it in any way relevant to this gist? I would also note that parallel lines can not intersect based on the definition of parallel, and on that note it seems to me that you are also wrong.

@wbuchanan
Copy link
Author

@coxdenis32
Stop spamming my page with advertisements for services that violate academic integrity policies everywhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment