function MyTreemap(eleId, _w, _h, _o) { var tm = {}; var width = _w !== undefined ? _w : 400, height = _h !== undefined ? _h : 400, offset = _o !== undefined ? _o : 3; var format = d3.format(",d"); var color = d3.scaleMagma() .domain([-4, 4]); var stratify = d3.stratify() .parentId(function(d) { return d.id.substring(0, d.id.lastIndexOf(".")); }); var treemap = d3.treemap() .size([width, height]) .paddingInner(1) .paddingOuter(offset) .paddingTop(function(d) { return d.depth < 3 ? 19 : offset; }) .round(true); var base = d3.select(eleId) .style("width", width+"px") .style("height", height+"px") .style("position", "relative") .style("top", 0) .style("left", 0); tm.public_var = "John"; tm.update = function (data) { var root = stratify(data) .sum(function(d) { return d.value; }) .sort(function(a, b) { return b.height - a.height || b.value - a.value; }); cascade(treemap(root)); base .selectAll(".node") .data(root.descendants()) .enter().append("div") .attr("class", function(d) { return "node" + (d.children ? " node--internal" : " node--left"); }) .attr("title", function(d) { return d.id + "\n" + format(d.value); }) .style("left", function(d) { return d.x0 + "px"; }) .style("top", function(d) { return d.y0 + "px"; }) .style("width", function(d) { return d.x1 - d.x0 + "px"; }) .style("height", function(d) { return d.y1 - d.y0 + "px"; }) .style("background", function(d) { return color(d.depth); }) .each(function(d) { d.node = this; }) .on("mouseover", hovered(true)) .on("mouseout", hovered(false)) .text(function(d) { return d.id.substring(d.id.lastIndexOf(".") + 1).split(/(?=[A-Z][^A-Z])/g).join("\u200b"); }); }; function cascade(root) { return root.eachAfter(function(d) { if (d.children) { d.heightRight = 1 + d3.max(d.children, function(c) { return c.x1 === d.x1 - offset ? c.heightRight : NaN; }); d.heightBottom = 1 + d3.max(d.children, function(c) { return c.y1 === d.y1 - offset ? c.heightBottom : NaN; }); } else { d.heightRight = d.heightBottom = 0; } }).eachBefore(function(d) { d.x1 -= 2 * offset * d.heightRight; d.y1 -= 2 * offset * d.heightBottom; }); } function hovered(hover) { return function(d) { d3.selectAll(d.ancestors().map(function(d) { return d.node; })) .classed("node--hover", hover); }; } return tm; }