Skip to content

Instantly share code, notes, and snippets.

@jeremy6462
Created February 1, 2018 18:36
Show Gist options
  • Save jeremy6462/d92f72110c8a4c6c67e84fb85a88ca4f to your computer and use it in GitHub Desktop.
Save jeremy6462/d92f72110c8a4c6c67e84fb85a88ca4f to your computer and use it in GitHub Desktop.
Homework 1 for CS 590V at UMass Amherst
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://d3js.org/d3.v4.min.js"></script>
<title>NBA Salaries Scatter Plot</title>
<style>
body {
margin: 0px
}
.axis-label {
fill: black;
font-size: 3em;
}
</style>
</head>
<body>
<svg width="960" height="500"></svg> <!-- Bl.ocks.org size-->
<script>
const svg = d3.select('svg');
xLabel = "NBA Season (in years)"
yLabel = "Salary (in USD)"
const margin = { left: 110,
right: 20,
top: 20,
bottom: 100
};
const width = svg.attr('width') - margin.left - margin.right;
const height = svg.attr('height') - margin.top - margin.bottom;
const g = svg.append('g')
.attr('transform', `translate(${margin.left}, ${margin.top})`);
const xAxisG = g.append('g')
.attr('transform', `translate(0, ${height})`);
const yAxisG = g.append('g');
xAxisG.append('text')
.attr('class', 'axis-label')
.attr('x', width/2)
.attr('y', 50)
.text(xLabel);
yAxisG.append('text')
.attr('class', 'axis-label')
.attr('x', -height/2 + 70)
.attr('y', -70)
.attr('transform', 'rotate(-90)')
.text(yLabel);
const xScale = d3.scaleLinear();
const yScale = d3.scaleLinear();
const xAxis = d3.axisBottom().scale(xScale);
const yAxis = d3.axisLeft().scale(yScale);
const row = d => {
d.salary = +d.salary;
return d;
};
d3.csv('nba_salaries.csv', row, data => {
xScale
.domain([
d3.min(data, d => d.season_start),
d3.max(data, d => d.season_start)
])
.range([0, width]);
yScale
.domain([
d3.min(data, d => d.salary),
d3.max(data, d => d.salary)
])
.range([height, 0]);
g.selectAll('circle').data(data)
.enter().append('circle')
.attr('cx', d => xScale(d.season_start))
.attr('cy', d => yScale(d.salary))
.attr('r', d => 2);
xAxisG.call(xAxis);
yAxisG.call(yAxis);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment