Pen Settings

HTML

CSS

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

JavaScript

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

Behavior

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.

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.

HTML

            
              
            
          
!

CSS

            
              html { height: 100%; }
body {
  align-items: center;
  background-color: #1D1E22;
  display: flex;
  justify-content: center;
  height: 100%;
}
svg {
  background-color: #fff;
  margin: 20px;
  padding: 0 0 40px 20px;
}

.title {
  font-size: 24px;
}

.bar:hover {
  fill: #333 !important;
}

div.tooltip {
  background: rgba(112, 127, 140, .85);
  border: none;
  border-radius: 2px;
  color: #fff;
  font-size: 24px;
  padding: 5px;
  pointer-events: none;
  position: absolute;
  text-align: center;
  width: 200px;
}

#x-axis .tick line {
  display: none;
}
            
          
!

JS

            
              // store data in variable
const dataURI = 'https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/GDP-data.json'

// set up svg
const w = 1120
const h = 500
const padding = 60

const svg = d3.select('body')
              .append('svg')
              .attr('width', w)
              .attr('height', h)

// set up chart title
svg.append('text')
   .attr('transform', `translate(${w/2}, ${padding})`)
   .attr('class', 'title')
   .attr('id', 'title')
   .style('text-anchor', 'middle')
   .text('United State GDP')

// get data
d3.json(dataURI, {crossOrigin: "anonymous"}).then(data => {
  
  // set up function to handle date parsing
  const dateFormat = d3.timeParse('%Y-%m-%d')
  
  // map over data for GDP values
  const GDP = data.data.map(item => item[1])
  const dates = data.data.map(item => item[0])
  const years = dates.map(date => dateFormat(date).getFullYear())
  
  // set up x scale (used scaleBand for help determining geometry of each bar)
  const xScale = d3.scaleBand()
                   .domain(GDP)
                   .range([padding, w - padding])
                   .padding(0.1)
  
  // set up y scale with scaleLinear
  const yScale = d3.scaleLinear()
                   .domain([0, d3.max(GDP)])
                   .range([h - padding, padding])
  
  // set up color scale
  const colorScale = d3.scaleSequential(d3.interpolateCool)
                       .domain([d3.min(GDP), d3.max(GDP)])
  
  // append div for use as tooltip
  const tooltip = d3.select('body')
                .append('div')
                .attr('class', 'tooltip')
                .attr('id', 'tooltip')
                .style('opacity', 0)
  
  // set up bars
  svg.selectAll('rect')
     .data(GDP)
     .enter()
     .append('rect')
     .attr('x', d => xScale(d))
     .attr('y', d => yScale(d))
     .attr('width', xScale.bandwidth())
     .attr('height', d => h - padding - yScale(d))
     .attr('fill', '#162238')
     .attr('class', 'bar')
     .attr('data-gdp', d => d)
     .attr('data-date', (d, i) => dates[i])
     .style('fill', d => colorScale(d))
     // set up mouseover to modify tooltip div
     .on('mouseover', (d, i) => {
       tooltip.transition()
          .duration(200)
          .style('opacity', 0.9)
       tooltip.html(`$${d3.format(',')(d)} Billion<br>${moment(dateFormat(dates[i])).format('MMM YYYY')}`)
          .attr('data-date', dates[i])
          .style('left', (d3.event.pageX) + 15 + 'px')
          .style('top', (d3.event.pageY) + 'px')
     })
     // set up mouseout to modify tooltip div
     .on('mouseout', d => {
       tooltip.transition()
          .duration(200)
          .style('opacity', 0)
     })
  
  // set up x axis scale
  const xAxisScale = d3.scaleLinear()
                       .domain([d3.min(years), d3.max(years)])
                       .range([padding, w - padding])
  // set up x axis
  const xAxis = d3.axisBottom()
                  .scale(xAxisScale)
                  .tickFormat(d3.format('d'))
  
  // use call to create x axis
  svg.append('g')
     .attr('transform', `translate(0, ${h - padding})`)
     .attr('id', 'x-axis')
     .call(xAxis)
  
  svg.append('text')
   .attr('transform', `translate(${w/2}, ${h})`)
   .style('text-anchor', 'middle')
   .text('Years')
  
  // set up y axis
  const yAxis = d3.axisLeft(yScale)
  
  // us call to create y axis
  svg.append('g')
     .attr('transform', `translate(${padding}, 0)`)
     .attr('id', 'y-axis')
     .call(yAxis)
  
  // set up y axis label
  svg.append('text')
     .attr('transform', `translate(80, ${h/2})rotate(-90)`)
     .style('text-anchor', 'middle')
     .text('Gross Domestic Product')
})
            
          
!
999px

Console