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>  


                .bin rect {
    fill: cornflowerblue;
.bin text {
    text-anchor: middle;
    fill: darkgrey;
    font-size: 12px;
    font-family: sans-serif;

.mean {
    stroke: maroon;
    stroke-dasharray: 2px 4px;

.x-axis-label {
    fill: black;
    font-size: 1.4em;
    text-transform: capitalize;

body {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2em;

button {
    font-size: 1.2em;
    margin-left: 1em;
    padding: 0.5em 1em;
    appearance: none;
    -webkit-appearance: none;
    color: white;
    border: none;
    box-shadow: 0 5px 0 0 seagreen;
    border-radius: 6px;
    font-weight: 600;
    outline: none;
    cursor: pointer;
    transition: all 0.1s ease-out;

button:focus {
    background: #73b173;
    box-shadow: 0 4px 0 0 seagreen;
    transform: translateY(1px);

button:active {
    box-shadow: 0 1px 0 0 seagreen;
    transform: translateY(4px);


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

  // 2. Create chart dimensions

  const width = 500;
  let dimensions = {
    width: width,
    height: width * 0.6,
    margin: {
      top: 30,
      right: 10,
      bottom: 50,
      left: 50
  dimensions.boundedWidth =
    dimensions.width - dimensions.margin.left - dimensions.margin.right;
  dimensions.boundedHeight =
    dimensions.height - - dimensions.margin.bottom;

  // 3. Draw canvas

  const wrapper = d3
    .attr("width", dimensions.width)
    .attr("height", dimensions.height);

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

  // init static elements
  bounds.append("g").attr("class", "bins");
  bounds.append("line").attr("class", "mean");
    .attr("class", "x-axis")
    .style("transform", `translateY(${dimensions.boundedHeight}px)`)
    .attr("class", "x-axis-label")
    .attr("x", dimensions.boundedWidth / 2)
    .attr("y", dimensions.margin.bottom - 10);

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

    // 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 barPadding = 1;

    const exitTransition = d3.transition().duration(600);

    const updateTransition = exitTransition

    let binGroups = bounds

    const oldBinGroups = binGroups.exit();

      .style("fill", "red")
      .attr("y", dimensions.boundedHeight)
      .attr("height", 0);

      .attr("y", dimensions.boundedHeight);


    const newBinGroups = binGroups
      .attr("class", "bin");

      .attr("height", 0)
      .attr("x", d => xScale(d.x0) + barPadding)
      .attr("y", dimensions.boundedHeight)
      .attr("width", d => d3.max([0, xScale(d.x1) - xScale(d.x0) - barPadding]))
      .style("fill", "yellowgreen");

      .attr("x", d => xScale(d.x0) + (xScale(d.x1) - xScale(d.x0)) / 2)
      .attr("y", dimensions.boundedHeight);

    // update binGroups to include new points
    binGroups = newBinGroups.merge(binGroups);

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

    const barText = binGroups
      .attr("x", d => xScale(d.x0) + (xScale(d.x1) - xScale(d.x0)) / 2)
      .attr("y", d => yScale(yAccessor(d)) - 5)
      .text(d => yAccessor(d) || "");

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

    const meanLine = bounds
      .attr("x1", xScale(mean))
      .attr("x2", xScale(mean))
      .attr("y1", -20)
      .attr("y2", dimensions.boundedHeight);

    // 6. Draw peripherals

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

    const xAxis = bounds

    const xAxisLabel =".x-axis-label").text(metric);

  const metrics = [
  let selectedMetricIndex = 0;

  const button = d3
    .text("Change metric");

  button.node().addEventListener("click", onClick);
  function onClick() {
    selectedMetricIndex = (selectedMetricIndex + 1) % (metrics.length - 1);