123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- Shiny.addCustomMessageHandler("treemap2_values", function(message) {
- var data = message[0]; // Get the single object inside the message
- var margin = {top: 25, right: 0, bottom: 0, left: 0},
- width = window.innerWidth * 0.9,
- height = window.innerHeight * 0.6,
- formatNumber = d3.format(",d"),
- transitioning,
- hovering;
- var tooltip = d3.select("body")
- .append("div")
- .style("position", "absolute")
- .style("z-index", "10")
- .style("visibility", "hidden")
- .style("color", "white")
- .style("padding", "8px")
- .style("background-color", "rgba(0, 0, 0, 0.75)")
- .style("border-radius", "6px")
- .style("font", "12px sans-serif")
- .text("tooltip");
- var x = d3.scaleLinear()
- .domain([0, width])
- .range([0, width]);
- var y = d3.scaleLinear()
- .domain([0, height - margin.top - margin.bottom])
- .range([0, height - 3*margin.top - margin.bottom]);
- var c = [];
- function q (d){
- for (var i = 0; i < d.children.length; i++) {
- c[i] = d.children[i].value;
- //Do something
- }
- return c.sort(function(a,b){return (a - b);});
- }
- q(data);
- var maxValue = c[c.length - 1];
- var minValue = maxValue / 9;
- var color = d3.scaleThreshold()
- .domain([minValue, minValue*2, minValue*3, minValue*4, minValue*5, minValue*6, minValue*7, minValue*8])
- .range(d3.schemeBlues[9]);
- // var color = d3.scaleQuantile()
- // .domain(/*GET VALUE BY LEVEL*/
- // .range(d3.schemeBlues[9]);
- //var color = d3.scaleOrdinal()
- // .range(d3.schemeSet3);
- //.map(function(c) { c = d3.rgb(c); c.opacity = 1; return c; });
- //var color = d3.scaleOrdinal(d3.schemeCategory20.map(fader)); //schemeCategory20* is no longer supported
- //var fader = function(color) { return d3.interpolateRgb(color, "#fff")(0.2); };
- var format = d3.format(",d");
- var createTreemap; //Modificado de treemap
- var treemap, grandparent; //Modificar de svg para treemap
- updateDrillDown();
- function updateDrillDown() {
- if (treemap) { //Modificar de svg para treemap
- treemap.selectAll("*").remove(); //Modificar de svg para treemap
- } else {
- // var treemap = d3.layout.treemap()
- // .children(function(d, depth) { return depth ? null : d._children; })
- // .sort(function(a, b) { return a.value - b.value; })
- // .ratio(height / width * 0.5 * (1 + Math.sqrt(5)))
- // .round(false);
- treemap = d3.select("#treemap2_area").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");
- grandparent = treemap.append("g") //Modificar de svg para treemap
- .attr("class", "grandparent");
- grandparent.append("rect")
- .style("fill", "#ADEAEA")
- //.style("stroke", "#ADEAEA")
- .style("padding", "1px")
- .attr("y", -margin.top)
- .attr("width", width)
- .attr("height", margin.top);
- grandparent.append("text")
- .attr("x", 6)
- .attr("y", 6 - margin.top)
- .attr("dy", ".75em");
- createTreemap = d3.treemap() //Modificado de treemap
- .tile(d3.treemapBinary) //.ratio(height / width * 0.5 * (1 + Math.sqrt(5))/2))
- .size([width, height - 10])
- .round(false)
- .paddingInner(0);
- }
- var root = d3.hierarchy(data)
- .eachBefore(function(d) { d.id = (d.parent ? d.parent.id + "." : "") + d.data.name; })
- //.sum((d) => d.value)
- .sum(function(d) {
- //console.log(d.value);
- return d.value;
- })
- .sort(function(a, b) {
- //console.log('initial root sort a ' + a.value + ' b ' + b.value);
- return b.height - a.height || b.value - a.value;
- });
- initialize(root);
- accumulate(root);
- layout(root);
- createTreemap(root); //Modificado de treemap
- display(root);
- };
- function initialize(root) {
- root.x = root.y = 0;
- root.x1 = width;
- root.y1 = height;
- root.depth = 0;
- }
- // Aggregate the values for internal nodes. This is normally done by the
- // treemap layout, but not here because of our custom implementation.
- // We also take a snapshot of the original children (_children) to avoid
- // the children being overwritten when when layout is computed.
- function accumulate(d) {
- //console.log('accumulate called ' + d.data.name);
- return (d._children = d.children)
- ? d.value = d.children.reduce(function(p, v) { return p + accumulate(v); }, 0) : d.value;
- }
- // Compute the treemap layout recursively such that each group of siblings
- // uses the same size (1×1) rather than the dimensions of the parent cell.
- // This optimizes the layout for the current zoom state. Note that a wrapper
- // object is created for the parent node for each group of siblings so that
- // the parent’s dimensions are not discarded as we recurse. Since each group
- // of sibling was laid out in 1×1, we must rescale to fit using absolute
- // coordinates. This lets us use a viewport to zoom.
- function layout(d) {
- if (d._children) {
- // treemap.nodes({_children: d._children});
- // treemap(d);
- d._children.forEach(function(c) {
- /*c.x0 = d.x0 + c.x0 * (d.x1 - d.x0);
- c.y0 = d.y0 + c.y0 * (d.y1 - d.y0);
- c.x1 *= d.x1;
- c.y1 *= d.y1;*/
- c.x0 = d.x0 + c.x0 * d.x1;
- c.y0 = d.y0 + c.y0 * d.y1;
- c.x1 *= (d.x1 - d.x0);
- c.y1 *= (d.y1 - d.y0);
- c.parent = d;
- layout(c);
- });
- }
- }
- function display(d) {
- grandparent
- .datum(d.parent)
- .on("click", transition)
- .select("text")
- .text(name(d));
- var g1 = treemap.insert("g", ".grandparent") //Modificar de svg para treemap
- .datum(d)
- .attr("class", "depth");
- var g = g1.selectAll("g")
- .data(d._children)
- .enter().append("g").style("opacity", 1);
- g.filter(function(d) { return d._children; })
- .classed("children", true)
- .on("click", transition);
- var children = g.selectAll(".child")
- .data(function(d) { return d._children || [d]; })
- .enter().append("g")//SHOW CHILDREN FROM TREEMAP VIEW
- .style("opacity", 0.7);// DEFINES THE OPACITY OF THE INTERNAL NODES
- children.append("rect")
- .attr("class", "child")
- .call(rect)
- .append("title")
- .text(function(d) { return d.data.name + " (" + formatNumber(d.data.value) + ")"; });
- children.append("text")
- .attr("class", "ctext").style("font-size", "12px")
- .text(function(d) { return d.data.name; })
- .call(text2);
- g.append("rect")
- .attr("class", "parent")
- .call(rect);
- //MOUSEOVER: SHOW CHILDREN OF THE NODES
- g.selectAll("rect").classed("parent",true).on("mouseover", function(d) {
- d3.select(this).style("opacity", 0.3);//.
- tooltip.html(d.data.name + "<br/>" + formatNumber(d.data.value));
- tooltip.transition();
- return tooltip.style("visibility", "visible").style("opacity", 1);
- //children.select("g").style("opacity", 1);
- });
- //MOUSEOUT: HIDE CHILDREN OF THE NODES
- g.selectAll("rect").classed("parent",false).on("mouseout", function() {
- d3.select(this).style("opacity", 1);
- return tooltip.style("visibility", "hidden");
- });
- g.selectAll("rect").classed("parent",false).on("mousemove", function() {
- d3.select(this).style("opacity", 0.3);
- tooltip.style("opacity", 1);
- return tooltip.style("top",(d3.event.pageY-10)+"px").style("left",(d3.event.pageX+10)+"px");
- });
- var t = g.append("text")
- .attr("class", "ptext").style("font-weight", "bold")
- .attr("dy", ".75em");
- t.append("tspan")
- .attr("font-weight", "bold")
- .text(function(d) { return d.data.name; });
- t.append("tspan")
- .attr("dy", "1.0em")
- .text(function(d) { return formatNumber(d.data.value); });
- t.call(text);
- g.selectAll("rect")
- .style("fill", function(d) { return color(d.data.value); });
- function transition(d) {
- if (transitioning || !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.x0, d.x0 + d.x1]);
- //y.domain([d.y0, d.y0 + d.y1]);
- x.domain([d.x0, d.x0 + (d.x1 - d.x0)]);
- y.domain([d.y0, d.y0 + (d.y1 - d.y0)]);
- // Enable anti-aliasing during the transition.
- treemap.style("shape-rendering", null); //Modificar de svg para treemap
- // Draw child nodes on top of parent nodes.
- //treemap.selectAll(".depth").sort(function(a, b) { //Modificar de svg para treemap
- //console.log('.depth sort a ' + a.depth + ' b ' + b.depth);
- //return a.depth - b.depth; });
- // Fade-in entering text.
- g2.selectAll("text").style("fill-opacity", 0);
- // Transition to the new view.
- t1.selectAll(".ptext").call(text).style("fill-opacity", 0).style("font-weight", "bold");//.style("font-size", "12px");
- t2.selectAll(".ptext").call(text).style("fill-opacity", 1).style("font-weight", "bold");//.style("font-size", "12px");
- t1.selectAll(".ctext").call(text2).style("fill-opacity", 0);
- t2.selectAll(".ctext").call(text2).style("fill-opacity", 1);
- t1.selectAll("rect").call(rect);//.style("fill", function(d) { return color(d.data.value); });
- t2.selectAll("rect").call(rect);//.style("fill", function(d) { return color(d.data.value); });
- // Remove the old node when the transition is finished.
- t1.remove().on("end", function() {
- treemap.style("shape-rendering", "crispEdges"); //Modificar de svg para treemap
- transitioning = false;
- });
- }
- return g;
- }
- function make_title(d){
- //console.log("making_title", d);
- return d ? d.data.name + " (" + formatNumber(d.data.value) + ")" : d.data.name + " (" + formatNumber(d.data.value) + ")";
- }
- function text(text) {
- text.selectAll("tspan")
- .attr("x", function(d) { return x(d.x0) + 5;});
- text.attr("x", function(d) { return x(d.x0) + 5;})
- .attr("y", function(d) { return y(d.y0) + 3;})
- .style("opacity", function(d) {
- var w = x(d.x1) - x(d.x0);
- //console.log("text opacity setting textlength " + this.getComputedTextLength() + " d size " + w);
- return this.getComputedTextLength() <= w ? 1 : 0; })
- .style("font-size", "10px");
- }
- function text2(text) {
- text
- .attr("x", function(d) {
- return x(d.x1) - this.getComputedTextLength() - 2;
- })
- .attr("y", function(d) { return y(d.y1) - 2; })
- .style("opacity", function(d) {
- var w = x(d.x1) - x(d.x0);
- //console.log("text2 opacity setting textlength " + this.getComputedTextLength() + " d size " + w);
- return this.getComputedTextLength() <= w ? 1 : 0;
- })
- .style("font-size", "10px");
- }
- function rect(rect) {
- rect.attr("x", function(d) { return x(d.x0); })
- .attr("y", function(d) { return y(d.y0); })
- .attr("width", function(d) {
- var w = x(d.x1) - x(d.x0) - 1;
- //console.log('id ' + d.id +' rect width ' + w);
- return w;
- })
- .attr("height", function(d) {
- var h = y(d.y1) - y(d.y0);
- //console.log('id ' + d.id +' rect height ' + h);
- return h;
- })
- .style("stroke", "darkblue")
- .style("stroke-width", 1);
- }
- function name(d) {
- return d.parent ? name(d.parent) + " -> " + d.data.name + " (" + formatNumber(d.data.value) + ")" : d.data.name + " (" + formatNumber(d.data.value) + ")";
- }
- });
|