HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs 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 its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<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>
.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;
}
$("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";}
}
Also see: Tab Triggers