Skip to content

Instantly share code, notes, and snippets.

@mvacha
Created November 15, 2018 12:48
Show Gist options
  • Save mvacha/0c4877d24932782b7702f689bb6616e8 to your computer and use it in GitHub Desktop.
Save mvacha/0c4877d24932782b7702f689bb6616e8 to your computer and use it in GitHub Desktop.
d3.select(window).on("resize", resize);
var margin = {top: 20, right: 20, bottom: 30, left: 40};
var viewWidth = window.innerWidth;
var viewHeight = window.innerHeight;
var width = viewWidth - margin.left - margin.right;
var height = viewHeight - margin.top - margin.bottom;
data = boat_data.boats;
var x = d3.scaleLinear()
.range([0, width])
.domain(d3.extent(data, d => d.x))
var y = d3.scaleLinear()
.range([height, 0])
.domain([0, d3.max(data, d => d.y)])
var svg = d3.select("svg")
.attr("width", viewWidth)
.attr("height", viewHeight)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
function drawScatterplot() {
svg.selectAll(".dot")
.data(data).enter().append("circle")
.attr("class", "dot")
.attr("r", 5)
.attr("cx", d => x(d.x) )
.attr("cy", d => y(d.y) )
svg.append("g")
.attr("class", "x-axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
svg.append("g")
.attr("class", "y-axis")
.call(d3.axisLeft(y))
}
function resize() {
viewWidth = window.innerWidth
viewHeight = window.innerHeight
width = viewWidth - margin.left - margin.right;
height = viewHeight - margin.top - margin.bottom;
var s = d3.select("svg")
.attr("width", viewWidth)
.attr("height", viewHeight)
x.range([0, width])
y.range([height, 0])
s.select(".x-axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
s.select(".y-axis")
.call(d3.axisLeft(y))
s.selectAll(".dot")
.attr("r", 5)
.attr("cx", d => x(d.x) )
.attr("cy", d => y(d.y) )
}
drawScatterplot();
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////// ADDITIONAL TASKS ///////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
/*
Once that you have implemented your basic scatterplot you can work on new features
* Color coding of the points based on a third attribute
* Legend for the third attribute with color scale
* Interactive selection of the 3 attributes visualized in the scatterplot
* Resizing of the window updates the scatterplot
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment