xxxxxxxxxx
<meta charset="utf-8">
<title>Class 10</title>
<style>
h1 {
display: block;
font-size: 1.5em;
text-align: CENTER;
margin-top: 0.67em;
margin-bottom: 0.67em;
margin-left: 0;
margin-right: 0;
font-weight: bold;
}
body {
margin: 0;
}
path {
fill: none;
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: 100%;
max-height: 100%;
}
</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 src="https://cdnjs.cloudflare.com/ajax/libs/d3-legend/2.21.0/d3-legend.js"></script>
<script>
var tooltip = d3.select('body').append('div')
.attr('class', 'tooltip')
.attr("width", 170)
.attr("height", 105)
var pi = Math.PI,
tau = 2 * pi;
var width = Math.max(960, window.innerWidth),
height = Math.max(500, window.innerHeight);
// Initialize the projection, only Mercator available with basemap
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);
// path and points radius
var path = d3.geoPath()
.projection(projection)
.pointRadius(4);
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");
//bike staions
var bstations = svg.append("g");
// trails
var trl = svg.append("g");
// Lincoln Memorial
var LM = svg.append("circle")
.attr("r",5)
.style("fill","yellow");
// Jefferson Memorial
var JM = svg.append("circle")
.attr("r",5)
.style("fill","yellow");
// Navy – Merchant Marine Memorial
var MarineM = svg.append("circle")
.style("fill","yellow");
// Washington Monument
var WM = svg.append("circle")
.attr("r",5)
.style("fill","yellow");
// ZOO
var ZOO = svg.append("circle")
.attr("r",5)
.style("fill","yellow");
// Title
// rect under the title
var rect = svg.append("rect")
.attr("x", width / 4)
.attr("y", ".8em")
.attr("width", width / 2)
.attr("height", "2em")
.style("fill", "#a3a3a3")
.style("fill-opacity", "1")
.style("stroke", "none")
.style("stroke-width", "1.5px");
var text = svg.append('text')
.attr('x', width / 2)
.attr('y', "1.5em")
.style('font-size', '1.5em')
.style('text-anchor', 'middle')
.text('DISCOVERING D.C. BY BIKE')
.style("fill", "black")
// coordinates to move legend
x = 1050;
y = 705;
// rect under the legend
var rect = svg.append("rect")
.attr("x", x+10)
.attr("y", y)
.attr("width", 170)
.attr("height", 105)
.style("fill", "#b5b5b5")
.style("fill-opacity", "1")
.style("stroke", "none")
.style("stroke-width", "1.5px");
var legend = d3.select("svg").append("svg")
.attr("x", x)
.attr("y", y)
.style("font-size","15px");
var legend2 = d3.select("svg").append("svg")
.attr("x", x+7)
.attr("y", y+65)
.style("font-size","15px");
// Colors and description for a legend
var ordinal = d3.scaleOrdinal()
.domain(["Bike Station", "Tourist Attraction"])
.range(["#B22222","yellow"])
var ordinal2 = d3.scaleOrdinal()
.domain(["Bike Trail"])
.range(["#006400"])
// add legend
legend.append("g")
.attr("class", "legendOrdinal")
.attr("transform", "translate(20,20)")
var legendOrdinal = d3.legendColor()
.shape("path", d3.symbol().type(d3.symbolCircle).size(150)())
.shapePadding(10)
.title("Legend")
.scale(ordinal)
svg.select(".legendOrdinal")
.call(legendOrdinal);
legend2.append("g")
.attr("class", "legendOrdinal2")
.attr("transform", "translate(20,20)")
var legendOrdinal2 = d3.legendColor()
.shape("path", d3.symbol().type(d3.symbolSquare).size(150)())
.shapePadding(10)
.scale(ordinal2)
svg.select(".legendOrdinal2")
.call(legendOrdinal2);
// add geojsons
var layer1 = 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";
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);
});
// 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);
// add points of intrest
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>")
})
JM
.attr('cx', projection([-77.03654320042097, 38.881325713574476])[0])
.attr('cy', projection([-77.03654320042097, 38.881325713574476])[1])
.style("stroke", 'none')
.on("mouseover", function(d) {
div.transition()
.duration(2000)
.style("opacity", 1)
div.html("<span style='color:blue'><h1>Jefferson Memorial</h1><br><img src='https://washington-org.s3.amazonaws.com/s3fs-public/jefferson-at-night_credit-carol-m.-highsmith-lib-of-congress.jpg'></span> <span style='color:blue'></span>")
})
MarineM
.attr('cx', projection([-77.04725090111135, 38.87628230575873])[0])
.attr('cy', projection([-77.04725090111135, 38.87628230575873])[1])
.style("stroke", 'none')
.on("mouseover", function(d) {
div.transition()
.duration(100)
.attr("width", 170)
.attr("height", 105)
.style("opacity", 1)
div.html("<span style='color:blue'><h1>Navy – Merchant Marine Memorial</h1><br><img src='https://images.fineartamerica.com/images-medium-large-5/1-navy-merchant-marine-memorial-david-hyun.jpg'></span> <span style='color:blue'></span>")
})
WM
.attr('cx', projection([-77.03522783044863, 38.88946969709221])[0])
.attr('cy', projection([-77.03522783044863, 38.88946969709221])[1])
.style("stroke", 'none')
.on("mouseover", function(d) {
div.transition()
.duration(2000)
.style("opacity", 1)
div.html("<span style='color:blue'><h1>Washington Monument</h1><br><img src='https://wtop.com/wp-content/uploads/2015/08/540809551.jpg'></span> <span style='color:blue'></span>")
})
ZOO
.attr('cx', projection([-77.049870, 38.929825])[0])
.attr('cy', projection([-77.049870, 38.929825])[1])
.style("stroke", 'none')
.on("mouseover", function(d) {
div.transition()
.duration(2000)
.style("opacity", 1)
div.html("<span style='color:blue'><h1>Smithsonian National Zoological Park</h1><br><img src='https://www.etraveltrips.com/wp-content/uploads/2012/05/National-Zoo-Pandas.jpg'></span> <span style='color:blue'></span>")
})
// add raster
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
https://cdnjs.cloudflare.com/ajax/libs/d3-legend/2.21.0/d3-legend.js