Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ 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.


Auto Save

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.


                <!--Content -->
<section  id="main-content" class="container row">
      <div class="col-md-12">        
        <section class="interactive-chart">
<!--           corrected grammar to remove the extra "are" -->
            <figure class="card-skin centered" style="display: none">
    Data Options
    <select name="dataset">
<!--       Switched the order of the two datasets so that the balance transfer information shows upon pageload, due to the fact that 'Most organic traffic to this page is coming to find balance transfer information' -->
      <option value="transfer" data-json='
          "foo": {
            "name": "Credit Card One",
            "totals": {
              "Metric One": 50,
              "Metric Two": 10,
              "Metric Three": 24
          "bar": {
            "name": "Credit Card Two",
            "totals": {
              "Metric One": 0,
              "Metric Two": 15,
              "Metric Three": 20
          "buzz": {
            "name": "Credit Card Three",
            "totals": {
              "Metric One": 45,
              "Metric Two": 24,
              "Metric Three": 25
      '>Dataset 1</option>
<!--       Spelling error on line 115, need to change from "Anual" to "Annual"-->
      <option value="Rewards" data-json='
          "foo": {
            "name": "Credit Card One",
            "totals": {
              "Metric Four": 500,
              "Metric Two": 300,
              "Metric Three": 100,
              "Metric One": 45                        
          "bar": {
            "name": "Credit Card Two",
            "totals": {
              "Metric Four": 250,
              "Metric Two": 300,
              "Metric Three": 120,
              "Metric One": 0                        
          "buzz": {
            "name": "Credit Card Three",
            "totals": {
              "Metric Four": 120,
              "Metric Two": 225,
              "Metric Three": 100,
              "Metric One": 45                        
        Dataset 2
  <svg class="bar-chart" width="800"  ></svg>


                html, body {
  background-color: #efefef;
  padding: 0;
  margin: 0;
header {
  width: 100%;
  padding: 2rem 0rem;
  background-color: #07a;
  border-color: #07a;
  box-shadow: 0 0 2px -1px rgba(0, 0, 0, 0.12), 0 1px 1px rgba(0, 0, 0, 0.24);
.container {
  max-width: 1240px;
  margin-left: auto;
  margin-right: auto;
#instructions.container {
    background: #FFF8DC;
    border: solid .25rem #E7E2D0;
    border-radius: 0.3rem;
    margin-top: 2rem;
    padding: 2rem 3rem;
    font-size: 140%;
    line-height: 1.8;
    small {
      opacity: .6;
      text-transform: uppercase;
      margin: .25rem auto;
      display: block;
      font-weight: bold;
      border-bottom: solid 1px #ddd;
#main-content.container {
  background-color: #fff;
  box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.08);
  border: 1px solid #e8e8e8;
  border-radius: 0.3rem;
  margin-top: 2rem;
  min-height: 400px;
  padding: 2rem 3rem;

.price-pos {
  color: #007711;
.price-neg {
  color: #bb0000;

.bar text{
  using .card-skin
  using .centered
  using --codepen-gray


                const {
  keys, max, values, zip,
  scaleBand, scaleLinear,
  select, axisBottom, axisLeft, axisTop,
  format, schemeSet1,
    //Changed color scheme and thus eliminated schemeCategory10 and added schemeSet1
  // schemeCategory10,
} = d3

// helpers
// -------
const axis = (name, type='axis') => (
  a[name + type.charAt(0).toUpperCase() + type.slice(1)] ?`.${name}.${type}`) :
  chart.append('g').attr('class', `${name} ${type}`)

//Scale offset and padding
const scaleBarOffset = (scale, i) => scalePadding.outer(scale) + scale.step() * i
const scalePadding = {
  inner: scale => Math.round(scale.paddingInner() * scale.step()),
  outer: scale => Math.round(scale.paddingOuter() * scale.step())
const translate = (x, y) => (`translate(${x}, ${y})`)
// chart
// -----

//Specifics on schemeCategory10
// const barColors = schemeCategory10

//changed barColors to schemeSet1. Specifics on schemeSet1 
const barColors = schemeSet1
//define font size, height and width, and number of ticks for chart
// const font = 12, height = 330, ticks = 5, width = 400
//altered the height and width to accomodate orientation change to vertical
const font = 8, height = 400, ticks = 5, width = 330

//Define margin of chart elements
// const margin = {top: 20, right: 300, bottom: 20, left: 100, inner: 50}
//Altered margins to accomodate orientation change to vertical
const margin = {top: 20, right: 400, bottom: 20, left: 50, inner: 80}

//Assign margins to width and height of chart
let wrap = select('.bar-chart').attrs({
  width: width + margin.left + margin.right,
  height: height + + margin.bottom

//Assign class of 'chart-body' to svg 'g' grouping
let chart = wrap.append('g').attr('class', 'chart-body')
let en = {}, m = {}, a = {}

//Binding Data
const withData = (donations) => {
   //define the maximum values listed on the chart ticks, based on the maximum value listed in the array
  const donationMax = max(values(donations), d => max(values(d.totals)))
  const donationPeriods = keys(
  const donationTotals = zip(...values(donations).map(d => values(d.totals)))
  const donorNames = values(donations).map(d =>

  //Scales - axis have been inverted to accomodate the change to vertical orientation
  //const x = scaleLinear().domain([Math.round(1.1 * donationMax), 0]).range([ height,0]) // currently runs along the y axis
  const x = scaleLinear().domain([0, donationMax]).range([ height,0])
 // const y = scaleBand().domain(donationPeriods).rangeRound([0, width]).paddingInner(.2).paddingOuter(.2) // attempting to flip the range to have the bars start at the bottom of the chart
  const yscale =  scaleLinear().domain([0,  donationMax]).range([ height,0])
  const y =  scaleBand().domain(donationPeriods).rangeRound([0, width]).paddingInner(.2).paddingOuter(.2);
  const ySub = scaleBand().domain(donorNames).rangeRound([0, y.bandwidth()]).paddingInner(.25) // attempting to flip the range to have the bars start at the bottom of the chart
  //defines legend margin, offset from chart and legend text
  const legendLine = ySub.bandwidth(), legendMargin = scalePadding.inner(ySub)
  const legendOffset = {x: width + margin.inner, y: scalePadding.outer(y)}
  const legendTextX = legendOffset.x + legendLine + legendMargin
  //const barTextOffset = {x: 1.4 * ySub.bandwidth(), y: -.6 * (ySub.bandwidth() - font)}
   const barTextOffset = {x: 1.4 * ySub.bandwidth(), y: -.6 * (ySub.bandwidth() - font)}
  //create each axis and the contents of it's ticks
  //axis have been flipped to accomodate vertical orientation
  const t = d3.transition().ease(d3.easeQuadInOut).duration(!en.barGroup ? 0 : 400)
  a.yAxis = axis('y').transition(t).call(axisLeft().scale(x).ticks(ticks, '$s'))
  // K. Changed xGrid to yGrid (and all x values to y) to remove overlaying of x-axis labels
  a.yGrid = axis('y', 'grid').transition(t).call(axisLeft().scale(x).ticks(ticks).tickSize(-height).tickFormat(''))
  a.xAxis = axis('x').transition(t).call(axisBottom().scale(y).tickSize(0))
  //select chart including data of donation totals
  let barGroup = chart.selectAll('.bar-group').data(donationTotals)
  //Append the svg grouping with the class bar-group
  en.barGroup = barGroup.enter().append('g').attr('class', 'bar-group')
  //Define the opacity styling of the bars on the chart'opacity', 0).transition(t).style('opacity', 1)
  //merge the group together
  m.barGroup = en.barGroup.merge(barGroup)
  //exit transition
  barGroup.exit().transition(t).style('opacity', 0).remove()

  //Chart legend
  let legend = chart.selectAll('.legend').data(ySub.domain())
  en.legend = legend.enter().append('g').attr('class', 'legend')
    .call((s) => s.append('rect') && s.append('text'))
  m.legend = en.legend.merge(legend)
  let [legendSwatch, legendText] = ['rect'),'text').text(d => d)]

  let bar = m.barGroup.selectAll('.bar').data(d => d) = bar.enter().append('g').attr('class', 'bar')
    .call((s) => s.append('rect') && s.append('text')) =
  let [barRect, barText] = ['rect'),'text')]

  // position of the chart and legened on the page
  chart.attr('transform', translate(margin.left,
  a.xAxis.attr('transform', translate(0, height))
  m.barGroup.transition(t).attr('transform', (d, i) => translate(scaleBarOffset(y, i), 0)) // Translate(x,y) switch x and y translations'transform', (d, i) => translate(scaleBarOffset(ySub, i), 1)) // Need to reposition bars at bottom of chart. Remember, (0,0) is the top left of the chart!!
  m.legend.transition(t).attr('transform', (d, i) => translate(0, legendOffset.y + scaleBarOffset(ySub, i)))
  //barText.transition(t).attrs({dx: barTextOffset.y, dy: barTextOffset.x, y, x: ySub.bandwidth})
  barText.transition(t).attrs({dx: barTextOffset.y, dy: barTextOffset.x, y, x: ySub.bandwidth})
  legendSwatch.attr('x', legendOffset.x)
  legendText.transition(t).attrs({dy: barTextOffset.y, x: legendTextX, y: ySub.bandwidth})

  // creates bars on chart 
 // en.barGroup.attr('height', y.bandwidth)
  //////Switched height and width of chart bars (need to still solve inverted bar problem)
  barRect.transition(t).attrs({width: ySub.bandwidth})
 barRect.transition(t).attrs({height: function(d){
   return height-yscale(d)  ;
  barRect.transition(t).attrs({'y': function(d){return yscale(d)} }); 
  //Creates swatches on legend
  legendSwatch.transition(t).attrs({width: legendLine, height: legendLine})

  // styling of the bar colors'fill', (d, i) => barColors[i])
  //Specifies number of digits used in the text next to the chart bars
  barText.transition(t).attrs({'y': function(d){return yscale(d+(donationMax/10))} }); //FIXING THE Y POSITION OF THE BAR TEXT
  barText.transition(t).attrs({'x': 9});
  //barText.transition(t).attrs({'x':  function(d){return x(d)}});
  //barText.transition(t).attrs({'transform': 'rotate(-90)'});
  //Assigns the same colors to the swatches in the chart legend as the colors in the bar chart using barColors'fill', (d, i) => barColors[i])

// run
// ---

//select the dataset by name
let $select = $('[name=dataset]')
//parse the JSON
const data = ($option) => (JSON.parse($'json')))
//enable selection of dataset via dropdown
$select.on('change', () => withData(data($select.find('option:selected'))))
//first dataset listed in HTML is the first to appear to the user
//transition enabling a 500ms delay and fast fade in effect