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.


                <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("");

  const metricAccessor = d => d.humidity;
  const yAccessor = d => d.length;

  // 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;

  // 3: Draw canvas

  const wrapper = d3
    .attr("height", dimensions.height)
    .attr("width", dimensions.width)
    .attr("role", "figure")
    .attr("tabindex", "0");

    .text("Histogram looking at the distribution of humidity in 2016");

  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
    .attr("tabindex", "0")
    .attr("role", "list")
    .attr("aria-label", "histogram bars");

  const binGroups = binsGroup
    .attr("tabindex", "0")
    .attr("role", "listitem")
      d =>
        `There were ${yAccessor(d)} days between ${d.x0
          .slice(0, 4)} and ${d.x1.toString().slice(0, 4)} humidity levels`

  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")

    .attr("role", "presentation")
    .attr("aria-hidden", "true");

  // 7: Set up interactions