Pen Settings



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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource


Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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


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 Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.


                <div id="wrapper"></div>  


                body {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 2em;
  letter-spacing: -0.011em;
  font-family: sans-serif;
  font-size: 16px;
  color: #34495e;
  background: #f8f9fa;

svg {
  fill: #34495e;

.axis path,
.axis line {
  stroke: #1347a6;

.axis text {
  fill: #1347a6;


                async function drawBars() {
  // 1: Access Data
  const dataset = await d3.json("");

  // 2: Create chart dimensions

  const width = 600;
  let dimensions = {
    height: width * 0.6,
    width: width,
    margin: {
      bottom: 50,
      left: 50,
      right: 10,
      top: 30

  dimensions.boundedHeight =
    dimensions.height - dimensions.margin.bottom -;
  dimensions.boundedWidth =
    dimensions.width - dimensions.margin.left - dimensions.margin.right;

  const drawHistogram = metric => {
    const metricAccessor = d => d[metric];
    const yAccessor = d => d.length;

    // 3: Draw canvas

    const wrapper = d3
      .attr("height", dimensions.height)
      .attr("width", dimensions.width)
      .style("margin-bottom", "3rem");

    const bounds = wrapper
        `translate(${dimensions.margin.left}px, ${}px)`

    // 4: Create scales

    const xScale = d3
      .domain(d3.extent(dataset, metricAccessor))
      .range([0, dimensions.boundedWidth])

    const binsGenerator = d3

    const bins = binsGenerator(dataset);

    const yScale = d3
      .domain([0, d3.max(bins, yAccessor)])
      .range([dimensions.boundedHeight, 0])

    // 5: Draw data

    const binsGroup = bounds.append("g");

    const binGroups = binsGroup

    const barPadding = 1;

    const barRects = binGroups
      .attr("x", d => xScale(d.x0) + barPadding / 2)
      .attr("y", d => yScale(yAccessor(d)))
      .attr("width", d => d3.max([0, xScale(d.x1) - xScale(d.x0) - barPadding]))
      .attr("height", d => dimensions.boundedHeight - yScale(yAccessor(d)))
      .attr("fill", "cornflowerblue");

    // 6: Draw peripherals

    const barText = binGroups
      .attr("x", d => xScale(d.x0) + (xScale(d.x1) - xScale(d.x0)) / 2)
      .attr("y", d => yScale(yAccessor(d)) - 5)
      .style("text-anchor", "middle")
      .attr("fill", "darkgrey")
      .attr("font-size", "12px");

    const mean = d3.mean(dataset, metricAccessor);

    const line = bounds
      .attr("x1", xScale(mean))
      .attr("y1", -15)
      .attr("x2", xScale(mean))
      .attr("y2", dimensions.boundedHeight)
      .attr("stroke", "maroon")
      .attr("stroke-dasharray", "2px 4px");

    const lineText = bounds
      .attr("x", xScale(mean))
      .attr("y", -20)
      .attr("fill", "maroon")
      .attr("font-size", "12px")
      .style("text-anchor", "middle");

    const xAxisGenerator = d3.axisBottom().scale(xScale);

    const xAxis = bounds
      .style("transform", `translateY(${dimensions.boundedHeight}px)`)
      .attr("class", "axis");

    const xAxisLabel = xAxis
      .attr("x", dimensions.boundedWidth / 2)
      .attr("y", dimensions.margin.bottom - 10)
      .attr("fill", "#1347a6")
      .style("font-size", "1.4em")
      .style("text-transform", "capitalize");

  // 7: Set up interactions
  // Not yet

  // Automating 3 through 6

  const metrics = [