Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="logo">
    <a href='http://interworks.com'><img src="http://d3demo.preview.interworks.com/logo.png" width="195" height="42" alt=""/></a>
    </div>
    <div id="title"><span class="header"> D3+Tableau Demo - Federal Budget Dashboard</span></div>
  <div id="main">
    	<div id="d3viz">
        </div>
        <div id="tableau">      <!--https://public.tableausoftware.com/views/FederalSpending_0/2014?:embed=y&:display_count=no-->
        </div>
                <div id="intro">
                  <h2>D3+Tableau Demo</h2>
                  <p> This dashboard integrates a custom visualization using <strong>D3.js</strong> (left) with a <strong>Tableau </strong>workbook (right). </p>
                  <p>Select a name in the tree to navigate. </p>
                </div>
    </div>

              
            
!

CSS

              
                .node circle {
  cursor: pointer;
  fill: #fff;
  stroke: steelblue;
  stroke-width: 1.5px;
}

.node text {
  font-size: 11px;
  cursor:pointer;
  fill:black;
}

.selected {
    fill: red;
}

/*.root text{
	font-size:16px;
}*/

path.link {
  fill: none;
  stroke: #ccc;
  stroke-width: 1px;
}

#main{
	width: 1000px;
	height: 675px;
	background-color: #FFFFFF;
	margin-left: auto;
	margin-right: auto;
}

#d3viz{
	width: 680px;
	height: 600px;
	float: left;
	margin: 0;
	z-index: 999;
	font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}

#intro{
	display: block;
	position: absolute;
	width: 275px;
	float: left;
	margin-top: 100px;
	margin-left: 373px;
	font-size: medium;
	color: #000000;
	font-family: open-sans;
	font-style: normal;
	font-weight: 300;
}

#tableau{
	width: 320px;
	height: 800px;
	float: left;
	margin: 0;
	display: block;
}

#logo {
	margin-top: 45px;
	margin-left: auto;
	margin-right: auto;
	width: 1000px;
}

              
            
!

JS

              
                $("iframe").contents().find(".tabDashboard");
console.log($("iframe").contents());
var m = [20, 120, 20, 50],
    w = 680 - m[1] - m[3],
    h = 600 - m[0] - m[2],
    i = 0,
    root;
	
var selected = null;
var colored = null;
var firstClick = true; //to get around double-firing on first Tableau click

var tree = d3.layout.tree()
    .size([h, w]);

var diagonal = d3.svg.diagonal()
    .projection(function(d) { return [d.y, d.x]; });

var dvis = d3.select("#d3viz").append("svg:svg")
    .attr("width", w + m[1] + m[3])
    .attr("height", h + m[0] + m[2])
  .append("svg:g")
    .attr("transform", "translate(" + m[3] + "," + m[0] + ")");

d3.json("https://codepen.io/robertrouse/pen/GpRBOm.js", function(json) {
  root = json;
  root.x0 = h / 2;
  root.y0 = 0;

  function toggleAll(d) {
    if (d.children) {
      d.children.forEach(toggleAll);
      toggle(d);
    }
  }

  // Initialize the display to show a few nodes.
  root.children.forEach(toggleAll);
  selected = root.name[0];
  colored = selected;
  toggle(selected);
  update(root);
  
  //test filter
  tabfilter();
});

//listen for Tableau selections
//viz.addEventListener('marksselection', selectFunc);

