Skip to content

Instantly share code, notes, and snippets.

@li01012
Last active May 4, 2017 16:28
Show Gist options
  • Save li01012/1b40d5c794a1c1e20d0300d2b47817fc to your computer and use it in GitHub Desktop.
Save li01012/1b40d5c794a1c1e20d0300d2b47817fc to your computer and use it in GitHub Desktop.
may4thproject1.0
license: mit
<!DOCTYPE html> //class04
<meta charset="utf-8">
<title>Class 4</title>
<style>
body {
position: absolute;
margin: 0px;
}
svg {
background-color: #4682b4;
}
.info {
font-family: sans-serif;
color: #000;
position: absolute;
top: 450px;
left: 800px;
}
path {
fill: #555555;
stroke: #aaaaaa;
}
/* NEW: style the earthquakes */
//path.quake {
// fill: crimson;
}
</style>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/topojson.v2.min.js"></script>
<body>
<script>
var width = 960, height = 500;
var data; // declare a global variable
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.on("mousemove", mousemoved);
// NEW: Add one layer (i.e., <g> element) each for states & plot
var layer1 = svg.append("g");
var layer2 = svg.append("g");
var projection = d3.geoAlbersUsa();
var path = d3.geoPath()
.projection(projection);
// Topojson for US states
var url = "https://umbcvis.github.io/classes/class-03/us.json"
//CSV for YTS-Youth Tobacco Survey
var YTS = "https://raw.githubusercontent.com/li01012/classes/master/Project/Youth_Tobacco_Survey__YTS__Data.csv"
d3.json(url, plotStates);
function plotStates(error, json) {
// Convert topojson to an Array of GeoJSON features
// This defines "data", a global variable that can be manipulated in the developer console
data = json.objects.us.geometries.map(function(d) { return topojson.feature(json, d); })
// Plot the features (states) -- one <path> element for each state
// NEW: Plot the states in a <g> element
layer1.selectAll("path")
.data(data)
.enter()
.append("path")
.attr("d", path);
}
d3.csv(YTS, function(err, csv){
if (err) throw err;
console.log(csv[0])
});
var data = csv.features.objects(function(d) {
return [ d.geometry.coordinates[22], d.geometry.coordinates[23], d.properties.mag ];
function mousemoved() {
info.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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment