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, height = 775 - margin.bottom, 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; }); .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; } // 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") .data(d.children) .enter() .append("g"); // .call(rect); // 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; }); // write children rectangles g.selectAll(".child") .data(function(d) { return d.children || [d]; }) //.data(function(d) { return d; }) .enter() .append("rect") // Highlights the children .attr("class", "child") // Call the rectangle function to assign dimensions to the rectangle .call(rect); // .append("title") // .transition() /* .append("text") .attr("x", 125)//function(d) { return x(d.x) / 2 ; }) .attr("y", height)//function(d) { return y(d.y) / 2 ; }) .attr("dx", "10.35em") .attr("dy", "10.80em") .attr("stroke","white") // .each(wordwrap) // .style("font-size","18px") .style("text-anchor", "middle") .text("Test text for children "); //function(d) { return d.name }) // return d.name + " " + formatNumber(d.value); }) */ g.append("text") .attr("x", function(d) { return x(d.x) / 2 ; }) .attr("y", function(d) { return y(d.y) / 2 ; }) .attr("dy", "1.80em") .attr("stroke","white") .style("text-anchor", "end") .text(function (d,i,j){ return d.children[i];}); //.children[i].children[j].name; }); /* // transition on child click g.filter(function(d) { return d.children; }) .classed("children", true) .on("click", transition); var g2 = display(d), t2 = g2.transition().duration(200); t2.selectAll("text").call(text).style("fill-opacity", 1); t2.selectAll("rect").call(rect_transition); t2.selectAll(".textdiv").style("display", "block"); t2.selectAll(".foreignobj").call(foreign); */ /* // Adding a foreign object instead of a text object, allows for text wrapping g.append("foreignObject") // .data(function(d) { return d.children || [d]; }) // .display(d) // .call(rect) .attr("class","foreignobj") .append("xhtml:div") // Display Parent / Child name + Net Sales Value .html(function(d,i) { if (d.value > 0 && typeof(d.value) !== "undefined") { return d.children[i].name + " (" + formatNumber(d.children[i].value) + ")";// + " (" + formatNumber(d.value) + ")"; // return d.name + " (" + formatNumber(d.value) + ")"; } }) ; // Assign style to the added text .attr("class","textdiv"); //textdiv class allows us to style the text easily with CSS */ // 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(750); // Update the domain only after entering new elements. // x.domain([d.x, d.x + d.dx]); // y.domain([d.y, d.y + d.dy]); // Enable anti-aliasing during the transition. //svg.style("shape-rendering", null); // Draw child nodes on top of parent nodes. // svg.selectAll(".depth").sort(function(a, b) { return a.depth - b.depth; }); // Fade-in entering text. // g2.selectAll("text").style("fill-opacity", 0); // g2.selectAll("foreignObject div").style("display", "none"); /*added*/ // Transition to the new view. // t1.selectAll("text").call(text).style("fill-opacity", 0); // t2.selectAll("text").call(text).style("fill-opacity", 1); // t1.selectAll("rect").call(rect_transition); // t2.selectAll("rect").call(rect_transition); // t1.selectAll(".textdiv").style("display", "none"); /* added */ // t1.selectAll(".foreignobj").call(foreign); /* added */ // t2.selectAll(".textdiv").style("display", "block"); /* added */ // t2.selectAll(".foreignobj").call(foreign); /* added */ // 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) { return y(d.y) + 6; }); } //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.parent ? data_colorScale(d.parent.name) : null; }) .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) : "lightblue" ; }) .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); }