function update(source) {
  var duration = d3.event && d3.event.altKey ? 5000 : 500;

  // Compute the new tree layout.
  var nodes = tree.nodes(root).reverse();

  // Normalize for fixed-depth.
  nodes.forEach(function(d) { 
  	if (d.depth==0){ 
		d.y = 20; 
	}
	else if (d.depth==1){
		d.y = 260;
	}
	else {
		d.y = 350;
	}
	});

  // Update the nodes…
  var node = dvis.selectAll("g.node")
      .data(nodes, function(d) { return d.id || (d.id = ++i); });

  // Enter any new nodes at the parent's previous position.
  var nodeEnter = node.enter().append("svg:g")
      .attr("class", "node")
      .attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
      .on("click", function(d) {
		  //on first click, hide explainer div
		  document.getElementById("intro").style.display = "none";

		  //expand if clicked node is not root & not selected
		  //todo: set filter on parents where one bureau exists in multiple agencies
		  if(d.depth==1 && d!=selected){ 
			toggle(selected);
			toggle(d); 
			update(d); 
			tabfilter(d); 
		  } else {
		  	tabfilter(d);
			colored=d;
			update(d);
		  }
		});

  nodeEnter.append("svg:circle")
      .attr("r", 1e-6)
      .style("fill", function (d){ 
	  		if (d==colored){
				return "red";
			}else if(d._children){return "lightsteelblue"; 
			}else{return "#fff";}
			});
	  
  nodeEnter.append("svg:text")
      .attr("x", function(d) { return d.children || d._children ? -10 : 10; })
      .attr("dy", ".35em")
      .attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; })
	  .attr("font-size",function(d) { return d.depth==0?"14px":"11px"})
	  .attr("cursor","pointer")
      .text(function(d) { 
	  	if (d.depth==0){
	  		return "Federal"; 
		}
		else{
			return splitName(d);
		}
	  })
      .style("fill-opacity", 1e-6);

  // Transition nodes to their new position.
  var nodeUpdate = node.transition()
      .duration(duration)
      .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });

  nodeUpdate.select("circle")
      .attr("r", 4.5)
      .style("fill", function (d){ 
	  		if (d==colored){
				return "red";
			}else if(d._children){return "lightsteelblue"; 
			}else{return "#fff";}
			});
	  
  nodeUpdate.select("text")
      .style("fill-opacity", 1);

  // Transition exiting nodes to the parent's new position.
  var nodeExit = node.exit().transition()
      .duration(duration)
      .attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
      .remove();

  nodeExit.select("circle")
      .attr("r", 1e-6);

  nodeExit.select("text")
      .style("fill-opacity", 1e-6);

  // Update the links…
  var link = dvis.selectAll("path.link")
      .data(tree.links(nodes), function(d) { return d.target.id; });

  // Enter any new links at the parent's previous position.
  link.enter().insert("svg:path", "g")
      .attr("class", "link")
      .attr("d", function(d) {
        var o = {x: source.x0, y: source.y0};
        return diagonal({source: o, target: o});
      })
    .transition()
      .duration(duration)
      .attr("d", diagonal);

  // Transition links to their new position.
  link.transition()
      .duration(duration)
      .attr("d", diagonal);

  // Transition exiting nodes to the parent's new position.
  link.exit().transition()
      .duration(duration)
      .attr("d", function(d) {
        var o = {x: source.x, y: source.y};
        return diagonal({source: o, target: o});
      })
      .remove();

  // Stash the old positions for transition.
  nodes.forEach(function(d) {
    d.x0 = d.x;
    d.y0 = d.y;
  });
//console.log("updated");
}


// Toggle children.
function toggle(d) {
  //selected = (d.depth!=2) ? d : selected;
  //d.select(this).attr("fill")="red"
  selected = d;
  colored = selected;
  if (d.children) {
    d._children = d.children;
    d.children = null;
  } else {
    d.children = d._children;
    d._children = null;
  }
  //console.log(selected);
}

//filter Tableau viz
function tabfilter(d) {
  var filterString = "";
  if (d.depth==0){
     setParameter('filterInput','Federal');
  }else if(d.depth==1){
    filterString = 'Agency|'+d.name;
     setParameter('filterInput',filterString);
  } else {
     filterString = 'Bureau|'+d.parent.name+'|'+d.name;
     setParameter('filterInput',filterString);
  }
} 

function splitName(d) {
    var str = d.name;
	var res = str;
    if (d.depth==2 && str.length>50) {
		res = str.substring(0,50)+"..."
	}
    return res;
}
function colorSelected (d) {
	  		if (d==selected){
				return "lightsteelblue";
			}else if(d._children){return "#ccc"; 
			}else{return "#fff";}
}

              
            
!
999px

Console