Skip to content

Instantly share code, notes, and snippets.

Created April 2, 2012 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/2284843 to your computer and use it in GitHub Desktop.
Save anonymous/2284843 to your computer and use it in GitHub Desktop.
{
"name": "flare",
"children": [
{
"name": "vis",
"children": [
{
"name": "events",
"children": [
{"name": "DataEvent", "size": 2200,
"children": [
{"name": "DataEvent", "size": 800, "color" : "red"},
{"name": "SelectionEvent", "size": 600, "color" : "red"},
{"name": "TooltipEvent", "size": 300, "color" : "red"},
{"name": "VisualizationEvent", "size": 250, "color" : "red"},
{"name": "TooltipEvent", "size": 250, "color" : "red"},
{"name": "DataEvent", "size": 525, "color" : "red"},
{"name": "SelectionEvent", "size": 375, "color" : "red"},
{"name": "TooltipEvent", "size": 265, "color" : "red"},
{"name": "VisualizationEvent", "size": 136, "color" : "red"},
{"name": "TooltipEvent", "size": 100, "color" : "red"}
]
},
{"name": "SelectionEvent", "size": 1400,
"children": [
{"name": "DataEvent", "size": 525, "color" : "aqua"},
{"name": "SelectionEvent", "size": 375, "color" : "aqua"},
{"name": "TooltipEvent", "size": 265, "color" : "aqua"},
{"name": "VisualizationEvent", "size": 136, "color" : "aqua"},
{"name": "TooltipEvent", "size": 100, "color" : "aqua"}
]},
{"name": "TooltipEvent", "size": 1200, "color" : "green"},
{"name": "VisualizationEvent", "size": 825, "color" : "blue"},
{"name": "TooltipEvent", "size": 400, "color" : "purple"}
]
}
]
}
]
}
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="../d3.v2.js"></script>
<style type="text/css">
</style>
</head>
<body onResize="window.location=window.location;">
<script type="text/javascript" src="treemap-svg.js"></script>
</body>
</html>
var w = window.innerWidth-25,
h = window.innerHeight-25,
color = d3.scale.category20c();
var treemap = d3.layout.treemap()
.padding(0)
.size([w, h])
.sort(function(a, b) { return a.size - b.size; })
.value(function(d) { return d.size; });
// Tool tip implementatoin
var tooltip = d3.select("body")
.append("div")
//.style("position", "absolute")
.style("position", "absolute")
.style("height", "60px")
.style("width", "140px")
.style("background-color", "white")
.style("z-index", "100")
.style("visibility", "hidden")
.text("a simple tooltip");
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h)
.append("g")
.attr("transform", "translate(-.5,-.5)");
d3.json("../data/flare.json", function(json) {
var cell = svg.data([json]).selectAll("g")
.data(treemap.nodes(json).filter(function(d) { return !d.children; }))
//.sort(function(a, b) { return a.size - b.size; })
.enter().append("g")
.attr("class", "cell")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
//if node returns null for children, display, else display:none;
cell.append("rect")
.attr("width", function(d) { return d.dx; })
.attr("height", function(d ) { return d.dy; })
.style("stroke-width", 2)
.style("stroke", "blue")
.style("fill", function(d) { return d.color})
//.style("fill-opacity", .5)
.on("mouseover", function(){return tooltip.style("visibility", "visible");})
.on("mouseout", function(){return tooltip.style("visibility", "hidden");});
/*.on("mouseover", function() {
d3.select(this).style("fill-opacity", .5).style("stroke-width", 4);
//d3.select("rect").attr("visibility", "visible");
})
.on("mouseout", function() {
d3.select(this).style("fill",function(d) { return d.color}).style("fill-opacity", 1).style("stroke-width", 2);
//d3.select("text").attr("visibility", "hidden");
});
*/
//.style("fill", function(d) { return d.children ? color(d.data.name) : null; })
cell.append("text")
.attr("x", function(d) { return d.dx /2; })
.attr("y", function(d ) { return d.dy/2; })
//.attr("dy", ".35em")
.attr("font-size", 10)
.attr("font-family", "Georgia")
.attr("text-anchor", "middle")
//.attr("visibility", "hidden")
.attr("fill", "white")
// .style("stroke", "white") .style("stroke-width", .5)
//.text("hello");
.text(function(d) { return d.name; });
/*
.each(function(d) {
var box,
area = d.dx * d.dy,
w = d.dx,
t = d3.select(this);
do {
t.style("font-size", w-- + "px");
box = this.getBBox();
} while (norm2(box.x, box.y) > area
|| norm2(box.x + box.width, box.y) > area
|| norm2(box.x + box.width, box.y + box.height) > area
|| norm2(box.x, box.y + box.height) > area);
});
*/
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment