Skip to content

Instantly share code, notes, and snippets.

@angelialau
Last active June 11, 2020 20:49
Show Gist options
  • Save angelialau/bb7762643bc527cf8be878af2f78d59c to your computer and use it in GitHub Desktop.
Save angelialau/bb7762643bc527cf8be878af2f78d59c to your computer and use it in GitHub Desktop.
DV3 - Lab// source https://jsbin.com/zuwugup
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>DV3 - Lab</title>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<style id="jsbin-css">
#plotTitle {
font-family: Helvetica;
font-size: 16px;
}
.axisLabel {
font-family: Helvetica;
font-size: 14px;
fill: Black;
text-anchor:middle;
}
.legendText {
font-family: Helvetica;
font-size: 12px;
}
.tickLabel {
font-family: Helvetica;
font-size: 8px;
}
#background {
fill: white;
}
#legendBox {
fill: LightGray;
stroke: black;
}
.tickMark {
stroke: black;
fill: none;
}
.dot,.legendItem {
stroke: black;
}
.c2002 {
fill: SteelBlue;
}
.c2004 {
fill: SeaGreen;
}
.c2006 {
fill: IndianRed;
}
</style>
</head>
<body>
<div id="chart"></div>
<script id="jsbin-javascript">
d3.csv('https://raw.githubusercontent.com/hvo/datasets/master/nyc_grads.csv')
.then(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,
row.Borough
]);
createPlot(data);
});
function createPlot(data){
let byCohort = d3.nest()
.key(d=>d[2]) // groupby the third element ie year
.entries(data);
let colors = ['SteelBlue', 'SeaGreen', 'IndianRed'];
// create a list of traces, where each trace is one data series:
let traces = byCohort.map((cohort, i) => ({
// convert each key and value into x and y that plotly detects
x: cohort.values.map(d=>d[0]), // % Advanced
y: cohort.values.map(d=>d[1]), // % droppedOut
mode: 'markers',
type: 'scatter',
name: cohort.key,
marker: {
color: colors[i],
size: 10,
line: {
color: 'black',
width: 1
},
}
}));
let layout = {
title: 'NYC High School Graduate Statisics',
width: 500,
height: 500,
xaxis: {
title: 'Advanced Regents (%)',
range:[0, 30],
nticks:7,
ticks: 'outside',
mirror: true, // top border
linewidth: 1,
},
yaxis: {
title: 'Dropped Out (%)',
range:[0, 30],
nticks:7,
ticks: 'outside',
mirror: true, // right border
linewidth: 1,
}
}
Plotly.newPlot('chart', traces, layout);
// ^ same as
// let chart = d3.select('#chart').node();
// Plotly.newPlot(chart, traces, layout);
}
</script>
<script id="jsbin-source-css" type="text/css">#plotTitle {
font-family: Helvetica;
font-size: 16px;
}
.axisLabel {
font-family: Helvetica;
font-size: 14px;
fill: Black;
text-anchor:middle;
}
.legendText {
font-family: Helvetica;
font-size: 12px;
}
.tickLabel {
font-family: Helvetica;
font-size: 8px;
}
#background {
fill: white;
}
#legendBox {
fill: LightGray;
stroke: black;
}
.tickMark {
stroke: black;
fill: none;
}
.dot,.legendItem {
stroke: black;
}
.c2002 {
fill: SteelBlue;
}
.c2004 {
fill: SeaGreen;
}
.c2006 {
fill: IndianRed;
}
</script>
</body>
</html>
#plotTitle {
font-family: Helvetica;
font-size: 16px;
}
.axisLabel {
font-family: Helvetica;
font-size: 14px;
fill: Black;
text-anchor:middle;
}
.legendText {
font-family: Helvetica;
font-size: 12px;
}
.tickLabel {
font-family: Helvetica;
font-size: 8px;
}
#background {
fill: white;
}
#legendBox {
fill: LightGray;
stroke: black;
}
.tickMark {
stroke: black;
fill: none;
}
.dot,.legendItem {
stroke: black;
}
.c2002 {
fill: SteelBlue;
}
.c2004 {
fill: SeaGreen;
}
.c2006 {
fill: IndianRed;
}
d3.csv('https://raw.githubusercontent.com/hvo/datasets/master/nyc_grads.csv')
.then(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,
row.Borough
]);
createPlot(data);
});
function createPlot(data){
let byCohort = d3.nest()
.key(d=>d[2]) // groupby the third element ie year
.entries(data);
let colors = ['SteelBlue', 'SeaGreen', 'IndianRed'];
// create a list of traces, where each trace is one data series:
let traces = byCohort.map((cohort, i) => ({
// convert each key and value into x and y that plotly detects
x: cohort.values.map(d=>d[0]), // % Advanced
y: cohort.values.map(d=>d[1]), // % droppedOut
mode: 'markers',
type: 'scatter',
name: cohort.key,
marker: {
color: colors[i],
size: 10,
line: {
color: 'black',
width: 1
},
}
}));
let layout = {
title: 'NYC High School Graduate Statisics',
width: 500,
height: 500,
xaxis: {
title: 'Advanced Regents (%)',
range:[0, 30],
nticks:7,
ticks: 'outside',
mirror: true, // top border
linewidth: 1,
},
yaxis: {
title: 'Dropped Out (%)',
range:[0, 30],
nticks:7,
ticks: 'outside',
mirror: true, // right border
linewidth: 1,
}
}
Plotly.newPlot('chart', traces, layout);
// ^ same as
// let chart = d3.select('#chart').node();
// Plotly.newPlot(chart, traces, layout);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment