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

+ add another resource

JavaScript

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

Packages

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.

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.

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

              
                <script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>

<div id="container">
  <div id="tooltip"></div>
  <svg>
    <text id="title"></text>
  </svg>
  <div id="details">
    <details>
      <summary>Details</summary>
    </details>
  </div>
</div> 
              
            
!

CSS

              
                html, body {
  
  --body : #9a8c98;
  --rect : #c9ada7;
  --text: #22223b;
  --svg: #f2e9e4;
  --tooltip : #4a4e69;  
 
  height : 100%;
  width : 100%;
  margin : 0;
  padding : 0;
  background-color : var(--body, black);
  display: flex;
  justify-content: center;
  align-items: center;
}

#container { 
  background-color : var(--body, black);
  width: 100%;
  height: 100%;
  justify-content: center;
  align-items: center;
  flex-direction : column;
  display: flex;
}
.bar:hover {
  fill : var(--text,  black);
}
text {
  color : var(--text, black);  
  font-size: 12px;
  font-weight: 600
} 
text > a {
 fill : var(--text, black);
 text-decoration: underline;
}
#details {
  width : 850px;
  height: 75px;  
  position : relative;
  margin-top: 15px;  
}
details, summary {
  color : var(--text, black);
  text-align: right;
}
svg {
  background-color: var(--svg, beige);     
  display: block;
  position : relative;
  border : 5px var(--text, black) solid;
  border-radius: 10px;
  padding: 20px;
}
rect {
  fill : var(--rect, darkGray)
}
#tooltip {
  position : absolute;
  display : flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  background-color: var(--svg, beige);
  color: var(--text, black);
  border-radius: 5px;
  border: 1px var(--tooltip, darkGray) solid;
  width: 100px;
  height: 80px;
  opacity : 0;
  z-index : 3;
}

 
              
            
!

JS

              
                document.addEventListener('DOMContentLoaded', function(){

  const req = new XMLHttpRequest();
  req.open('GET', 'https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/GDP-data.json', true);
  req.send();
  req.onload = function(){
    const json = JSON.parse(req.responseText);
    console.log(json)
    const w = 800
    const h = 450
    const padding = 70
    const jsonLength = json['data'].length
    chartInfo(json.name, json.display_url, w, h, padding)
    barchart(json['data'], w, h, padding);       
    $(document).ready(function() {
      $("details").text(json["description"])
    })
  };

 const chartInfo = (name, url, w, h, p) => {
   
   const svg = d3.select('svg')
   
  d3.select("#title")
     .text(name)
     .attr("y", 80)
     .attr("x", w / 2)
     .attr("text-anchor", "middle")
     .attr("id", "title")   
     .style("font-size", 20+"px")
     .style("font-weight", "bold")
     .style("fill", "#22223b")
   
 d3.select('svg')
    .append('text')
    .html(`<a href="${url}">${url}</a>`)
    .attr("y", h-3)
    .attr("x", w- p )
    .attr("text-anchor", "end")
    .style("font-size", 12+"px")
    .style("fill", "#22223b")
  }
  
  const barchart = (data , w, h, padding) => {
 
    const years = data.map( (item) => item[0].split('-')[0] )
    const svg = d3
      .select('svg')
      .attr("width", w)
      .attr("height", h)
 
    const maxGDP = d3.max(data, (d) => d[1]);
    const minDate = d3.min(data, (d) => d[0])
    const maxDate = d3.max(data, (d) => d[0])  

    const yScale = d3.scaleLinear()
      .domain([0, maxGDP] )
      .range([h-padding, padding])
    
    const xScale = d3.scaleTime()
      // https://github.com/d3/d3-scale#time-scales
      .domain([new Date(minDate), new Date(maxDate) ])
      .range([padding, w-padding])
  
    const xAxis = d3.axisBottom(xScale)
    const yAxis = d3.axisLeft(yScale)

    const dataToScale = data.map( (d) => {
      return [d[0], d[1], (d[1] / maxGDP * (h - padding * 2))]
    })  
    
    svg.append('g')
      // x is correct, een beetje naar boven schuiven, dus de hoogte is 1000, 
      // naar boven is 1000 min padding
      .attr("transform", `translate(0, ${h - padding})`)
      .attr("id", "x-axis")

      .call(xAxis)
    
    svg.append('g')
      // y is correct, een beetje naar rechts schuiven, dus padding erbij doen
      .attr("transform", `translate(${padding}, 0)`)
      .attr("id", "y-axis")
      .call(yAxis)

    const tooltip = d3
      .select("#tooltip")
      .style("top", (h/2) + 250 +"px" )
   
    svg
      .selectAll('rect')
      .data(dataToScale)
      .enter()    
      .append('rect')
      .attr("data-date", (d) => d[0])
      .attr("data-gdp", (d) => d[1])
      .attr("class", 'bar')
      .attr("x", (d) => xScale(new Date(d[0])))
      .attr("y", (d) => (h - padding) - d[2])
      .attr("height", (d) => d[2])  
      .attr("width", (w - padding * 2) / data.length)
      .on("mouseenter", (d, i) => {
        const date = d.target.dataset.date;
        const gdp = d.target.dataset.gdp;
        const xPos = d.offsetLeft;
        tooltip
          .style("opacity", 0.9) 
          .style("left", xPos - w  + "px")
          .attr("data-date", date)
          .attr("data-gdp", gdp)
          .html(date + "<br>" + gdp)     
      })
      .on("mouseleave", (d, i) => {
        tooltip
          .style("opacity", 0)
      })
  } 
  
})


              
            
!
999px

Console