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.
<!-- markup structure
.app, wrapping container
header, introducing the visualization
.input, showing the array behind the visualization
.visualization, showing one circle for each data point
.controls, allowing to add or remove data points in the array
.selection, highlighting the selections after each operation
-->
<div class="app">
<header>
<h1>D3 Data Binding</h1>
</header>
<div class="input">
<p>
<strong>Input array:</strong>
<!-- include here the array describing the data points -->
<span></span>
</p>
</div>
<!-- include here one sphere for each data point -->
<main class="visualization"></main>
<div class="controls">
<button class="controls--add">Add data point</button>
<button class="controls--remove">Remove data point</button>
</div>
<!-- include in each paragraph the data points described in the enter/update/exit selection -->
<div class="selection">
<div class="selection--enter">
<h3>Enter Selection</h3>
<p></p>
</div>
<div class="selection--update">
<h3>Update Selection</h3>
<p></p>
</div>
<div class="selection--exit">
<h3>Exit Selection</h3>
<p></p>
</div>
</div>
</div>
@import url("https://fonts.googleapis.com/css?family=Lato:400,900&display=swap");
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
min-height: 100vh;
background: repeating-linear-gradient(
-45deg,
hsl(0, 0%, 94%) 0px,
hsl(0, 0%, 94%) 5px,
transparent 0px,
transparent 40px
),
repeating-linear-gradient(
-45deg,
hsl(0, 0%, 94%) 0px,
hsl(0, 0%, 94%) 10px,
transparent 0px,
transparent 100px
),
hsl(0, 0%, 97%);
font-family: "Lato", sans-serif;
}
.app {
background: #fff;
padding: 1rem 2rem;
max-width: 900px;
margin: 2rem auto;
min-height: 80vh;
box-shadow: 0 1px 10px hsla(0, 0%, 0%, 0.1);
/* display the containers in a column */
display: flex;
flex-direction: column;
}
/* separate the direct children vertically */
.app > * {
margin: 1rem 0;
}
/* stretch the visualization to cover the available space, if any */
.visualization {
flex-grow: 1;
/* display the circle in a wrapping row */
display: flex;
flex-wrap: wrap;
}
/* style the div included for the data points as circles */
.visualization div {
width: 48px;
height: 48px;
margin: 0.5rem;
line-height: 48px;
border-radius: 50%;
text-align: center;
font-weight: 900;
}
/* center the controls */
.controls {
display: flex;
justify-content: center;
}
.controls button {
margin: 0 1rem;
padding: 0.5rem 1rem;
background: hsl(0, 0%, 20%);
color: hsl(0, 0%, 94%);
border: none;
border-radius: 5px;
box-shadow: 0 1px 5px hsla(0, 0%, 0%, 0.1);
font-family: inherit;
font-size: 1rem;
text-transform: uppercase;
letter-spacing: 0.1rem;
}
.controls button:active {
transform: translateY(2px);
}
/* display the selections in a wrapping row */
.selection {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
}
.selection div {
margin: 1rem;
border-radius: 20px;
padding: 1rem;
}
/* style the classes describing the different selection with a different background and border-style */
.selection--update {
background: hsl(240, 75%, 90%);
border: 2px solid currentColor;
}
.selection--enter {
background: hsl(120, 75%, 90%);
border: 2px dashed currentColor;
}
.selection--exit {
background: hsl(0, 75%, 90%);
border: 2px dotted currentColor;
}
// utility functions to generate a random data point
const randomInt = (max = 10) => Math.floor(Math.random() * max);
const randomLetter = () => String.fromCharCode(Math.floor(Math.random() * ('Z'.charCodeAt() - 'A'.charCodeAt()) + 'A'.charCodeAt()));
// specify a unique identifier to differentiate the data points
const randomDataPoint = () => ({
value: `${randomInt()}${randomLetter()}`,
id: Math.random(),
});
const dataPoints = 10;
// array describing the data
let data = Array(dataPoints).fill('').map(() => randomDataPoint());
// function visualizing the data points
function visualizeData(inputData) {
const visualization = d3
.select('.visualization');
// update selection, existing nodes
const updateSelection = visualization
.selectAll('div')
.data(inputData, d => d.id); // object constancy, bind each element according to the id of the connected data point
// enter selection, to be added
const enterSelection = updateSelection
.enter();
// exit selection, to be removed
const exitSelection = updateSelection
.exit();
// style the update selection with the matching class
updateSelection
.attr('class', 'selection--update');
// add a div for each new data point
enterSelection
.append('div')
.attr('class', 'selection--enter')
.text(d => d.value)
// transition the div from 0 to 1
.style('transform', 'scale(0)')
.transition()
.delay((d, i) => 200 + 50 * i)
.duration(500)
.style('transform', 'scale(1)');
// remove the div for each no-longer-existing data point
exitSelection
.attr('class', 'selection--exit')
// remove after the transition
// ! if two elements are removed in succession and before the transition is complete the exit selection will consider both
.transition()
.delay(200)
.duration(1000)
.style('transform', 'scale(0)')
.remove();
// complete the visualization updating the UI of the application
// include the data points in the prescribed span
d3
.select('.input p span')
.html(inputData.map(({ value }) => value));
// add the selections to their respective container
d3
.select('.selection .selection--enter p')
.text(enterSelection._groups[0].map(group => group.__data__.value));
d3
.select('.selection .selection--update p')
.text(updateSelection._groups[0].map(group => group.__data__.value));
d3
.select('.selection .selection--exit p')
.text(exitSelection._groups[0].map(group => group.__data__.value));
}
// immediately call the function to visualize the existing data
visualizeData(data);
// call the function following a click event on the .controls buttons, afte adding / removing a data point from the array
// ! add / remove a data point at a random index of the array
function addDataPoint() {
const randomIndex = randomInt(data.length);
data = [...data.slice(0, randomIndex), randomDataPoint(), ...data.slice(randomIndex)];
visualizeData(data);
}
// function removing a data point
function removeDataPoint() {
const randomIndex = randomInt(data.length);
data = [...data.slice(0, randomIndex), ...data.slice(randomIndex + 1)];
visualizeData(data);
}
// bind the function
d3
.select('.controls--add')
.on('click', addDataPoint);
d3
.select('.controls--remove')
.on('click', removeDataPoint);
Also see: Tab Triggers