Skip to content

Instantly share code, notes, and snippets.

@JackyLiu97
Created October 10, 2019 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JackyLiu97/b7e09acf5219db86d7c96dd730f25710 to your computer and use it in GitHub Desktop.
Save JackyLiu97/b7e09acf5219db86d7c96dd730f25710 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/rahicuj
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://serv.cusp.nyu.edu/~hvo/files/nyc_grads.js" type="text/javascript"></script></head>
<body>
<div id="chart"></div>
<script id="jsbin-javascript">
var data = grads
.filter(row => ((row.Type=='Borough Total') &&
(row.Cohort%2===0)))
.map(row => [row.Advanced/row.Total,
row.DroppedOut/row.Total,
row.Cohort]);
function addElementSVG(parent, name, attrs={}) {
let SVGNS = "http://www.w3.org/2000/svg";
var element = document.createElementNS(SVGNS, name);
for (var key in attrs)
element.setAttributeNS(null, key, attrs[key]);
parent.appendChild(element);
return element;
}
var canvasSize = [400, 400];
var tickSize = 5;
var pArea = [50, 30, 390, 370];
var pSize = [pArea[2]-pArea[0], pArea[3]-pArea[1]];
var chart = document.getElementById("chart");
var canvas = addElementSVG(chart, "svg", {
"width": canvasSize[0], "height":canvasSize[1],});
addElementSVG(canvas, "text", {
"x":90,
"y":25,
"style": "font-family: Helvetica;",
}).textContent = "NYC High School Graduate Statistics";
var palette = ['SteelBlue', 'SeaGreen', 'IndianRed'];
addElementSVG(canvas, "rect", {
"x": pArea[0], "y": pArea[1],
"width": pSize[0], "height": pSize[1],
"fill": "white",
});
data.forEach(function (d, i) {
addElementSVG(canvas, "circle", {
"cx": pArea[0]+d[0]/0.30*pSize[0],
"cy": pArea[3]-d[1]/0.30*pSize[1],
"r": 5,
"fill": palette[(d[2]-2002)/2],
"stroke": "black",
});
});
addElementSVG(canvas, "rect", {
"x": pArea[2]-65, "y": pArea[1]+5,
"width": 60, "height": 60,
"fill": "LightGray",
"stroke": "black",
});
[2002,2004,2006].forEach(function (d, i) {
addElementSVG(canvas, "rect", {
"x": pArea[2]-60, "y": pArea[1]+10+i*20,
"width": 10, "height": 10,
"fill": palette[(d-2002)/2],
"stroke": "black",
});
addElementSVG(canvas, "text", {
"x": pArea[2]-40, "y": pArea[1]+19+i*20,
"style": "font-family:Helvetica;font-size:12px;",
}).textContent = d;
});
addElementSVG(canvas, "path", {
"d": `M ${pArea[0]} ${pArea[1]} L ${pArea[0]} ${pArea[3]} L ${pArea[2]} ${pArea[3]}`,
"stroke": "black",
"fill": "none",
});
[1,2,3,4,5].forEach(function (d, i) {
addElementSVG(canvas, "line", {
"x1": pArea[0]+pSize[0]/6*d, "y1": pArea[3],
"x2": pArea[0]+pSize[0]/6*d, "y2": pArea[3]+tickSize,
"stroke": "black",
"fill": "none",
});
addElementSVG(canvas, "text", {
"x": pArea[0]-tickSize+pSize[0]/6*d, "y": pArea[3]+12,
"style": "font-family:Helvetica;font-size:8px;",
}).textContent = (d*5)+'%';
});
[1,2,3,4,5].forEach(function (d, i) {
addElementSVG(canvas, "line", {
"x1": pArea[0]-tickSize, "y1": pArea[3]-pSize[1]/6*d,
"x2": pArea[0], "y2": pArea[3]-pSize[1]/6*d,
"stroke": "black",
"fill": "none",
});
addElementSVG(canvas, "text", {
"x": pArea[0]-18, "y": pArea[3]-2-pSize[1]/6*d,
"style": "font-family:Helvetica;font-size:8px;",
}).textContent = (d*5)+'%';
});
xLabel = addElementSVG(canvas, "text", {
"x": pSize[0]*0.5+pArea[0]-70,
"y": pArea[3]+25,
"style": "font-family: Helvetica;font-size:14px;",
})
xLabel.textContent = "Advanced Regents (%)";
yLabel = addElementSVG(canvas, "text", {
"x": -pSize[1]*0.5+pArea[1]-95,
"y": 5,
"style": "font-family: Helvetica;font-size:14px;",
})
yLabel.textContent = "Dropped Out (%)";
yLabel.setAttribute("transform", "rotate(-90,20,0)");
</script>
<script id="jsbin-source-javascript" type="text/javascript">var data = grads
.filter(row => ((row.Type=='Borough Total') &&
(row.Cohort%2===0)))
.map(row => [row.Advanced/row.Total,
row.DroppedOut/row.Total,
row.Cohort]);
function addElementSVG(parent, name, attrs={}) {
let SVGNS = "http://www.w3.org/2000/svg";
var element = document.createElementNS(SVGNS, name);
for (var key in attrs)
element.setAttributeNS(null, key, attrs[key]);
parent.appendChild(element);
return element;
}
var canvasSize = [400, 400];
var tickSize = 5;
var pArea = [50, 30, 390, 370];
var pSize = [pArea[2]-pArea[0], pArea[3]-pArea[1]];
var chart = document.getElementById("chart");
var canvas = addElementSVG(chart, "svg", {
"width": canvasSize[0], "height":canvasSize[1],});
addElementSVG(canvas, "text", {
"x":90,
"y":25,
"style": "font-family: Helvetica;",
}).textContent = "NYC High School Graduate Statistics";
var palette = ['SteelBlue', 'SeaGreen', 'IndianRed'];
addElementSVG(canvas, "rect", {
"x": pArea[0], "y": pArea[1],
"width": pSize[0], "height": pSize[1],
"fill": "white",
});
data.forEach(function (d, i) {
addElementSVG(canvas, "circle", {
"cx": pArea[0]+d[0]/0.30*pSize[0],
"cy": pArea[3]-d[1]/0.30*pSize[1],
"r": 5,
"fill": palette[(d[2]-2002)/2],
"stroke": "black",
});
});
addElementSVG(canvas, "rect", {
"x": pArea[2]-65, "y": pArea[1]+5,
"width": 60, "height": 60,
"fill": "LightGray",
"stroke": "black",
});
[2002,2004,2006].forEach(function (d, i) {
addElementSVG(canvas, "rect", {
"x": pArea[2]-60, "y": pArea[1]+10+i*20,
"width": 10, "height": 10,
"fill": palette[(d-2002)/2],
"stroke": "black",
});
addElementSVG(canvas, "text", {
"x": pArea[2]-40, "y": pArea[1]+19+i*20,
"style": "font-family:Helvetica;font-size:12px;",
}).textContent = d;
});
addElementSVG(canvas, "path", {
"d": `M ${pArea[0]} ${pArea[1]} L ${pArea[0]} ${pArea[3]} L ${pArea[2]} ${pArea[3]}`,
"stroke": "black",
"fill": "none",
});
[1,2,3,4,5].forEach(function (d, i) {
addElementSVG(canvas, "line", {
"x1": pArea[0]+pSize[0]/6*d, "y1": pArea[3],
"x2": pArea[0]+pSize[0]/6*d, "y2": pArea[3]+tickSize,
"stroke": "black",
"fill": "none",
});
addElementSVG(canvas, "text", {
"x": pArea[0]-tickSize+pSize[0]/6*d, "y": pArea[3]+12,
"style": "font-family:Helvetica;font-size:8px;",
}).textContent = (d*5)+'%';
});
[1,2,3,4,5].forEach(function (d, i) {
addElementSVG(canvas, "line", {
"x1": pArea[0]-tickSize, "y1": pArea[3]-pSize[1]/6*d,
"x2": pArea[0], "y2": pArea[3]-pSize[1]/6*d,
"stroke": "black",
"fill": "none",
});
addElementSVG(canvas, "text", {
"x": pArea[0]-18, "y": pArea[3]-2-pSize[1]/6*d,
"style": "font-family:Helvetica;font-size:8px;",
}).textContent = (d*5)+'%';
});
xLabel = addElementSVG(canvas, "text", {
"x": pSize[0]*0.5+pArea[0]-70,
"y": pArea[3]+25,
"style": "font-family: Helvetica;font-size:14px;",
})
xLabel.textContent = "Advanced Regents (%)";
yLabel = addElementSVG(canvas, "text", {
"x": -pSize[1]*0.5+pArea[1]-95,
"y": 5,
"style": "font-family: Helvetica;font-size:14px;",
})
yLabel.textContent = "Dropped Out (%)";
yLabel.setAttribute("transform", "rotate(-90,20,0)");
</script></body>
</html>
var data = grads
.filter(row => ((row.Type=='Borough Total') &&
(row.Cohort%2===0)))
.map(row => [row.Advanced/row.Total,
row.DroppedOut/row.Total,
row.Cohort]);
function addElementSVG(parent, name, attrs={}) {
let SVGNS = "http://www.w3.org/2000/svg";
var element = document.createElementNS(SVGNS, name);
for (var key in attrs)
element.setAttributeNS(null, key, attrs[key]);
parent.appendChild(element);
return element;
}
var canvasSize = [400, 400];
var tickSize = 5;
var pArea = [50, 30, 390, 370];
var pSize = [pArea[2]-pArea[0], pArea[3]-pArea[1]];
var chart = document.getElementById("chart");
var canvas = addElementSVG(chart, "svg", {
"width": canvasSize[0], "height":canvasSize[1],});
addElementSVG(canvas, "text", {
"x":90,
"y":25,
"style": "font-family: Helvetica;",
}).textContent = "NYC High School Graduate Statistics";
var palette = ['SteelBlue', 'SeaGreen', 'IndianRed'];
addElementSVG(canvas, "rect", {
"x": pArea[0], "y": pArea[1],
"width": pSize[0], "height": pSize[1],
"fill": "white",
});
data.forEach(function (d, i) {
addElementSVG(canvas, "circle", {
"cx": pArea[0]+d[0]/0.30*pSize[0],
"cy": pArea[3]-d[1]/0.30*pSize[1],
"r": 5,
"fill": palette[(d[2]-2002)/2],
"stroke": "black",
});
});
addElementSVG(canvas, "rect", {
"x": pArea[2]-65, "y": pArea[1]+5,
"width": 60, "height": 60,
"fill": "LightGray",
"stroke": "black",
});
[2002,2004,2006].forEach(function (d, i) {
addElementSVG(canvas, "rect", {
"x": pArea[2]-60, "y": pArea[1]+10+i*20,
"width": 10, "height": 10,
"fill": palette[(d-2002)/2],
"stroke": "black",
});
addElementSVG(canvas, "text", {
"x": pArea[2]-40, "y": pArea[1]+19+i*20,
"style": "font-family:Helvetica;font-size:12px;",
}).textContent = d;
});
addElementSVG(canvas, "path", {
"d": `M ${pArea[0]} ${pArea[1]} L ${pArea[0]} ${pArea[3]} L ${pArea[2]} ${pArea[3]}`,
"stroke": "black",
"fill": "none",
});
[1,2,3,4,5].forEach(function (d, i) {
addElementSVG(canvas, "line", {
"x1": pArea[0]+pSize[0]/6*d, "y1": pArea[3],
"x2": pArea[0]+pSize[0]/6*d, "y2": pArea[3]+tickSize,
"stroke": "black",
"fill": "none",
});
addElementSVG(canvas, "text", {
"x": pArea[0]-tickSize+pSize[0]/6*d, "y": pArea[3]+12,
"style": "font-family:Helvetica;font-size:8px;",
}).textContent = (d*5)+'%';
});
[1,2,3,4,5].forEach(function (d, i) {
addElementSVG(canvas, "line", {
"x1": pArea[0]-tickSize, "y1": pArea[3]-pSize[1]/6*d,
"x2": pArea[0], "y2": pArea[3]-pSize[1]/6*d,
"stroke": "black",
"fill": "none",
});
addElementSVG(canvas, "text", {
"x": pArea[0]-18, "y": pArea[3]-2-pSize[1]/6*d,
"style": "font-family:Helvetica;font-size:8px;",
}).textContent = (d*5)+'%';
});
xLabel = addElementSVG(canvas, "text", {
"x": pSize[0]*0.5+pArea[0]-70,
"y": pArea[3]+25,
"style": "font-family: Helvetica;font-size:14px;",
})
xLabel.textContent = "Advanced Regents (%)";
yLabel = addElementSVG(canvas, "text", {
"x": -pSize[1]*0.5+pArea[1]-95,
"y": 5,
"style": "font-family: Helvetica;font-size:14px;",
})
yLabel.textContent = "Dropped Out (%)";
yLabel.setAttribute("transform", "rotate(-90,20,0)");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment