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.
.container
h1 The Dev Team @ FINE
.display.js-display
@import url(https://fonts.googleapis.com/css?family=Lato);
*,
*:before,
*:after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html {
font-family: 'Lato', sans-serif;
font-size: 85%;
}
body {
background: #ececec;
}
.container {
width: 680px;
margin: 0 auto;
}
h1 {
margin: .5em 0;
}
p {
margin-bottom: 1em;
}
svg {
font-size: 75%;
}
.node {
&-rect {
fill: saturate(darken(#adb946, 5%), 5%);
opacity: .85;
}
}
.axis-group {
path {
fill: none;
stroke: black;
}
line {
stroke: black;
}
}
form {
float: left;
margin: 0 2em 1em 0;
.input {
margin-bottom: 5px;
}
input {
margin-right: 5px;
position: relative;
top: -1px;
}
}
.display {
margin-bottom: 4em;
}
if (!d3.chart) {
d3.chart = {};
}
d3.chart.visual = function() {
var div,
svg,
xScale,
xAxis,
xAxisGroup,
yScale,
yAxis,
yAxisGroup,
nodes,
nodesEnter,
margin = { top: 20, right: 20, bottom: 40, left: 45 },
width = 680 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
function todaysDate() {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1;
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
return new Date(yyyy + '-' + mm + '-' + dd);
}
function age(birthdate) {
var today = todaysDate();
var bday = new Date(birthdate);
var age = ((today - bday) / 31536000000).toFixed(2); // Milliseconds in a year
return parseFloat(age);
}
function yearsAtWork(startdate) {
var today = todaysDate();
var startDay = new Date(startdate);
var months = ((today - startDay) / 31536000000).toFixed(2); // Milleseconds in a year
return parseFloat(months);
}
function chart(container) {
div = container;
// XScale Toggle Form
var scaleForm = div.append('form');
var scaleHeading = scaleForm.append('h3').text('Toggle');
var scaleInputOne = scaleForm.append('div').classed('input', true);
var scaleInputTwo = scaleForm.append('div').classed('input', true);
scaleInputOne
.append('input')
.attr({
type: 'radio',
name: 'scale',
id: 'age',
checked: true
})
scaleInputOne.append('label')
.attr('for', 'age')
.text('Age')
scaleInputTwo.append('input')
.attr({
type: 'radio',
name: 'scale',
id: 'experience'
})
scaleInputTwo.append('label')
.attr('for', 'experience')
.text('Years At FINE')
// Sort Form aka yScale Toggle Form
var sortForm = div.append('form');
var sortHeading = sortForm.append('h3').text('Sort');
var sortInputOne = sortForm.append('div').classed('input', true);
var sortInputTwo = sortForm.append('div').classed('input', true);
var sortInputThree = sortForm.append('div').classed('input', true);
sortInputOne
.append('input')
.attr({
type: 'radio',
name: 'sort',
id: 'alphabetical',
checked: true
})
sortInputOne.append('label')
.attr('for', 'alphabetical')
.text('By Name')
sortInputTwo.append('input')
.attr({
type: 'radio',
name: 'sort',
id: 'by_age'
})
sortInputTwo.append('label')
.attr('for', 'by_age')
.text('By Age')
sortInputThree.append('input')
.attr({
type: 'radio',
name: 'sort',
id: 'by_years'
})
sortInputThree.append('label')
.attr('for', 'by_years')
.text('By Years At FINE')
svg = div.append('svg').classed('visual', true)
.attr({
width: width + margin.left + margin.right,
height: height + margin.top + margin.bottom
})
.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')')
svg.append('g').classed('x axis-group', true)
.attr('transform', 'translate(0,' + height +')')
svg.append('g').classed('y axis-group', true)
xScale = d3.scale.linear()
.domain([0, d3.max(data, function(d) { return age(d.birthdate); })])
.range([0, width])
xAxis = d3.svg.axis()
.scale(xScale)
.orient('bottom')
yScale = d3.scale.ordinal()
.domain(data.map(function(d) { return d.name; }))
.rangeRoundBands([0, height], .1)
yAxis = d3.svg.axis()
.scale(yScale)
.orient('left');
update();
}
chart.update = update;
function update() {
nodes = svg.selectAll('.node')
.data(data, function(d) { return d.id; })
nodesEnter = nodes.enter()
.append('g').classed('node', true)
.attr('transform', function(d) { return 'translate(0,' + yScale(d.name) + ')'; })
nodesEnter.append('rect').classed('node-rect', true)
.attr({
x: 0,
width: 0,
height: yScale.rangeBand()
})
.transition().delay(function(d,i) { return i * 100; })
.attr('width', function(d) { return xScale(age(d.birthdate)); });
nodes.exit().remove();
// X Axis
xAxisGroup = svg.select('.x.axis-group');
xAxis(xAxisGroup);
svg
.append('text')
.text('Years')
.attr({
'text-anchor': 'middle',
'transform': 'translate(' + (width / 2) + ',' + (height + margin.bottom) + ')',
'dy': '-.35em'
})
// Y Axis
yAxisGroup = svg.select('.y.axis-group');
yAxis(yAxisGroup);
d3.selectAll('input[name="scale"]')
.on('change', function() {
toggle(d3.select(this).attr('id'));
})
d3.selectAll('input[name="sort"]')
.on('change', function() {
sortDevs(d3.select(this).attr('id'));
})
}
function toggle(value) {
var domain,
property,
fn;
if (value === 'age') {
domain = [0, d3.max(data, function(d) { return age(d.birthdate); })];
property = 'birthdate';
fn = age;
} else {
domain = [0, d3.max(data, function(d) { return yearsAtWork(d.startdate); })];
property = 'startdate';
fn = yearsAtWork;
}
xScale.domain(domain);
xAxis.scale(xScale);
xAxisGroup.transition().call(xAxis);
d3.selectAll('.node-rect').transition().attr('width', function(d) { return xScale(fn(d[property])); });
}
function sortDevs(value) {
var sorted;
if (value === 'alphabetical') {
sorted = data.sort(function(a,b) { return d3.ascending(a.name, b.name); });
} else if (value === 'by_age') {
sorted = data.sort(function(a,b) { return new Date(a.birthdate) - new Date(b.birthdate); });
} else {
sorted = data.sort(function(a,b) { return new Date(a.startdate) - new Date(b.startdate); });
}
chart.data(sorted);
yScale.domain(sorted.map(function(d) { return d.name; }));
yAxis.scale(yScale);
yAxisGroup.transition().duration(500).call(yAxis);
nodesEnter.transition().duration(500).attr('transform', function(d) { return 'translate(0,' + yScale(d.name) + ')'; });
}
chart.data = function(value) {
if (!arguments.length) return data;
data = value;
return chart;
}
chart.width = function(value) {
if (!arguments.length) return width;
width = value;
return chart;
}
chart.height = function(value) {
if (!arguments.length) return height;
height = value;
return chart;
}
return chart;
};
var json = {
"teammates": [
{
"id": 1,
"name": "Charlie",
"birthdate": "1988-10-17",
"position": "DevOps",
"startdate": "2015-07-06"
},
{
"id": 2,
"name": "Eman",
"birthdate": "1986-09-16",
"position": "Front End",
"startdate": "2015-05-12"
},
{
"id": 3,
"name": "James",
"birthdate": "1983-03-02",
"position": "Director",
"startdate": "2008-09-16"
},
{
"id": 4,
"name": "Jason",
"birthdate": "1980-09-30",
"position": "Back End",
"startdate": "2005-03-01"
},
{
"id": 5,
"name": "Kyle",
"birthdate": "1985-04-10",
"position": "Full Stack",
"startdate": "2013-11-11"
},
{
"id": 6,
"name": "Mark",
"birthdate": "1979-07-13",
"position": "Director",
"startdate": "2007-10-23"
},
{
"id": 7,
"name": "Nichole",
"birthdate": "1988-10-15",
"position": "Back End",
"startdate": "2015-03-16"
},
{
"id": 8,
"name": "Niles",
"birthdate": "1983-12-30",
"position": "Back End",
"startdate": "2014-04-25"
},
{
"id": 9,
"name": "Tim",
"birthdate": "1991-10-15",
"position": "Full Stack",
"startdate": "2014-06-28"
},
{
"id": 10,
"name": "Travis",
"birthdate": "1986-02-09",
"position": "Front End",
"startdate": "2015-04-15"
}
]
};
var data = json.teammates;
var display = d3.select('.js-display');
var visual = d3.chart.visual()
.data(data);
visual(display);
Also see: Tab Triggers