xxxxxxxxxx
<meta charset="utf-8">
<title>Class 10</title>
<style>
h1 {
display: block;
font-size: 1.5em;
text-align: LEFT;
margin-top: 0.67em;
margin-bottom: 0.67em;
margin-left: 0;
margin-right: 0;
font-weight: bold;
}
body {
margin: 0;
}
path {
fill: none;
stroke: #006400;
stroke-linejoin: round;
stroke-width: 3px;
}
.info {
color: #000;
position: absolute;
background-color: #66CDAA;
visibility: hidden;
padding: 0.5em;
bottom: 50px;
right: 50px;
}
.tooltip {
position: absolute;
visibility: visible;
background-color: #E9967A;
padding: 8px;
}
img {
max-width: 50%;
max-height: 50%;
}
</style>
<svg></svg>
<div class='info'></div>
<script src="//d3js.org/d3.v4.min.js"></script>
<script src="//d3js.org/d3-tile.v0.0.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script>
var tooltip = d3.select('body').append('div')
.attr('class', 'tooltip')
var pi = Math.PI,
tau = 2 * pi;
var width = Math.max(960, window.innerWidth),
height = Math.max(500, window.innerHeight);
// Initialize the projection to fit the world in a 1×1 square centered at the origin.
var projection = d3.geoMercator()
.scale(1 / tau)
.translate([0, 0]);
// Define the tooltip for hover-over info windows
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 10);
var path = d3.geoPath()
.projection(projection)
.pointRadius(3);
var tile = d3.tile()
.size([width, height]);
var zoom = d3.zoom()
.scaleExtent([1 << 11, 1 << 24])
.on("zoom", zoomed);
var svg = d3.select("svg")
.attr("width", width)
.attr("height", height);
var raster = svg.append("g");
var bstations = svg.append("g");
var trl = svg.append("g");
var LM = svg.append("circle")
.attr("r",5)
.style("fill","yellow")
var color = d3.scaleOrdinal(d3.schemeCategory10);
var layer1 = svg.append("g");
var layer2 = svg.append("g");
var trails = "https://raw.githubusercontent.com/AnaMal08/classes/master/finalproject/trails.json";
var stations = "https://raw.githubusercontent.com/AnaMal08/classes/master/finalproject/sharebike2.json";
var places = "https://raw.githubusercontent.com/AnaMal08/classes/master/finalproject/poinfOfIntrest.json";
var legend = layer2.selectAll('.legend')
.data(d3.range(10))
.enter().append('g')
.attr('class', 'legend')
.attr('transform', 'translate(' + [width - 150, height / 3] +')');
legend.append('circle')
.attr('cy', function(d) { return 4 * d * path.pointRadius() ; })
.attr('r', path.pointRadius())
.attr('fill', color)
.attr('stroke', '#aaaaaa')
legend.append('text')
.attr('x', '1em')
.attr('y', function(d) { return 4 * d * path.pointRadius(); })
.attr('dy', '.3em')
.text(function(d) { return d + ' - ' + (d + 1); })
d3.queue()
.defer(d3.json, trails)
.defer(d3.json, stations)
.await(ready);
function ready(error, trails, stations) {
if (error) throw error;
// add trails
trl = trl.selectAll(".trail")
.data(trails.features)
.enter().append("path")
.attr("class", "trail")
.style("fill", 'none')
.style("stroke", '#006400')
.on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", .8)
div.html("<font size = 3>" + "<u>" + "Trail Name:" + "</u>" + " " + d.properties.NAME + "<br>" + "<font size = 3>" + "<u>" + "Length of trail:" + "</u>" + " " + ((d.properties.LENGTH)*0.00018939).toFixed(2) + " " +"miles" )
.style("left", (d3.event.pageX + 10) + "px")
.style("top", (d3.event.pageY - 30) + "px");
})
.on("mouseout", function(d) {
div.transition()
.duration(500)
.style("opacity", 0);
});
// add bakiestations
bstations = bstations.selectAll(".station")
.data(stations.features)
.enter().append("path")
.attr("class", "station")
.style("fill", '#B22222')
.style("stroke", 'none')
.on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", .8)
div.html("<font size = 3>" + "<u>" + "Address:" + "</u>" + " " + d.properties.ADDRESS +"<br>" + "<font size = 3>" + "<u>" + "Number of bikes:" + "</u>" + " " + d.properties.NUMBER_OF_BIKES)
.style("left", (d3.event.pageX + 10) + "px")
.style("top", (d3.event.pageY - 30) + "px");
})
.on("mouseout", function(d) {
div.transition()
.duration(500)
.style("opacity", 0);
});
// add points of intrest
// Compute the projected initial center for DC
var center = projection([-77.04, 38.91]);
// Apply a zoom transform equivalent to projection.{scale,translate,center}.
svg
.on('mousemove', mousemoved)
.on('mouseout', function() { d3.select('.info').style('visibility', 'hidden') })
.call(zoom)
.call(zoom.transform, d3.zoomIdentity
.translate(width / 2, height / 2)
.scale(1 << 19)
.translate(-center[0], -center[1]));
}
function zoomed() {
var transform = d3.event.transform;
var tiles = tile
.scale(transform.k)
.translate([transform.x, transform.y])
();
projection
.scale(transform.k / tau)
.translate([transform.x, transform.y]);
trl
.attr("d", path);
bstations
.attr("d", path);
LM
.attr('cx', projection([-77.05018389318994, 38.88929091200698])[0])
.attr('cy', projection([-77.05018389318994, 38.88929091200698])[1])
.style("stroke", 'none')
.on("mouseover", function(d) {
div.transition()
.duration(2000)
.style("opacity", 1)
div.html("<span style='color:blue'><h1>Lincoln Memorial</h1><br><img src='https://theenchantedmanor.com/wp-content/uploads/2015/02/Lincoln-Memorial-1.jpg'></span> <span style='color:blue'></span>")
})
var image = raster
.attr("transform", stringify(tiles.scale, tiles.translate))
.selectAll("image")
.data(tiles, function(d) { return d; });
image.exit().remove();
image.enter().append("image")
.attr("xlink:href", function(d) { return "https://" + "abc"[d[1] % 3] + ".tile.openstreetmap.org/" + d[2] + "/" + d[0] + "/" + d[1] + ".png"; })
.attr("x", function(d) { return d[0] * 256; })
.attr("y", function(d) { return d[1] * 256; })
.attr("width", 256)
.attr("height", 256);
}
function stringify(scale, translate) {
var k = scale / 256, r = scale % 1 ? Number : Math.round;
return "translate(" + r(translate[0] * scale) + "," + r(translate[1] * scale) + ") scale(" + k + ")";
}
function mousemoved() {
d3.select('.info')
.style('visibility', 'visible')
.text(formatLocation(projection.invert(d3.mouse(this)), projection.scale()));
}
function formatLocation(p, k) {
var format = d3.format("." + Math.floor(Math.log(k) / 2 - 2) + "f");
return (p[1] < 0 ? format(-p[1]) + "°S" : format(p[1]) + "°N") + " "
+ (p[0] < 0 ? format(-p[0]) + "°W" : format(p[0]) + "°E");
}
</script>
https://d3js.org/d3.v4.min.js
https://d3js.org/d3-tile.v0.0.min.js
https://d3js.org/topojson.v1.min.js