Last active
June 4, 2020 20:27
-
-
Save pg1647/4acaaa81e6feab396a0de07ac4c26067 to your computer and use it in GitHub Desktop.
Data Vis. Summer 2020 || Lab 2 Demo example in Javascript // source https://jsbin.com/quxafos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>DV2 - Lab</title> | |
<script src="https://serv.cusp.nyu.edu/~hvo/files/nyc_grads.js" type="text/javascript"></script><style id="jsbin-css"> | |
#plotTitle { | |
font-family: Helvetica; | |
font-size: 16px; | |
} | |
.axisLabel { | |
font-family: Helvetica; | |
font-size: 14px; | |
} | |
.legendText { | |
font-family: Helvetica; | |
font-size: 12px; | |
} | |
.tickLabel { | |
font-family: Helvetica; | |
font-size: 8px; | |
} | |
#background { | |
fill: white; | |
} | |
#legendBox { | |
fill: LightGray; | |
stroke: black; | |
} | |
.axis,.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"> | |
function addElementSVG(parent, name, attrs={}) { | |
let SVGNS = "http://www.w3.org/2000/svg"; | |
let element = document.createElementNS(SVGNS, name); | |
for (var key in attrs) | |
element.setAttributeNS(null, key, attrs[key]); | |
parent.appendChild(element); | |
return element; | |
} | |
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]); | |
const canvasSize = [400, 400]; | |
const tickSize = 5; | |
const pArea = [50, 30, 390, 370]; | |
const 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", { | |
"id": "plotTitle", | |
"x":90, | |
"y":25, | |
}).textContent = "NYC High School Graduate Statistics"; | |
var palette = ['SteelBlue', 'SeaGreen', 'IndianRed']; | |
addElementSVG(canvas, "rect", { | |
"id": "background", | |
"x": pArea[0], "y": pArea[1], | |
"width": pSize[0], "height": pSize[1], | |
}); | |
data.forEach(function (d, i) { | |
addElementSVG(canvas, "circle", { | |
"class": `c${d[2]} dot`, | |
"cx": pArea[0]+d[0]/0.30*pSize[0], | |
"cy": pArea[3]-d[1]/0.30*pSize[1], | |
"r": 5, | |
}); | |
}); | |
addElementSVG(canvas, "rect", { | |
"id": "legendBox", | |
"x": pArea[2]-65, "y": pArea[1]+5, | |
"width": 60, "height": 60, | |
}); | |
[2002,2004,2006].forEach((d, i) => { | |
addElementSVG(canvas, "rect", { | |
"class": `c${d} legendItem`, | |
"x": pArea[2]-60, "y": pArea[1]+10+i*20, | |
"width": 10, "height": 10, | |
}); | |
addElementSVG(canvas, "text", { | |
"class": "legendText", | |
"x": pArea[2]-40, "y": pArea[1]+19+i*20, | |
}).textContent = d; | |
}); | |
addElementSVG(canvas, "path", { | |
"class": "axis", | |
"d": `M ${pArea[0]} ${pArea[1]} L ${pArea[0]} ${pArea[3]} L ${pArea[2]} ${pArea[3]}`, | |
}); | |
[1,2,3,4,5].forEach(d => { | |
addElementSVG(canvas, "line", { | |
"class": "tickMark", | |
"x1": pArea[0]+pSize[0]/6*d, "y1": pArea[3], | |
"x2": pArea[0]+pSize[0]/6*d, "y2": pArea[3]+tickSize, | |
}); | |
addElementSVG(canvas, "text", { | |
"class": "tickLabel", | |
"x": pArea[0]-tickSize+pSize[0]/6*d, "y": pArea[3]+12, | |
}).textContent = (d*5)+'%'; | |
}); | |
[1,2,3,4,5].forEach(function (d, i) { | |
addElementSVG(canvas, "line", { | |
"class": "tickMark", | |
"x1": pArea[0]-tickSize, "y1": pArea[3]-pSize[1]/6*d, | |
"x2": pArea[0], "y2": pArea[3]-pSize[1]/6*d, | |
}); | |
addElementSVG(canvas, "text", { | |
"class": "tickLabel", | |
"x": pArea[0]-18, "y": pArea[3]-2-pSize[1]/6*d, | |
}).textContent = (d*5)+'%'; | |
}); | |
addElementSVG(canvas, "text", { | |
"class": "axisLabel", | |
"x": pSize[0]*0.5+pArea[0]-70, | |
"y": pArea[3]+25, | |
}).textContent = "Advanced Regents (%)"; | |
addElementSVG(canvas, "text", { | |
"class": "axisLabel", | |
"x": -pSize[1]*0.5+pArea[1]-95, | |
"y": 5, | |
"transform": "rotate(-90,20,0)", | |
}).textContent = "Dropped Out (%)"; | |
</script> | |
<script id="jsbin-source-css" type="text/css">#plotTitle { | |
font-family: Helvetica; | |
font-size: 16px; | |
} | |
.axisLabel { | |
font-family: Helvetica; | |
font-size: 14px; | |
} | |
.legendText { | |
font-family: Helvetica; | |
font-size: 12px; | |
} | |
.tickLabel { | |
font-family: Helvetica; | |
font-size: 8px; | |
} | |
#background { | |
fill: white; | |
} | |
#legendBox { | |
fill: LightGray; | |
stroke: black; | |
} | |
.axis,.tickMark { | |
stroke: black; | |
fill: none; | |
} | |
.dot,.legendItem { | |
stroke: black; | |
} | |
.c2002 { | |
fill: SteelBlue; | |
} | |
.c2004 { | |
fill: SeaGreen; | |
} | |
.c2006 { | |
fill: IndianRed; | |
} | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">function addElementSVG(parent, name, attrs={}) { | |
let SVGNS = "http://www.w3.org/2000/svg"; | |
let element = document.createElementNS(SVGNS, name); | |
for (var key in attrs) | |
element.setAttributeNS(null, key, attrs[key]); | |
parent.appendChild(element); | |
return element; | |
} | |
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]); | |
const canvasSize = [400, 400]; | |
const tickSize = 5; | |
const pArea = [50, 30, 390, 370]; | |
const 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", { | |
"id": "plotTitle", | |
"x":90, | |
"y":25, | |
}).textContent = "NYC High School Graduate Statistics"; | |
var palette = ['SteelBlue', 'SeaGreen', 'IndianRed']; | |
addElementSVG(canvas, "rect", { | |
"id": "background", | |
"x": pArea[0], "y": pArea[1], | |
"width": pSize[0], "height": pSize[1], | |
}); | |
data.forEach(function (d, i) { | |
addElementSVG(canvas, "circle", { | |
"class": `c${d[2]} dot`, | |
"cx": pArea[0]+d[0]/0.30*pSize[0], | |
"cy": pArea[3]-d[1]/0.30*pSize[1], | |
"r": 5, | |
}); | |
}); | |
addElementSVG(canvas, "rect", { | |
"id": "legendBox", | |
"x": pArea[2]-65, "y": pArea[1]+5, | |
"width": 60, "height": 60, | |
}); | |
[2002,2004,2006].forEach((d, i) => { | |
addElementSVG(canvas, "rect", { | |
"class": `c${d} legendItem`, | |
"x": pArea[2]-60, "y": pArea[1]+10+i*20, | |
"width": 10, "height": 10, | |
}); | |
addElementSVG(canvas, "text", { | |
"class": "legendText", | |
"x": pArea[2]-40, "y": pArea[1]+19+i*20, | |
}).textContent = d; | |
}); | |
addElementSVG(canvas, "path", { | |
"class": "axis", | |
"d": `M ${pArea[0]} ${pArea[1]} L ${pArea[0]} ${pArea[3]} L ${pArea[2]} ${pArea[3]}`, | |
}); | |
[1,2,3,4,5].forEach(d => { | |
addElementSVG(canvas, "line", { | |
"class": "tickMark", | |
"x1": pArea[0]+pSize[0]/6*d, "y1": pArea[3], | |
"x2": pArea[0]+pSize[0]/6*d, "y2": pArea[3]+tickSize, | |
}); | |
addElementSVG(canvas, "text", { | |
"class": "tickLabel", | |
"x": pArea[0]-tickSize+pSize[0]/6*d, "y": pArea[3]+12, | |
}).textContent = (d*5)+'%'; | |
}); | |
[1,2,3,4,5].forEach(function (d, i) { | |
addElementSVG(canvas, "line", { | |
"class": "tickMark", | |
"x1": pArea[0]-tickSize, "y1": pArea[3]-pSize[1]/6*d, | |
"x2": pArea[0], "y2": pArea[3]-pSize[1]/6*d, | |
}); | |
addElementSVG(canvas, "text", { | |
"class": "tickLabel", | |
"x": pArea[0]-18, "y": pArea[3]-2-pSize[1]/6*d, | |
}).textContent = (d*5)+'%'; | |
}); | |
addElementSVG(canvas, "text", { | |
"class": "axisLabel", | |
"x": pSize[0]*0.5+pArea[0]-70, | |
"y": pArea[3]+25, | |
}).textContent = "Advanced Regents (%)"; | |
addElementSVG(canvas, "text", { | |
"class": "axisLabel", | |
"x": -pSize[1]*0.5+pArea[1]-95, | |
"y": 5, | |
"transform": "rotate(-90,20,0)", | |
}).textContent = "Dropped Out (%)"; | |
</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#plotTitle { | |
font-family: Helvetica; | |
font-size: 16px; | |
} | |
.axisLabel { | |
font-family: Helvetica; | |
font-size: 14px; | |
} | |
.legendText { | |
font-family: Helvetica; | |
font-size: 12px; | |
} | |
.tickLabel { | |
font-family: Helvetica; | |
font-size: 8px; | |
} | |
#background { | |
fill: white; | |
} | |
#legendBox { | |
fill: LightGray; | |
stroke: black; | |
} | |
.axis,.tickMark { | |
stroke: black; | |
fill: none; | |
} | |
.dot,.legendItem { | |
stroke: black; | |
} | |
.c2002 { | |
fill: SteelBlue; | |
} | |
.c2004 { | |
fill: SeaGreen; | |
} | |
.c2006 { | |
fill: IndianRed; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function addElementSVG(parent, name, attrs={}) { | |
let SVGNS = "http://www.w3.org/2000/svg"; | |
let element = document.createElementNS(SVGNS, name); | |
for (var key in attrs) | |
element.setAttributeNS(null, key, attrs[key]); | |
parent.appendChild(element); | |
return element; | |
} | |
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]); | |
const canvasSize = [400, 400]; | |
const tickSize = 5; | |
const pArea = [50, 30, 390, 370]; | |
const 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", { | |
"id": "plotTitle", | |
"x":90, | |
"y":25, | |
}).textContent = "NYC High School Graduate Statistics"; | |
var palette = ['SteelBlue', 'SeaGreen', 'IndianRed']; | |
addElementSVG(canvas, "rect", { | |
"id": "background", | |
"x": pArea[0], "y": pArea[1], | |
"width": pSize[0], "height": pSize[1], | |
}); | |
data.forEach(function (d, i) { | |
addElementSVG(canvas, "circle", { | |
"class": `c${d[2]} dot`, | |
"cx": pArea[0]+d[0]/0.30*pSize[0], | |
"cy": pArea[3]-d[1]/0.30*pSize[1], | |
"r": 5, | |
}); | |
}); | |
addElementSVG(canvas, "rect", { | |
"id": "legendBox", | |
"x": pArea[2]-65, "y": pArea[1]+5, | |
"width": 60, "height": 60, | |
}); | |
[2002,2004,2006].forEach((d, i) => { | |
addElementSVG(canvas, "rect", { | |
"class": `c${d} legendItem`, | |
"x": pArea[2]-60, "y": pArea[1]+10+i*20, | |
"width": 10, "height": 10, | |
}); | |
addElementSVG(canvas, "text", { | |
"class": "legendText", | |
"x": pArea[2]-40, "y": pArea[1]+19+i*20, | |
}).textContent = d; | |
}); | |
addElementSVG(canvas, "path", { | |
"class": "axis", | |
"d": `M ${pArea[0]} ${pArea[1]} L ${pArea[0]} ${pArea[3]} L ${pArea[2]} ${pArea[3]}`, | |
}); | |
[1,2,3,4,5].forEach(d => { | |
addElementSVG(canvas, "line", { | |
"class": "tickMark", | |
"x1": pArea[0]+pSize[0]/6*d, "y1": pArea[3], | |
"x2": pArea[0]+pSize[0]/6*d, "y2": pArea[3]+tickSize, | |
}); | |
addElementSVG(canvas, "text", { | |
"class": "tickLabel", | |
"x": pArea[0]-tickSize+pSize[0]/6*d, "y": pArea[3]+12, | |
}).textContent = (d*5)+'%'; | |
}); | |
[1,2,3,4,5].forEach(function (d, i) { | |
addElementSVG(canvas, "line", { | |
"class": "tickMark", | |
"x1": pArea[0]-tickSize, "y1": pArea[3]-pSize[1]/6*d, | |
"x2": pArea[0], "y2": pArea[3]-pSize[1]/6*d, | |
}); | |
addElementSVG(canvas, "text", { | |
"class": "tickLabel", | |
"x": pArea[0]-18, "y": pArea[3]-2-pSize[1]/6*d, | |
}).textContent = (d*5)+'%'; | |
}); | |
addElementSVG(canvas, "text", { | |
"class": "axisLabel", | |
"x": pSize[0]*0.5+pArea[0]-70, | |
"y": pArea[3]+25, | |
}).textContent = "Advanced Regents (%)"; | |
addElementSVG(canvas, "text", { | |
"class": "axisLabel", | |
"x": -pSize[1]*0.5+pArea[1]-95, | |
"y": 5, | |
"transform": "rotate(-90,20,0)", | |
}).textContent = "Dropped Out (%)"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment