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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource


Babel includes JSX processing.

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


Add Packages

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.


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="chart"></div>


                #chart {
  background: lightgray;
  border: 1px solid black;

rect {
  fill: steelblue;


                const margin = { top: 10, right: 20, bottom: 30, left: 30 };

// the exact dimensions of 400 x 400
// will only be used for the initial render
// but the width to height proportion 
// will be preserved as the chart is resized
const width = 400 - margin.left - margin.right;
const height = 400 - - margin.bottom;

const data = [5, 15, 25, 35, 45, 55, 65, 76, 85, 95]

const xScale = d3.scaleBand()
  .range([0, width]);

const yScale = d3.scaleLinear()
  .domain([0, 100])
  .range([height, 0]);

const svg ='#chart')
    .attr('width', width + margin.left + margin.right)
    .attr('height', height + + margin.bottom)
    .call(responsivefy) // this is all it takes to make the chart responsive
    .attr('transform', `translate(${margin.left}, ${})`);

    .attr('x', d => xScale(d))
    .attr('y', d => yScale(d))
    .attr('width', d => xScale.bandwidth())
    .attr('height', d => height - yScale(d))


  .attr('transform', `translate(0, ${height})`)

function responsivefy(svg) {
  // container will be the DOM element the svg is appended to
  // we then measure the container and find its aspect ratio
  const container =,
      width = parseInt('width'), 10),
      height = parseInt('height'), 10),
      aspect = width / height;

  // add viewBox attribute and set its value to the initial size
  // add preserveAspectRatio attribute to specify how to scale
  // and call resize so that svg resizes on inital page load
  svg.attr('viewBox', `0 0 ${width} ${height}`)
      .attr('preserveAspectRatio', 'xMinYMid')

  // add a listener so the chart will be resized when the window resizes
  // to register multiple listeners for same event type,
  // you need to add namespace, i.e., ''
  // necessary if you invoke this function for multiple svgs
  // api docs:'resize.' + container.attr('id'), resize);

  // this is the code that actually resizes the chart
  // and will be called on load and in response to window resize
  // gets the width of the container and proportionally resizes the svg to fit
  function resize() {
      const targetWidth = parseInt('width'));
      svg.attr('width', targetWidth);
      svg.attr('height', Math.round(targetWidth / aspect));