-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5cadd67
commit 52ebdc5
Showing
6 changed files
with
165 additions
and
1 deletion.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
.node { | ||
border: solid 1px white; | ||
font: 10px sans-serif; | ||
line-height: 12px; | ||
overflow: hidden; | ||
position: absolute; | ||
text-indent: 2px; | ||
padding: 0px; /* form div giving top 1px */ | ||
box-sizing: content-box; /* otherwise inheriting border-box */ | ||
} | ||
|
||
.treemap-container { | ||
position: relative; | ||
margin: auto; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// JS | ||
var d3 = window.d3 || require('d3'); | ||
var px = window.px || require('../javascripts/modules/caravel.js'); | ||
|
||
// CSS | ||
require('./treemap.css'); | ||
|
||
/* Modified from https://bl.ocks.org/mbostock/4063582 */ | ||
function treemap(slice) { | ||
|
||
var div = d3.select(slice.selector); | ||
|
||
var _draw = function (data, eltWidth, eltHeight, includeTitle) { | ||
|
||
var margin = { top: 0, right: 0, bottom: 0, left: 0 }, | ||
headerHeight = includeTitle ? 30 : 0, | ||
width = eltWidth - margin.left - margin.right, | ||
height = eltHeight - headerHeight - margin.top - margin.bottom; | ||
|
||
var treemap = d3.layout.treemap() | ||
.size([width, height]) | ||
.value(function (d) { return d.value; }); | ||
|
||
var root = div.append("div") | ||
.classed("treemap-container", true); | ||
|
||
var header = root.append("div") | ||
.style("width", (width + margin.left + margin.right) + "px") | ||
.style("height", headerHeight + "px"); | ||
|
||
var container = root.append("div") | ||
.style("position", "relative") | ||
.style("width", (width + margin.left + margin.right) + "px") | ||
.style("height", (height + margin.top + margin.bottom) + "px") | ||
.style("left", margin.left + "px") | ||
.style("top", margin.top + "px"); | ||
|
||
var position = function (selection) { | ||
selection.style("left", function (d) { return d.x + "px"; }) | ||
.style("top", function (d) { return d.y + "px"; }) | ||
.style("width", function (d) { return Math.max(0, d.dx - 1) + "px"; }) | ||
.style("height", function (d) { return Math.max(0, d.dy - 1) + "px"; }); | ||
}; | ||
|
||
container.datum(data).selectAll(".node") | ||
.data(treemap.nodes) | ||
.enter().append("div") | ||
.attr("class", "node") | ||
.call(position) | ||
.style("background", function (d) { | ||
return d.children ? px.color.category21(d.name) : null; | ||
}) | ||
.style("color", function (d) { | ||
// detect if our background is dark and we need a | ||
// light text color or vice-versa | ||
var bg = d.parent ? px.color.category21(d.parent.name) : null; | ||
if (bg) { | ||
return d3.hsl(bg).l < 0.35 ? '#d3d3d3' : '#111111'; | ||
} | ||
}) | ||
.text(function (d) { return d.children ? null : d.name; }); | ||
|
||
if (includeTitle) { | ||
// title to help with multiple metrics (if any) | ||
header.append("span") | ||
.style("font-size", "18px") | ||
.style("font-weight", "bold") | ||
.text(data.name); | ||
} | ||
|
||
}; | ||
|
||
var render = function () { | ||
|
||
d3.json(slice.jsonEndpoint(), function (error, json) { | ||
|
||
if (error !== null) { | ||
slice.error(error.responseText); | ||
return ''; | ||
} | ||
|
||
div.selectAll("*").remove(); | ||
var width = slice.width(); | ||
// facet muliple metrics (no sense in combining) | ||
var height = slice.height() / json.data.length; | ||
var includeTitles = json.data.length > 1; | ||
for (var i = 0, l = json.data.length; i < l; i ++) { | ||
_draw(json.data[i], width, height, includeTitles); | ||
} | ||
|
||
slice.done(json); | ||
|
||
}); | ||
|
||
}; | ||
|
||
return { | ||
render: render, | ||
resize: render | ||
}; | ||
} | ||
|
||
module.exports = treemap; | ||
|
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
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