Skip to content

Instantly share code, notes, and snippets.

@JackyLiu97
Created October 10, 2019 19:52
Show Gist options
  • Save JackyLiu97/b65864dab344e9b1c4a7944c4a77319d to your computer and use it in GitHub Desktop.
Save JackyLiu97/b65864dab344e9b1c4a7944c4a77319d to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/zehayul
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin<style id="jsbin-css">
.title {
font-family: Helvetica;
font-size: 20px;
fill: black;
text-anchor: middle;
}
.dot {
stroke: black;
}
.legend--frame {
stroke: black;
fill: LightGray;
}
.legend--item--box {
stroke: black;
}
.legend--item--label {
font-family: Helvetica;
font-size: 14px;
fill: black;
alignment-baseline: central;
}
.label {
fill: black;
font-family: Helvetica;
font-size: 14px;
text-anchor: middle;
}
</style>
</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.9.2/d3.min.js"></script>
<body>
<div id="chart"></div>
<script id="jsbin-javascript">
d3.csv("https://raw.githubusercontent.com/hvo/datasets/master/nyc_grads.csv")
.then(function (grads) {
var data = grads
.filter(row => ((row.Type=='Borough Total') &&
(row.Cohort%2===0)))
.map(row => [row.Advanced/row.Total*100,
row.DroppedOut/row.Total*100,
row.Cohort]);
createPlot(data);
});
function createPlot(data) {
var canvasSize = [500, 500];
var tickSize = 5;
var pArea = [50, 30, 390, 370];
var pSize = [pArea[2]-pArea[0], pArea[3]-pArea[1]];
var palette = ['SteelBlue', 'SeaGreen', 'IndianRed'];
var canvas = d3.select("#chart")
.append("svg")
.attr('width', canvasSize[0])
.attr('height', canvasSize[1]);
var x = d3.scaleLinear()
.domain([0, 30])
.range([pArea[0], pArea[2]]);
var y = d3.scaleLinear()
.domain([0, 30])
.range([pArea[3], pArea[1]]);
var g = canvas.append("g");
g.append('text')
.attr('class', 'title')
.attr('x', (x.range()[0]+x.range()[1])*0.5)
.attr('y', 25)
.text('NYC High School Graduate Statistics');
g.append('g')
.attr('class', 'axis axis--x')
.attr('transform', `translate(0,${pArea[3]})`)
.call(d3.axisBottom(x).ticks(5, "I"))
.append('text')
.attr('class', 'label')
.attr('x', (x.range()[0]+x.range()[1])*0.5)
.attr('y', 35)
.text('Advanced Regents (%)');
g.append('g')
.attr('class', 'axis axis--y')
.attr('transform', `translate(${pArea[0]},0)`)
.call(d3.axisLeft(y).ticks(5, "I"))
.append('text')
.attr('class', 'label')
.attr('transform', 'rotate(-90)')
.attr('x', -(y.range()[0]+y.range()[1])*0.5)
.attr('y', -25)
.text('Dropped Out (%)');
var dots = g.selectAll('.dot')
.data(data);
dots.enter()
.append('circle')
.attr('class', 'dot')
.attr('cx', d => x(d[0]))
.attr('cy', d => y(d[1]))
.attr('r', 5)
.style('fill', d => palette[(d[2]-2002)/2]);
var legend = g.append('g')
.attr('transform',
`translate(${pArea[2]-60},${pArea[1]+10})`);
legend.append('rect')
.attr('class', 'legend--frame')
.attr('x', -5)
.attr('y', -5)
.attr('width', 60)
.attr('height', 60);
var legendItems = legend.selectAll('.legend--item--line')
.data([2002, 2004, 2006])
.enter().append('g');
legendItems.append('rect')
.attr('class', 'legend--item--box')
.attr('x', 0)
.attr('y', (d,i) => (i*20))
.attr('width', 10)
.attr('height', 10)
.style('fill', (d,i) => palette[i]);
legendItems.append('text')
.attr('class', 'legend--item--label')
.attr('x', 20)
.attr('y', (d,i) => (5+i*20))
.text(d => d);
}
</script>
<script id="jsbin-source-css" type="text/css">.title {
font-family: Helvetica;
font-size: 20px;
fill: black;
text-anchor: middle;
}
.dot {
stroke: black;
}
.legend--frame {
stroke: black;
fill: LightGray;
}
.legend--item--box {
stroke: black;
}
.legend--item--label {
font-family: Helvetica;
font-size: 14px;
fill: black;
alignment-baseline: central;
}
.label {
fill: black;
font-family: Helvetica;
font-size: 14px;
text-anchor: middle;
}</script>
<script id="jsbin-source-javascript" type="text/javascript">d3.csv("https://raw.githubusercontent.com/hvo/datasets/master/nyc_grads.csv")
.then(function (grads) {
var data = grads
.filter(row => ((row.Type=='Borough Total') &&
(row.Cohort%2===0)))
.map(row => [row.Advanced/row.Total*100,
row.DroppedOut/row.Total*100,
row.Cohort]);
createPlot(data);
});
function createPlot(data) {
var canvasSize = [500, 500];
var tickSize = 5;
var pArea = [50, 30, 390, 370];
var pSize = [pArea[2]-pArea[0], pArea[3]-pArea[1]];
var palette = ['SteelBlue', 'SeaGreen', 'IndianRed'];
var canvas = d3.select("#chart")
.append("svg")
.attr('width', canvasSize[0])
.attr('height', canvasSize[1]);
var x = d3.scaleLinear()
.domain([0, 30])
.range([pArea[0], pArea[2]]);
var y = d3.scaleLinear()
.domain([0, 30])
.range([pArea[3], pArea[1]]);
var g = canvas.append("g");
g.append('text')
.attr('class', 'title')
.attr('x', (x.range()[0]+x.range()[1])*0.5)
.attr('y', 25)
.text('NYC High School Graduate Statistics');
g.append('g')
.attr('class', 'axis axis--x')
.attr('transform', `translate(0,${pArea[3]})`)
.call(d3.axisBottom(x).ticks(5, "I"))
.append('text')
.attr('class', 'label')
.attr('x', (x.range()[0]+x.range()[1])*0.5)
.attr('y', 35)
.text('Advanced Regents (%)');
g.append('g')
.attr('class', 'axis axis--y')
.attr('transform', `translate(${pArea[0]},0)`)
.call(d3.axisLeft(y).ticks(5, "I"))
.append('text')
.attr('class', 'label')
.attr('transform', 'rotate(-90)')
.attr('x', -(y.range()[0]+y.range()[1])*0.5)
.attr('y', -25)
.text('Dropped Out (%)');
var dots = g.selectAll('.dot')
.data(data);
dots.enter()
.append('circle')
.attr('class', 'dot')
.attr('cx', d => x(d[0]))
.attr('cy', d => y(d[1]))
.attr('r', 5)
.style('fill', d => palette[(d[2]-2002)/2]);
var legend = g.append('g')
.attr('transform',
`translate(${pArea[2]-60},${pArea[1]+10})`);
legend.append('rect')
.attr('class', 'legend--frame')
.attr('x', -5)
.attr('y', -5)
.attr('width', 60)
.attr('height', 60);
var legendItems = legend.selectAll('.legend--item--line')
.data([2002, 2004, 2006])
.enter().append('g');
legendItems.append('rect')
.attr('class', 'legend--item--box')
.attr('x', 0)
.attr('y', (d,i) => (i*20))
.attr('width', 10)
.attr('height', 10)
.style('fill', (d,i) => palette[i]);
legendItems.append('text')
.attr('class', 'legend--item--label')
.attr('x', 20)
.attr('y', (d,i) => (5+i*20))
.text(d => d);
}</script></body>
</html>
.title {
font-family: Helvetica;
font-size: 20px;
fill: black;
text-anchor: middle;
}
.dot {
stroke: black;
}
.legend--frame {
stroke: black;
fill: LightGray;
}
.legend--item--box {
stroke: black;
}
.legend--item--label {
font-family: Helvetica;
font-size: 14px;
fill: black;
alignment-baseline: central;
}
.label {
fill: black;
font-family: Helvetica;
font-size: 14px;
text-anchor: middle;
}
d3.csv("https://raw.githubusercontent.com/hvo/datasets/master/nyc_grads.csv")
.then(function (grads) {
var data = grads
.filter(row => ((row.Type=='Borough Total') &&
(row.Cohort%2===0)))
.map(row => [row.Advanced/row.Total*100,
row.DroppedOut/row.Total*100,
row.Cohort]);
createPlot(data);
});
function createPlot(data) {
var canvasSize = [500, 500];
var tickSize = 5;
var pArea = [50, 30, 390, 370];
var pSize = [pArea[2]-pArea[0], pArea[3]-pArea[1]];
var palette = ['SteelBlue', 'SeaGreen', 'IndianRed'];
var canvas = d3.select("#chart")
.append("svg")
.attr('width', canvasSize[0])
.attr('height', canvasSize[1]);
var x = d3.scaleLinear()
.domain([0, 30])
.range([pArea[0], pArea[2]]);
var y = d3.scaleLinear()
.domain([0, 30])
.range([pArea[3], pArea[1]]);
var g = canvas.append("g");
g.append('text')
.attr('class', 'title')
.attr('x', (x.range()[0]+x.range()[1])*0.5)
.attr('y', 25)
.text('NYC High School Graduate Statistics');
g.append('g')
.attr('class', 'axis axis--x')
.attr('transform', `translate(0,${pArea[3]})`)
.call(d3.axisBottom(x).ticks(5, "I"))
.append('text')
.attr('class', 'label')
.attr('x', (x.range()[0]+x.range()[1])*0.5)
.attr('y', 35)
.text('Advanced Regents (%)');
g.append('g')
.attr('class', 'axis axis--y')
.attr('transform', `translate(${pArea[0]},0)`)
.call(d3.axisLeft(y).ticks(5, "I"))
.append('text')
.attr('class', 'label')
.attr('transform', 'rotate(-90)')
.attr('x', -(y.range()[0]+y.range()[1])*0.5)
.attr('y', -25)
.text('Dropped Out (%)');
var dots = g.selectAll('.dot')
.data(data);
dots.enter()
.append('circle')
.attr('class', 'dot')
.attr('cx', d => x(d[0]))
.attr('cy', d => y(d[1]))
.attr('r', 5)
.style('fill', d => palette[(d[2]-2002)/2]);
var legend = g.append('g')
.attr('transform',
`translate(${pArea[2]-60},${pArea[1]+10})`);
legend.append('rect')
.attr('class', 'legend--frame')
.attr('x', -5)
.attr('y', -5)
.attr('width', 60)
.attr('height', 60);
var legendItems = legend.selectAll('.legend--item--line')
.data([2002, 2004, 2006])
.enter().append('g');
legendItems.append('rect')
.attr('class', 'legend--item--box')
.attr('x', 0)
.attr('y', (d,i) => (i*20))
.attr('width', 10)
.attr('height', 10)
.style('fill', (d,i) => palette[i]);
legendItems.append('text')
.attr('class', 'legend--item--label')
.attr('x', 20)
.attr('y', (d,i) => (5+i*20))
.text(d => d);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment