Skip to content

Instantly share code, notes, and snippets.

@tomruarol
Last active December 30, 2018 20:25
Show Gist options
  • Save tomruarol/25bd71549f98668c30f6fd4cd40d172d to your computer and use it in GitHub Desktop.
Save tomruarol/25bd71549f98668c30f6fd4cd40d172d to your computer and use it in GitHub Desktop.
basic waffle chart
license: mit
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 1 column, instead of 3 in line 1.
YearStart;YearEnd;LocationAbbr;LocationDesc;Datasource;Class;Topic;Question;Data_Value_Unit;Data_Value_Type;Data_Value;Data_Value_Alt;Data_Value_Footnote_Symbol;Data_Value_Footnote;Low_Confidence_Limit;High_Confidence_Limit ;Sample_Size;Total;Age(years);Education;Gender;Income;Race/Ethnicity;GeoLocation;ClassID;TopicID;QuestionID;DataValueTypeID;LocationID;StratificationCategory1;Stratification1;StratificationCategoryId1;StratificationID1
2011;2011;AL;Alabama;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;29.6;29.6;;;29.3;29.8;438,479;Total;18 - 24;;Female;;Non-Hispanic White;(32.84057112200048, -86.63186076199969);OWS;OWS1;Q036;VALUE;59;Total;Total;OVR;OVERALL
2011;2011;AK;Alaska;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;29.6;29.6;;;29.2;30;198,440;;45 - 54;;Male;;Hispanic;(64.84507995700051, -147.72205903599973);OWS;OWS1;Q036;VALUE;59;Gender;Male;GEN;MALE
2011;2011;CA;California;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;29.5;29.5;;;29.1;29.9;240,000;;18 - 24;;Female;;Non-Hispanic White;(37.63864012300047, -120.99999953799971);OWS;OWS1;Q036;VALUE;59;Gender;Female;GEN;FEMALE
2012;2012;AL;Alabama;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;35.5;35.5;;;34.5;36.5;32,325;;25 - 34;Less than high school;Female;;Non-Hispanic White;(32.84057112200048, -86.63186076199969);OWS;OWS1;Q036;VALUE;59;Education;Less than high school;EDU;EDUHS
2012;2012;FL;Florida;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;32.3;32.3;;;31.8;32.8;123,241;;45 - 54;High school graduate;Male;;Non-Hispanic Black;(28.932040377000476, -81.92896053899966);OWS;OWS1;Q036;VALUE;59;Education;High school graduate;EDU;EDUHSGRAD
2012;2012;AK;Alaska;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;31;31;;;30.5;31.5;120,735;;18 - 24;Some college or technical school;Male;;Hispanic;(64.84507995700051, -147.72205903599973);OWS;OWS1;Q036;VALUE;59;Education;Some college or technical school;EDU;EDUCOTEC
2012;2012;CA;California;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;22.2;22.2;;;21.9;22.6;161,309;;25 - 34;College graduate;Male;;Non-Hispanic White;(37.63864012300047, -120.99999953799971);OWS;OWS1;Q036;VALUE;59;Education;College graduate;EDU;EDUCOGRAD
2013;2013;CA;California;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;17.3;17.3;;;16.5;18.1;23,734;;18 - 24;;Female;;Non-Hispanic White;(37.63864012300047, -120.99999953799971);OWS;OWS1;Q036;VALUE;59;Age (years);18 - 24;AGEYR;AGEYR1824
2013;2013;AL;Alabama;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;27.2;27.2;;;26.5;27.9;42,706;;25 - 34;;Female;;Non-Hispanic Black;(32.84057112200048, -86.63186076199969);OWS;OWS1;Q036;VALUE;59;Age (years);25 - 34;AGEYR;AGEYR2534
2013;2013;AK;Alaska;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;33.1;33.1;;;32.3;33.8;48,951;;35 - 44;;Female;;Hispanic;(64.84507995700051, -147.72205903599973);OWS;OWS1;Q036;VALUE;59;Age (years);35 - 44;AGEYR;AGEYR3544
2014;2014;FL;Florida;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;35.1;35.1;;;34.4;35.8;68,854;;45 - 54;;Male;;Non-Hispanic White;(28.932040377000476, -81.92896053899966);OWS;OWS1;Q036;VALUE;59;Age (years);45 - 54;AGEYR;AGEYR4554
2014;2014;FL;Florida;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;34.2;34.2;;;33.6;34.8;96,566;;55 - 64;;Female;;Non-Hispanic Black;(28.932040377000476, -81.92896053899966);OWS;OWS1;Q036;VALUE;59;Age (years);55 - 64;AGEYR;AGEYR5564
2014;2014;AL;Alabama;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;28;28;;;27.5;28.5;157,668;;45 - 54;;Male;;Non-Hispanic White;(32.84057112200048, -86.63186076199969);OWS;OWS1;Q036;VALUE;59;Age (years);65 or older;AGEYR;AGEYR65PLUS
2014;2014;AK;Alaska;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;35.4;35.4;;;34.4;36.4;36,639;;25 - 34;;Male;Less than $15,000;Non-Hispanic White;(64.84507995700051, -147.72205903599973);OWS;OWS1;Q036;VALUE;59;Income;Less than $15,000;INC;INCLESS15
2014;2014;CA;California;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;33.4;33.4;;;32.6;34.1;62,605;;35 - 44;;Female;$15,000 - $24,999;Non-Hispanic Black;(37.63864012300047, -120.99999953799971);OWS;OWS1;Q036;VALUE;59;Income;$15,000 - $24,999;INC;INC1525
2015;2015;FL;Florida;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;31.9;31.9;;;30.9;32.8;40,914;;18 - 24;;Male;$25,000 - $34,999;Asian;(28.932040377000476, -81.92896053899966);OWS;OWS1;Q036;VALUE;59;Income;$25,000 - $34,999;INC;INC2535
2015;2015;AK;Alaska;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;32;32;;;31.2;32.8;54,639;;35 - 44;;Female;$35,000 - $49,999;Non-Hispanic White;(64.84507995700051, -147.72205903599973);OWS;OWS1;Q036;VALUE;59;Income;$35,000 - $49,999;INC;INC3550
2015;2015;CA;California;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;31.1;31.1;;;30.3;31.8;61,211;;25 - 34;;Female;$50,000 - $74,999;Non-Hispanic Black;(37.63864012300047, -120.99999953799971);OWS;OWS1;Q036;VALUE;59;Income;$50,000 - $74,999;INC;INC5075
2015;2015;CA;California;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;25.4;25.4;;;24.9;25.9;120,901;;45 - 54;;Male;$75,000 or greater;Non-Hispanic White;(37.63864012300047, -120.99999953799971);OWS;OWS1;Q036;VALUE;59;Income;$75,000 or greater;INC;INC75PLUS
2015;2015;AL;Alabama;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;25.5;25.5;;;24.8;26.2;61,570;;18 - 24;;Female;Data not reported;Non-Hispanic Black;(32.84057112200048, -86.63186076199969);OWS;OWS1;Q036;VALUE;59;Income;Data not reported;INC;INCNR
2015;2015;AK;Alaska;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;28.6;28.6;;;28.3;28.9;341,192;;45 - 54;;Male;;Non-Hispanic White;(64.84507995700051, -147.72205903599973);OWS;OWS1;Q036;VALUE;59;Race/Ethnicity;Non-Hispanic White;RACE;RACEWHT
2016;2016;FL;Florida;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;38.3;38.3;;;37.4;39.3;35,091;;25 - 34;;Male;;Non-Hispanic Black;(28.932040377000476, -81.92896053899966);OWS;OWS1;Q036;VALUE;59;Race/Ethnicity;Non-Hispanic Black;RACE;RACEBLK
2016;2016;AK;Alaska;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;33.1;33.1;;;32.1;34.1;28,666;;18 - 24;;Male;;Hispanic;(64.84507995700051, -147.72205903599973);OWS;OWS1;Q036;VALUE;59;Race/Ethnicity;Hispanic;RACE;RACEHIS
2016;2016;AK;Alaska;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;9.8;9.8;;;8.6;11;8,971;;35 - 44;;Female;;Asian;(64.84507995700051, -147.72205903599973);OWS;OWS1;Q036;VALUE;59;Race/Ethnicity;Asian;RACE;RACEASN
2016;2016;CA;California;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;30.6;30.6;;;25.2;36.7;840;;18 - 24;;Male;;Non-Hispanic Black;(37.63864012300047, -120.99999953799971);OWS;OWS1;Q036;VALUE;59;Race/Ethnicity;Hawaiian/Pacific Islander;RACE;RACEHPI
2016;2016;FL;Florida;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;38.1;38.1;;;35.7;40.4;6,630;;25 - 34;;Male;;American Indian/Alaska Native;(28.932040377000476, -81.92896053899966);OWS;OWS1;Q036;VALUE;59;Race/Ethnicity;American Indian/Alaska Native;RACE;RACENAA
2016;2016;AK;Alaska;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;31;31;;;29;33.1;8,622;;25 - 34;;Male;;Non-Hispanic Black;(64.84507995700051, -147.72205903599973);OWS;OWS1;Q036;VALUE;59;Race/Ethnicity;2 or more races;RACE;RACE2PLUS
2016;2016;CA;California;Behavioral Risk Factor Surveillance System;Obesity / Weight Status;Obesity / Weight Status;Percent of adults aged 18 years and older who have obesity;;Value;26.1;26.1;;;22.5;29.9;1,891;;18 - 24;;Female;;Asian;(37.63864012300047, -120.99999953799971);OWS;OWS1;Q036;VALUE;59;Race/Ethnicity;Other;RACE;RACEOTH
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta charset="utf-8">
<style>
body {
font: 18px Arial;
}
p {
margin: 0;
}
#waffle {
margin-bottom: 30px;
}
#legend {
font: 13px Arial;
}
</style>
</head>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<div id="title">
<p>A measure, by age population</p>
</div>
<div id="waffle">
</div>
<div id="legend">
</div>
</body>
<script>
var total = 0;
var width,
height,
widthSquares = 20,
heightSquares = 5,
squareSize = 25,
squareValue = 0,
gap = 1,
theData = [];
var color = d3.scale.category20();
d3.csv("data.csv", function(error, data)
{
//total
total = d3.sum(data, function(d) { return d.population; });
//value of a square
squareValue = total / (widthSquares*heightSquares);
//remap data
data.forEach(function(d, i)
{
d.population = +d.population;
d.units = Math.floor(d.population/squareValue);
theData = theData.concat(
Array(d.units+1).join(1).split('').map(function()
{
return { squareValue:squareValue,
units: d.units,
population: d.population,
groupIndex: i};
})
);
});
width = (squareSize*widthSquares) + widthSquares*gap + 25;
height = (squareSize*heightSquares) + heightSquares*gap + 25;
var waffle = d3.select("#waffle")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.selectAll("div")
.data(theData)
.enter()
.append("rect")
.attr("width", squareSize)
.attr("height", squareSize)
.attr("fill", function(d)
{
return color(d.groupIndex);
})
.attr("x", function(d, i)
{
//group n squares for column
col = Math.floor(i/heightSquares);
return (col*squareSize) + (col*gap);
})
.attr("y", function(d, i)
{
row = i%heightSquares;
return (heightSquares*squareSize) - ((row*squareSize) + (row*gap))
})
.append("title")
.text(function (d,i)
{
return "Age range: " + data[d.groupIndex].age + " | " + d.population + " , " + d.units + "%"
});
//add legend with categorical data
var legend = d3.select("#legend")
.append("svg")
.attr('width', 300)
.attr('height', 200)
.append('g')
.selectAll("div")
.data(data)
.enter()
.append("g")
.attr('transform', function(d,i){ return "translate(0," + i*20 + ")";});
legend.append("rect")
.attr("width", 18)
.attr("height", 18)
.style("fill", function(d, i) { return color(i)});
legend.append("text")
.attr("x", 25)
.attr("y", 13)
.text( function(d) { return d.age});
//add value of a unit square
var legend2 = d3.select("#legend")
.select('svg')
.append('g')
.attr('transform', "translate(100,0)");
legend2.append("text")
.attr('y', '14')
.attr('font-size', '18px')
.text("Total: " + total)
.attr("fill", "#444444");
});
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment