Skip to content

Instantly share code, notes, and snippets.

@andrewwitherspoon
Last active June 19, 2020 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewwitherspoon/1a4a22022eca1eb8b5f7b4fc0da59974 to your computer and use it in GitHub Desktop.
Save andrewwitherspoon/1a4a22022eca1eb8b5f7b4fc0da59974 to your computer and use it in GitHub Desktop.
Fortune 100 BLM
border: no
height: 600
license: gpl-3.0
{
"name": "flare",
"children": [
{"name":"Bank of America","size":1000000000},
{"name":"PepsiCo","size":400000000},
{"name":"Walmart","size":100000000},
{"name":"Apple","size":100000000},
{"name":"Comcast","size":100000000},
{"name":"Anthem","size":50000000},
{"name":"Nike","size":40000000},
{"name":"Alphabet","size":38000000},
{"name":"Boeing","size":25000000},
{"name":"Humana","size":11500000},
{"name":"Amazon.com","size":10000000},
{"name":"UnitedHealth Group","size":10000000},
{"name":"General Motors","size":10000000},
{"name":"Verizon Communications","size":10000000},
{"name":"Johnson & Johnson","size":10000000},
{"name":"Target","size":10000000},
{"name":"Facebook","size":10000000},
{"name":"Goldman Sachs Group","size":10000000},
{"name":"Charter Communications","size":10000000},
{"name":"TJX","size":10000000},
{"name":"Capital One Financial","size":10000000},
{"name":"Kroger","size":5000000},
{"name":"Walt Disney","size":5000000},
{"name":"Procter & Gamble","size":5000000},
{"name":"Albertsons","size":5000000},
{"name":"Morgan Stanley","size":5000000},
{"name":"Cisco Systems","size":5000000},
{"name":"Tyson Foods","size":5000000},
{"name":"MetLife","size":5000000},
{"name":"Dow","size":5000000},
{"name":"AbbVie","size":5000000},
{"name":"UPS","size":4200000},
{"name":"JPMorgan Chase","size":3000000},
{"name":"Coca-Cola","size":2500000},
{"name":"Northrop Grumman","size":2000000},
{"name":"Microsoft","size":1500000},
{"name":"Home Depot","size":1000000},
{"name":"State Farm Insurance","size":1000000},
{"name":"Intel","size":1000000},
{"name":"American Express","size":1000000},
{"name":"Nationwide","size":1000000},
{"name":"Liberty Mutual Insurance Group","size":1000000},
{"name":"Deere","size":1000000},
{"name":"Progressive","size":1000000},
{"name":"Publix Super Markets","size":1000000},
{"name":"Caterpillar","size":1000000},
{"name":"AT&T","size":500000},
{"name":"AIG","size":500000},
{"name":"HP","size":500000},
{"name":"Archer Daniels Midland","size":200000}
]
}
<!DOCTYPE html>
<style>
form {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
svg {
font: 10px sans-serif;
}
</style>
<svg width="290" height="412"></svg>
<form>
<label><input type="radio" name="mode" value="sumBySize" checked> Size</label>
<!-- <label><input type="radio" name="mode" value="sumByCount"> Count</label> -->
</form>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var fader = function(color) { return d3.interpolateRgb(color, "#fff")(0.2); },
color = d3.scaleOrdinal(d3.schemeCategory20.map(fader)),
format = d3.format(",d");
var treemap = d3.treemap()
.tile(d3.treemapResquarify)
.size([width, height])
.round(true)
.paddingInner(1);
d3.json("flare.json", function(error, data) {
if (error) throw error;
var root = d3.hierarchy(data)
.eachBefore(function(d) { d.data.id = (d.parent ? d.parent.data.id + "." : "") + d.data.name; })
.sum(sumBySize)
.sort(function(a, b) { return b.height - a.height || b.value - a.value; });
treemap(root);
var cell = svg.selectAll("g")
.data(root.leaves())
.enter().append("g")
.attr("transform", function(d) { return "translate(" + d.x0 + "," + d.y0 + ")"; });
cell.append("rect")
.attr("id", function(d) { return d.data.id; })
.attr("width", function(d) { return d.x1 - d.x0; })
.attr("height", function(d) { return d.y1 - d.y0; })
.attr("fill", function(d) { return color(d.parent.data.id); });
cell.append("clipPath")
.attr("id", function(d) { return "clip-" + d.data.id; })
.append("use")
.attr("xlink:href", function(d) { return "#" + d.data.id; });
cell.append("text")
.attr("clip-path", function(d) { return "url(#clip-" + d.data.id + ")"; })
.selectAll("tspan")
.data(function(d) { return d.data.name.split(/(?=[A-Z][^A-Z])/g); })
.enter().append("tspan")
.attr("x", 4)
.attr("y", function(d, i) { return 13 + i * 10; })
.text(function(d) { return d; });
cell.append("title")
.text(function(d) { return d.data.id + "\n" + format(d.value); });
d3.selectAll("input")
.data([sumBySize, sumByCount], function(d) { return d ? d.name : this.value; })
.on("change", changed);
var timeout = d3.timeout(function() {
d3.select("input[value=\"sumByCount\"]")
.property("checked", true)
.dispatch("change");
}, 2000);
function changed(sum) {
timeout.stop();
treemap(root.sum(sum));
cell.transition()
.duration(750)
.attr("transform", function(d) { return "translate(" + d.x0 + "," + d.y0 + ")"; })
.select("rect")
.attr("width", function(d) { return d.x1 - d.x0; })
.attr("height", function(d) { return d.y1 - d.y0; });
}
});
function sumByCount(d) {
return d.children ? 0 : 4;
}
function sumBySize(d) {
return d.size;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment