Trying to figure out categorical historgrams
Last active
June 6, 2017 15:02
-
-
Save johnnygizmo/35e1837d7f27d5d74e51eb9cf39adf19 to your computer and use it in GitHub Desktop.
Grade Histogram
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
height: 300 |
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
["A","A","A","A-","B+", | |
"B","B","B-","B-","C+", | |
"C","C-","D+","D","F"] |
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
<svg width="500" height="200" id=canvas border=1></svg> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script> | |
var y = d3.scaleLinear(); | |
var svg = d3.select("#canvas"), | |
margin = {top:10,bottom:60,left:100,right:10}, | |
barPad = 6, | |
width = +svg.attr("width")-margin.left-margin.right, | |
height = +svg.attr("height")-margin.top-margin.bottom, | |
g = svg.append("g") | |
.attr("transform","translate("+margin.left+","+margin.right+")") | |
.attr("class","canvas"); | |
var yAxis = d3.axisLeft().scale(y); | |
var xAxis = g.append("g") | |
.attr("class", "axis axis--x") | |
.attr("transform", "translate(0," + height + ")"); | |
g.append("g").classed("yAxis",true); | |
doHist(); | |
function doHist(){ | |
var url='data.json'; | |
d3.json(url, function(data){ | |
var grades = ["A","A-","B+","B","B-","C+","C","C-","D+","D","F"]; | |
var x = d3 .scaleBand() | |
.domain(grades) | |
.padding(.1) | |
.range([0,width]); | |
var histData = d3.histogram() | |
.domain([0,width]) | |
.thresholds(grades.map(x)) | |
(data.map(x)); | |
console.log(histData); | |
y.domain([0,d3.max(histData,function(d){return d.length;})]) | |
.range([height,0]); | |
var d3Data = g.selectAll('rect.bar').data(histData); | |
var bandwidth = x.bandwidth(); | |
d3Data.attr("x",function(d){return d.x0;}) | |
.transition() | |
.attr("y",function(d){return y(d.length);}) | |
.attr("height",function(d){return height-y(d.length)}) | |
d3Data.enter() | |
.append("rect") | |
.classed("bar",true) | |
.attr("x",function(d){return d.x0;}) | |
.attr("y",function(d){return y(d.length);}) | |
.attr("width",bandwidth) | |
.attr("height",function(d){return height-y(d.length)}) | |
.style("fill","red"); | |
var axis = d3.axisBottom().scale(x).tickSize(11); | |
xAxis.call(axis); | |
g.append("text") | |
.attr("transform", "translate("+ (width/2) +"," + (height+50) + ")") | |
.text("Grades"); | |
g.append("g") | |
.attr("transform", "translate( -70 ," + (height/2) + ") rotate(-90)") | |
.append("text") | |
.text("Count"); | |
g.select("g.yAxis") | |
.transition().duration(1500) | |
.call(yAxis); | |
}); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment