Skip to content

Instantly share code, notes, and snippets.

@jwang1616
Created February 10, 2017 04:45
Show Gist options
  • Save jwang1616/bc07f9515faced821305d624293608c9 to your computer and use it in GitHub Desktop.
Save jwang1616/bc07f9515faced821305d624293608c9 to your computer and use it in GitHub Desktop.
HW1_barChart
license: gpl-3.0

This grouped bar chart is constructed from a CSV file storing the populations of different states by age group. The chart employs conventional margins and a number of D3 features:

forked from mbostock's block: Grouped Bar Chart

forked from anonymous's block: Grouped Bar Chart

forked from anonymous's block: Grouped Bar Chart

forked from jwang1616's block: HW1_group

forked from zhaopan0558's block: HW1_group

forked from anonymous's block: HW1_group

forked from anonymous's block: HW1_group

forked from anonymous's block: HW1_group

State Under 5 Years 5 to 13 Years 14 to 17 Years 18 to 24 Years 25 to 44 Years 45 to 64 Years 65 Years and Over
CA 2704659 4499890 2159981 3853788 10604510 8819342 4114496
TX 2027307 3277946 1420518 2454721 7017731 5656528 2472223
NY 1208495 2141490 1058031 1999120 5355235 5120254 2607672
FL 1140516 1938695 925060 1607297 4782119 4746856 3187797
IL 894368 1558919 725973 1311479 3596343 3239173 1575308
PA 737462 1345341 679201 1203944 3157759 3414001 1910571
<!DOCTYPE html>
<style>
.axis .domain {
display: none;
}
</style>
<svg width="960" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 30, left: 40},
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.top + ")");
var x0 = d3.scaleBand()
.rangeRound([0, width])
.paddingInner(0.1);
var x1 = d3.scaleBand()
.padding(0.05);
var y = d3.scaleLinear()
.rangeRound([height, 0]);
var z = d3.scaleOrdinal()
.range(["#93c2a1", "#9877c4", "#906e70", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);
d3.csv("Table_2_new.csv", function(d, i, columns) {
for (var i = 1, n = columns.length; i < n; ++i) d[columns[i]] = +d[columns[i]];
return d;
}, function(error, data) {
if (error) throw error;
var keys = data.columns.slice(2,5);
x0.domain(data.map(function(d) { return d.Highest_level_completed
; }));
x1.domain(keys).rangeRound([0, x0.bandwidth()]);
y.domain([0, d3.max(data, function(d) { return d3.max(keys, function(key) { return d[key]; }); })]).nice();
g.append("g")
.selectAll("g")
.data(data)
.enter().append("g")
.attr("transform", function(d) { return "translate(" + x0(d.Highest_level_completed) + ",0)"; })
.selectAll("rect")
.data(function(d) { return keys.map(function(key) { return {key: key, value: d[key]}; }); })
.enter().append("rect")
.attr("x", function(d) { return x1(d.key); })
.attr("y", function(d) { return y(d.value); })
.attr("width", x1.bandwidth())
.attr("height", function(d) { return height - y(d.value); })
.attr("fill", function(d) { return z(d.key); });
g.append("g")
.style("font-size","10px")
.attr("class", "axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x0));
g.append("g")
.attr("class", "axis")
.call(d3.axisLeft(y).ticks(null, "s"))
.style("font-size","20px")
.append("text")
.attr("x", 20)
.attr("y", y(y.ticks().pop()) + 0.5)
.attr("dy", "0.32em")
.attr("fill", "#000")
.attr("font-weight", "bold")
.attr("text-anchor", "start")
var legend = g.append("g")
.style("font-size","20px")
.attr("font-family", "sans-serif")
.attr("font-size", 15)
.attr("text-anchor", "end")
.selectAll("g")
.data(keys.slice().reverse())
.enter().append("g")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", 160)
.attr("width", 19)
.attr("height", 19)
.attr("fill", z);
legend.append("text")
.attr("x", 155)
.attr("y", 9.5)
.attr("dy", "0.32em")
.text(function(d) { return d; });
});
// code for saving the image to a file
var doctype = '<?xml version="1.0" standalone="no"?>'
+ '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">';
// serialize our SVG XML to a string.
var source = (new XMLSerializer()).serializeToString(d3.select('svg').node());
// create a file blob of our SVG.
var blob = new Blob([ doctype + source], { type: 'image/svg+xml;charset=utf-8' });
var url = window.URL.createObjectURL(blob);
// Put the svg into an image tag so that the Canvas element can read it in.
var img = d3.select('body').append('img')
.attr('width', 100)
.attr('height', 100)
.node();
img.onload = function(){
// Now that the image has loaded, put the image into a canvas element.
var canvas = d3.select('body').append('canvas').node();
canvas.width = 100;
canvas.height = 100;
var ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
var canvasUrl = canvas.toDataURL("image/png");
var img2 = d3.select('body').append('img')
.attr('width', 100)
.attr('height', 100)
.node();
// this is now the base64 encoded version of our PNG! you could optionally
// redirect the user to download the PNG by sending them to the url with
// `window.location.href= canvasUrl`.
img2.src = canvasUrl;
}
// start loading the image.
img.src = url;
</script>
Highest_level_completed Number_of_people Total_income_A Earnings_FT_A Earnings_All_A Months_worked_A Total_income_M Earnings_FT_M Earnings_All_M
Elementary 12002.0 1166.0 2136.0 1813.0 1.7 914.0 1732.0 1516.0
Some high school 18260.0 1205.0 2434.0 1840.0 1.8 880.0 2000.0 1472.0
High school graduate 57880.0 1960.0 3179.0 2631.0 2.3 1458.0 2550.0 2078.0
Some college 35337.0 2204.0 3598.0 2789.0 2.6 1556.0 2917.0 2130.0
Vocational certificate* 24709.0 2400.0 3538.0 2945.0 2.6 1898.0 2950.0 2400.0
Associate's degree 18429.0 3001.0 4166.0 3562.0 3.0 2408.0 3456.0 2923.0
Bachelor's degree 38782.0 4259.0 5445.0 5034.0 3.0 3188.0 4355.0 3874.0
Master's degree 15132.0 5564.0 6731.0 6243.0 3.1 4508.0 5417.0 5000.0
Professional degree 3232.0 9501.0 11927.0 11071.0 3.3 6125.0 7417.0 7128.0
Doctorate degree 2486.0 7466.0 8434.0 8116.0 3.2 5756.0 6833.0 6260.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment