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

              
            
!

CSS

              
                *{
  padding:0;
  margin:0;
  box-sizing:border-box;
}

body{
  display:flex;
  flex-direction:column;
  align-items:center;
  justify-content:center;
}

#map{
  margin:1em 0;
}

#title{
  font-size:2rem;
  line-height:2.5rem;
}

#description{
  font-size:1rem;
  line-height:1.5rem;
}

              
            
!

JS

              
                //bring the data in
d3.json("https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/global-temperature.json",json=>{
  const baseTemp = json["baseTemperature"];
  const dataset = json["monthlyVariance"];

  //add title and description
  d3.select("body")
    .append("h1")
    .text("Monthly Global Land-Surface Temperature")
    .attr("id","title");
  d3.select("body")
    .append("p")
    .text(`1753 - 2015: base temperature ${baseTemp} ℃`)
    .attr("id","description");

  //create a div with svg inside it
  const height = 500;
  const width = 800;
  const padding = {topBottom: 50, leftRight: 100};
  const div = d3.select("body")
  .append("div")
  .attr("id","map");
  const svg = div.append("svg")
  .attr("height",height)
  .attr("width",width);

  //x-axis=year and y-axis=months
  const parsedYear = dataset.map(d=>d3.timeParse("%Y")(d["year"]));
  const parsedMonth = dataset.map(d=>d3.timeParse("%m")(d["month"]));
  const tempDiff = dataset.map(d=>baseTemp+d["variance"]);

  //defines the dynamic scale
  const yScale = d3.scaleTime()
  .domain(d3.extent(parsedMonth).reverse())
  .range([height - padding.topBottom, padding.topBottom]);
  const xScale = d3.scaleTime()
  .domain(d3.extent(parsedYear))
  .range([padding.leftRight,width-padding.leftRight]);

  //colour scale
  const cScale = d3.scaleLinear()
  .range(["#ca0020","#f4a582","#92c5de","#0571b0"])
  .domain([d3.extent(tempDiff)]);

  //append the axes
  const yAxis = d3.axisLeft(yScale)
  .tickFormat((d)=>d3.timeFormat("%B")(d));
  svg.append("g")
    .attr("id","y-axis")
    .attr("transform","translate("+ padding.leftRight+",0)")
    .call(yAxis);
  const xAxis = d3.axisBottom(xScale)
  .tickFormat((d)=>d3.timeFormat("%Y")(d));
  svg.append("g")
    .attr("id","x-axis")
    .attr("transform","translate( 0,"+ (height-padding.topBottom) +")")
    .call(xAxis);

  //append the rects
  svg.selectAll("rect")
    .data(dataset)
    .enter()
    .append("rect")
    .attr("class","cell")
    .attr("x",(d,i)=>xScale(parsedYear[i]))
    .attr("y",(d,i)=>yScale(parsedMonth[i]))
    .attr("width",(width-padding.leftRight)/dataset.length)
    .attr("height",(height-padding.topBottom)/12)
    .style("fill",(d,i)=>cScale(tempDiff[i]))
    .attr("data-month",(d,i)=>d["month"])
    .attr("data-year",(d,i)=>d["year"])
    .attr("data-temp",(d,i)=>d["variance"])
});


              
            
!
999px

Console