if(parent.document.getElementsByTagName("iframe")[0]) { parent.document.getElementsByTagName("iframe")[0].setAttribute('style', 'height: 800px !important'); } var margin = {top: 20, right: 0, bottom: 25, left: 0}, width = 1420, // 1420 height = 750 - margin.bottom, //775 formatNumber = d3.format(".2s"), transitioning, headerHeight = 20, headerColor = "#555555"; var x = d3.scale.linear() .domain([0, width]) .range([0, width]); var y = d3.scale.linear() .domain([0, height]) .range([0, height]); // Create a Tree Layout var treemap = d3.layout.treemap() //.ratio(height / width * 0.5 * (1 + Math.sqrt(1))) .round(false) .children(function(d, depth) { return depth ? null : d.children; }) //.children(function(d) { return d.children; }) //depth ? null : d.children; }) .sort(function(a, b) { return a.value - b.value; }); // Create the image container / canvas var svg = d3.select("#chart") .append("svg") .attr("width", width ) //+ margin.left + margin.right) .attr("height", height + margin.bottom + margin.top) .style("margin-left", -margin.left + "px") //.style("margin.right", -margin.right + "px") .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")") .style("shape-rendering", "crispEdges") var data_colorScale; function colorcode(colorScale) { data_colorScale = colorScale; } // variable to display tooltip var div = d3.select("body").append("div") .attr("class", "tooltip") .style("opacity", 0); // Append header container in the svg container var grandparent = svg.append("g") .attr("class", "grandparent"); // Create header area grandparent.append("rect") .attr("y", -margin.top) .attr("width", width) .attr("height", margin.top); // Add Header Text grandparent.append("text") .attr("x", 6) .attr("y", 6 - margin.top) .attr("dy", ".75em"); // Initialize the root node dimensions function initialize(root) { root.x = root.y = 0; root.dx = width; root.dy = height; root.depth = 0; } //Aggregate the values for internal nodes. This is normally done by the //treemap layout, but not here because of the custom implementation. function accumulate(d) { return d.children ? d.value = d.children.reduce(function(p, v) { return p + accumulate(v); }, 0) : d.value; } // Creates the layout // Defines how the nodes needs to be displayed in the svg function layout(d) { if (d.children) { treemap.nodes({children: d.children}); d.children.forEach(function(c) { c.x = d.x + c.x * d.dx; c.y = d.y + c.y * d.dy; c.dx *= d.dx; c.dy *= d.dy; c.parent = d; layout(c); }); } } /* display shows the treemap and writes the embedded transition function */ function display(d) { var legend = svg.selectAll(".legend") .data(data_colorScale.domain().slice()) .enter() .append("g") .attr("class", "legend") // .attr("transform", function (d, i) { return "translate(0," + i * 20 + ")"; }); .attr("transform", function (d, i) { return "translate(" + i * 150 + ",0)"; }); // create grandparent bar at top grandparent .datum(d.parent) .on("click", transition) .select("text") .text(name(d)); var g1 = svg.insert("g", ".grandparent"); //.datum(d) //.attr("class", "depth"); // add in data var g = g1.selectAll("g") //g1 .data(d.children) .enter() .append("g"); // write parent rectangle g.append("rect") .attr("class", "parent") // Call the rectangle function to assign dimensions to the rectangle .call(rect); // .attr("fill", function(d) { return data_colorScale(d.value)}); /* .append("title") .text(function(d) { return d.name + " " + formatNumber(d.value); }); */ legend.append("rect") .attr("x", 0) .attr("y", height + 5) .attr("width", 180) .attr("height", 100) .style("fill", data_colorScale); legend.append("text") .attr("x", 125) .attr("y", height) .attr("dy", "1.80em") .attr("stroke","black") .style("text-anchor", "end") .text(function (d){ return d; }); //getDepth(d); // write children rectangles var k = g.selectAll(".child") .data(function(d) { return d.children || [d]; }) //.data(function(d) { return d; }) .enter(); k.append("rect") .attr("class", "child") // Highlights the children .call(rect) // Call the rectangle function to assign dimensions to the rectangle .append("title") .text(function(d) { return d.name + "-" + formatNumber(d.value); }); /*should be d.value*/ // Add text // k.append("text") // .call(text); k.append("foreignObject") .call(rect) .attr("class","foreignobj") .append("xhtml:div") .html(function(d) { return d.name; }) // Display Child name // Assign style to the added text // .attr("class","textdiv"); //textdiv class allows us to style the text easily with CSS .attr("class", function(d) { if ((d.dy <= 25 && d.dx <= 25) || (d.dy <= 25 && d.dx > 25) || (d.dy > 25 && d.dx <= 25) ) { return "textdiv_xs"; } if ((d.dy > 25 && d.dy <=50) && (d.dx > 25 && d.dx <=50) || (d.dy > 50 && d.dy <=75) && (d.dx > 50 && d.dx <=75) // || // ((d.dy > 25 && d.dy <=50) && d.dx >25) || // (d.dy > 25 &&(d.dx > 25 && d.dx <=50)) ) { return "textdiv_s"; } if (((d.dy > 25 && d.dy <=50) && d.dx >25) || (d.dy > 25 &&(d.dx > 25 && d.dx <=50)) || // (d.dy > 50 && d.dy <=75) && (d.dx > 50 && d.dx <=75) || ((d.dy > 50 && d.dy <=75) && d.dx >50) || (d.dy > 50 &&(d.dx > 50 && d.dx <=75)) ) { return "textdiv_s"; } if ((d.dy > 75 && d.dy <=100) && (d.dx > 75 && d.dx <=100) || ((d.dy > 75 && d.dy <=100) && d.dx >75) || (d.dy > 75 && (d.dx > 75 && d.dx <=100)) ) { return "textdiv_m"; } if (d.dy > 100 && d.dx > 100 || d.dy < 100 && d.dx > 100 || d.dy > 100 && d.dx < 100) { return "textdiv"; } }); // transition on child click g.filter(function(d) { return d.children; }) .classed("children", true) .on("click", transition); // create transition function for transitions function transition(d) { if (transitioning || !d) return; // if ( !d ) return; transitioning = true; var g2 = display(d); t1 = g1.transition().duration(750), t2 = g2.transition().duration(500); // Update the domain only after entering new elements. x.domain([d.x, d.x + d.dx]); y.domain([d.y, d.y + d.dy]); // Transition to the new view. g2.selectAll("rect").call(rect); g2.append("foreignObject") .call(rect) .attr("class","foreignobj") .append("xhtml:div") .html(function(d) { if (d.children) {return d.name;} // if (d.parent) {return null;} }) ; g2.selectAll(".foreignobj").call(foreign); // Remove the old node when the transition is finished. t1.remove().each("end", function() { svg.style("shape-rendering", "crispEdges"); transitioning = false; }); }//endfunc transition return g; }//endfunc display // This function defines the placements of text within each parent/child function text(text) { text.attr("x", function(d) { return x(d.x) + 6; }) .attr("y", function(d) { if (d.dy <= 8) { return y(d.y) - 9; } if (d.dy > 8 && d.dy <= 13) { return y(d.y) - 10; } if (d.dy > 13) { return y(d.y) - 8; } }) .attr("dy", "1.7em") .attr("stroke","black") .style("text-anchor", "center") // .attr("class","textdiv") // .text("Test 01"); .attr("font-size", function(d) { if (d.dy <= 13 && d.dx <= 50) { return 5; } if (d.dy <= 13 && d.dx <= 70) { return 6; } if (d.dy <= 50 && d.dx <= 50) { return 7; } if (d.dy <= 75 && d.dx <= 75) { return 9; } if (d.dy <= 100 && d.dx <= 100) { return 10; } if (d.dy <= 150 && d.dx <= 150) { return 12; } if (d.dy > 150 && d.dx > 150) { return 14; } }) .text(function(d) { return d.name; });//to display the name } //This function defines the placements of text within each parent/child function text_2(text) { text.attr("x", function(d) { return x(d.x) + 6; }) .attr("y", function(d) { return y(d.y) + 6; }); // .text(function(d) { return d.name; }); } //This function defines the placements of rectangles for each parent/child function rect(rect) { rect.attr("x", function(d) { return x(d.x); }) .attr("y", function(d) { return y(d.y); }) .attr("width", function(d) { return x(d.x + d.dx) - x(d.x); }) .attr("height", function(d) { return y(d.y + d.dy) - y(d.y); }) // .attr("fill", function(d) { return data_colorScale(d.name)}) .style("fill", function(d) { return d.children ? data_colorScale(d.parent.name) : "green"; }) .attr("stroke","white"); } //This function defines the placements of rectangles for each parent/child function rect_transition(rect) { rect.attr("x", function(d) { return x(d.x); }) .attr("y", function(d) { return y(d.y); }) .attr("width", function(d) { return x(d.x + d.dx) - x(d.x); }) .attr("height", function(d) { return y(d.y + d.dy) - y(d.y); }) .style("fill", function(d) { return d.children ? data_colorScale(d.parent.name) : "green" ; }) .attr("stroke","white"); } function foreign(foreign){ /* added */ foreign.attr("x", function(d) { return x(d.x); }) .attr("y", function(d) { return y(d.y); }) .attr("width", function(d) { return x(d.x + d.dx) - x(d.x); }) .attr("height", function(d) { return y(d.y + d.dy) - y(d.y); }); } // This function is used to prepare the text at the header of the treemap function name(d) { return d.parent ? name(d.parent) + "." + d.name : d.name; } function loadData(root) { initialize(root); accumulate(root); layout(root); display(root); }