Last active
January 31, 2019 21:19
-
-
Save Ewiseman/49785c6e9b223fe2e733b65d0154e789 to your computer and use it in GitHub Desktop.
Scatter Plot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
var margin = {top: 20, right: 20, bottom: 160, left: 40}, | |
width = $("#scatter-plot").width() - margin.left - margin.right, | |
height = 600 - margin.top - margin.bottom; | |
// setup x | |
var xValue = function(d) { return d.acres;}, // data -> value | |
xScale = d3.scale.linear().range([0, width]), // value -> display | |
xMap = function(d) { return xScale(xValue(d));}, // data -> display | |
xAxis = d3.svg.axis().scale(xScale).orient("bottom"); | |
// setup y | |
var yValue = function(d) { return d.vertical;}, // data -> value | |
yScale = d3.scale.linear().range([height, 0]), // value -> display | |
yMap = function(d) { return yScale(yValue(d));}, // data -> display | |
yAxis = d3.svg.axis().scale(yScale).orient("left"); | |
// setup fill color | |
var cValue = function(d) { return d.state;}, | |
color = d3.scale.category20(); | |
// add the graph canvas to the #scatter-plot of the webpage | |
var svg = d3.select("#scatter-plot").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 + ")"); | |
var stateColor = "state"; | |
var colorScale = d3.scale.category20c (); | |
// add the tooltip area to the webpage | |
var tooltip = d3.select("body").append("div") | |
.attr("class", "tooltip") | |
.style("opacity", 0); | |
var acreageThreshold = 1000 ; //not greater than 4000 | |
// load data | |
d3.csv("/ski_resort_acres_three.csv", function(error, data) { | |
data = jQuery.grep(data, function(d, index) { | |
var is_viewable = d.acres <= acreageThreshold; | |
return is_viewable; | |
}); | |
// change string (from CSV) into number format | |
data.forEach(function(d) { | |
d.acres = +d.acres; | |
d.vertical = +d.vertical; | |
}); | |
// don't want dots overlapping axis, so add in buffer to data domain | |
xScale.domain([d3.min(data, xValue)-1, d3.max(data, xValue)+1]); | |
yScale.domain([d3.min(data, yValue)-1, d3.max(data, yValue)+1]); | |
// x-axis | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis) | |
.append("text") | |
.attr("class", "label") | |
.attr("x", width) | |
.attr("y", -6) | |
.style("text-anchor", "end") | |
.text("Acres"); | |
// y-axis | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(yAxis) | |
.append("text") | |
.attr("class", "label") | |
.attr("transform", "rotate(-90)") | |
.attr("y", 6) | |
.attr("dy", ".71em") | |
.style("text-anchor", "end") | |
.text("Vertical Drop"); | |
// draw dots | |
svg.selectAll(".dot") | |
.data(data) | |
.enter().append("circle") | |
.attr("class", "dot") | |
.attr("r", function(d) { return Math.sqrt(d.acres)/5; }) | |
.attr("cx", xMap) | |
.attr("cy", yMap) | |
.style("fill", function(d) { return color(cValue(d));}) | |
.style("opacity", .6) | |
.style("stroke-opacity", .3) | |
.on("mouseover", function(d) { | |
circle = d3.select(this) | |
tooltip.transition() | |
.duration(20) | |
.style("opacity", 1) | |
circle.transition() | |
.duration(100) | |
.style("opacity", 1) | |
.style("stroke-opacity", 1) | |
tooltip.html(d.resort_name + "<br/>" + addCommas(d.acres) + " ac / " + addCommas(d.vertical) + " ft") | |
.style("left", (d3.event.pageX) + "px") | |
.style("top", (d3.event.pageY-28) + "px"); | |
}) | |
.on("mouseout", function(d) { | |
tooltip.transition() | |
.duration(500) | |
.style("opacity", 0) | |
circle.transition() | |
.duration(200) | |
.style("opacity", .6) | |
.style("stroke-opacity", .3) | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment