Skip to content

Instantly share code, notes, and snippets.

@sabeehtaqi
Created March 16, 2017 22:59
Show Gist options
  • Save sabeehtaqi/a9e296f9c05b325e4d53150b0e10e297 to your computer and use it in GitHub Desktop.
Save sabeehtaqi/a9e296f9c05b325e4d53150b0e10e297 to your computer and use it in GitHub Desktop.
Homework 6 (Another way to do it)
license: mit
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.chart rect {
fill: seagreen;
}
.legend {
font: 10px sans-serif;
}
body {
font: 12px sans-serif;
font-weight: bold;
}
path {
stroke:steelblue;
stroke-width: 2;
fill: none;
}
.axis text {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.chart text {
font: 12px sans-serif;
}
.dot {
stroke: #000;
}
</style>
<body>
<svg class = "chart"></svg>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var quakes; //global variable
var url = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson";
var margin = {top: 20, right: 30, bottom: 40, left: 40},
width = 960 - margin.left - margin.right,
height = 520 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width])
var y = d3.scale.linear()
.range([height, 0]);
//Line generator
var line = d3.svg.line()
.x(function(d) { return x(d.x); })
.y(function(d) { return y(d.y); });
//Set colors based on 10 colors
var color = d3.scale.category10();
var xAxis = d3.svg.axis()
.scale(x)
//.tickValues ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
var chart = d3.select(".chart")//.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
//svg.call(tip);
d3.json(url, function(error, data) {
if (error) console.log(error);
console.log("Data from USGS:", data);
quakes = data
quakesMag = [];
quakesMag = data.features.map(function(d) { return d.properties.mag; });
console.log("Earthquake Magnitude:", quakesMag);
quakesBin = []; // empty array to bin the mag values
for (var i = 0; i < 10; i++) {
quakesBin[i] = quakesMag.filter(function(d) { return d >= i && d < i + 1;}).length;
}
x.domain([0, quakesBin.length]);
y.domain([d3.min(quakesBin, function(d) { return d; }), d3.max (quakesBin, function (d) { return d; })]);
//y.domain(d3.extent(quakesBin));
//var barWith = width / quakesMag.length;
//Create x axis
chart.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
//Append title for the Legend
chart.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -7)
.style("text-anchor", "end")
.text("Legend");
//Create x axis
chart.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
//Append for x axis label
chart.append("text")
.attr("text-anchor", "middle")
.attr("x", width/2)
.attr("y", height + 27)
.text("Earthquake Magnitude");
//Create y axis
chart.append("g")
.attr("class", "y axis")
.call(yAxis);
//Append for y axis label
chart.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -height/2)
.attr("y", -1 - margin.left/3)
.attr("dy", "-1.2em")
.attr("text-anchor", "middle")
.text("Earthquake Frequency");
//Append for the Title
chart.append("text")
.attr("text-anchor", "middle")
.attr("x", width/2)
.attr("y", -margin.top/2)
.attr("dy", ".5em")
.text("Earthquake Occurences - Last 2.5 Days");
//Plot the dots
chart.selectAll(".dot")
.data(quakesBin)
.enter().append("circle")
.attr("class", "dot")
.attr("r", 4) //Radius of 4
.attr("cx", function(d, i) { return x(i); })
.attr("cy", function(d) { return y(d); })
//.attr("height", function(d) { return height - y(d); })
//.attr("width", barWith)
.style("fill", function(d) { return color(d); });
//Create Legend
var legend = chart.selectAll(".legend")
.data(color.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate (0, " + i * 20 + ")"; });
//Append Legend
legend.append("rect")
.attr("x", width - 20)
.attr("width", 20)
.attr("height", 20)
.style("fill", color);
//Append label for frequency
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; })
// Create an array of objects that will be plotted as a line
var lineData = quakesBin.map(function(d, i) {
var obj = {};
obj.x = i;
obj.y = d;
return obj;
});
//Plot the data line
chart.append("path")
.datum(lineData)
.attr("class", "line")
.attr("d", line);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment