Create a gist now

Instantly share code, notes, and snippets.

Accession by State using Sortable barchart
statelong state rate
Alaska AK 2
Alabama AL 23
Arkansas AR 6
Arizona AZ 26
California CA 55
Colorado CO 35
Connecticut CT 9
D.C. DC 1
Delaware DE 1
Florida FL 69
Georgia GA 14
Hawaii HI 7
Iowa IA 8
Idaho ID 0
Illinois IL 19
Inidana IN 21
Kansas KS 11
Kentucky KY 8
Louisiana LA 9
Massachusetts MA 16
Maryland MD 3
Michigan MI 21
Minnesota MN 9
Missouri MO 16
Mississippi MS 10
Montana MT 4
North Carolina NC 27
North Dakota ND 3
Nebraska NE 6
New Hampshire NH 1
New Jersey NJ 13
New Mexico NM 4
Nevada NV 4
New York NY 15
Ohio OH 31
Oklahoma OK 16
Oregon OR 7
Pennsylvania PA 22
Rhode Island RI 8
South Carolina SC 28
South Dakota SD 3
Tennessee TN 11
Texas TX 69
Utah UT 16
Virginia VA 32
Vermont VT 2
Washington WA 8
Wisconsin WI 5
West Virginia WV 7
Wyoming WY 6
<html>
<!--Sortable: http://bl.ocks.org/mbostock/3885705-->
<style>
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
position: relative;
width: 960px;
}
.axis text {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: orange;
}
.bar:hover{
fill: orangered;
}
.x.axis path {
display: none;
}
label {
position: absolute;
top: 10px;
right: 10px;
}
.d3-tip {
line-height: 1;
font-weight: bold;
padding: 12px;
background: rgba(0, 0, 0, 0.8);
color: #fff;
border-radius: 2px;
}
/* Creates a small triangle extender for the tooltip */
.d3-tip:after {
box-sizing: border-box;
display: inline;
font-size: 10px;
width: 100%;
line-height: 1;
color: rgba(0, 0, 0, 0.8);
content: "\25BC";
position: absolute;
text-align: center;
}
/* Style northward tooltips differently */
.d3-tip.n:after {
margin: -1px 0 0 0;
top: 100%;
left: 0;
}
</style>
<label><input type="checkbox"> Sort values</label>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
<script>
var margin = {top: 50, right: 20, bottom: 30, left: 40},
width = 1200 - margin.left - margin.right,
height = 700 - margin.top - margin.bottom;
var formatPercent = d3.format("d");
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1, 1);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-17, -8])
.html(function(d) {
return "<strong> "+ d.statelong + ": </strong> <span style='color:red'>" + d.rate + "</span>";
});
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.call(tip);
d3.tsv("afsc_state.tsv", type, function(error, data) {
x.domain(data.map(function(d) { return d.state; }));
y.domain([0, d3.max(data, function(d) { return d.rate; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Number of cadets");
svg.append("text")
.attr("x", (width / 2))
.attr("y", 0 - (margin.top / 2))
.attr("text-anchor", "middle")
.style("font-size", "16px")
.text("CY2016 NRL ROTC Cadets by state of universities");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.state); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.rate); })
.attr("height", function(d) { return height - y(d.rate); })
.on('mouseover', tip.show)
.on('mouseout',tip.hide);
d3.select("input").on("change", change);
var sortTimeout = setTimeout(function() {
d3.select("input").property("checked", true).each(change);
}, 2000);
function change() {
clearTimeout(sortTimeout);
// Copy-on-write since tweens are evaluated after a delay.
var x0 = x.domain(data.sort(this.checked
? function(a, b) { return b.rate - a.rate; }
: function(a, b) { return d3.ascending(a.state, b.state); })
.map(function(d) { return d.state; }))
.copy();
svg.selectAll(".bar")
.sort(function(a, b) { return x0(a.state) - x0(b.state); });
var transition = svg.transition().duration(500),
delay = function(d, i) { return i * 25; };
transition.selectAll(".bar")
.delay(delay)
.attr("x", function(d) { return x0(d.state); });
transition.select(".x.axis")
.call(xAxis)
.selectAll("g")
.delay(delay);
}
});
function type(d) {
d.rate = +d.rate;
return d;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment