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.

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

              
                

<html lang="en">

<head>
  
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

 

  <link rel="stylesheet" href="style.css">

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

  <script src="https://d3js.org/d3-axis.v1.min.js"></script>
  <script src="https://d3js.org/d3.v5.min.js"></script> 
  <script src="https://cdnjs.cloudflare.com/ajax/libs/d3-legend/2.25.6/d3-legend.js"></script>
  <script src="graph.js"></script>
</head>


              
            
!

CSS

              
                

  .dot:hover {
    fill: lightslategray;
  }
  .dot{box-shadow: 1px 2px 1px 10px black;}
  div.tooltip {	
    position: absolute;			
    text-align: center;			
    width: 200px;					
    height: 50px;					
    padding: 2px;				
    font: 12px sans-serif;		
    background: rgb(143, 143, 143);	
    border: 0px;		
    border-radius: 8px;			
    pointer-events: none;	
    line-height: 150%	;
    color: black;
    box-shadow: 0px 0px 25px 5px rgba(241, 239, 239, 0.75);
  
}
#title,
#leftlegend,
#description{
  text-shadow: 1px 2px 3px black;
}
#leftlegend {color: midnightblue ;font-size: 22px;text-anchor:'middle';
}
.legend{font-size: 15px;

}
#title{
  text-anchor: middle;
  font-size: 25px;
}
#description{
  text-anchor: middle;
  font-size: 15px;
}
              
            
!

JS

              
                var dataUrl = " https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/global-temperature.json";
var xhttp = new XMLHttpRequest();
var data = [];
var dataYearsArr = [];
var dataValuesArr = [];
var parseDateY = d3.timeParse("%Y");
var parseDateM = d3.timeParse("%m");
var parseDateMD = d3.timeParse("%m/%d");
var formatDateY = d3.timeFormat("%Y");
var formatDateM = d3.timeFormat("%m");
var parseTime = d3.timeParse("%M:%S");
var formatTime = d3.timeFormat("%M:%S");
var monthFullName = d3.timeFormat("%B");
var middleT;
xhttp.onreadystatechange = function () {
    if (this.readyState == 4 && this.status == 200) {
        var json = JSON.parse(this.responseText);


        json.monthlyVariance.map((x) => data.push(x));
        middleT = json.baseTemperature;
        data.map((d)=> {

            d.year = d3.timeMonth.offset( parseDateY(d.year), d.month-1);
            dataYearsArr.push(d.year);
            d.month = parseDateM(d.month);
         
            dataValuesArr.push(+middleT + d.variance);
        });
      
        var padding = 100;

        var width = window.innerWidth - padding;
        var height = window.innerHeight / 5 * 3;
        const svg = d3.select("body")
            .append("svg").attr("width", width)
            .attr("height", height)
            .style("border", "1px dotted black");
        // color/legend//
        var minT = d3.min(dataValuesArr);
        var maxT = d3.max(dataValuesArr);
        var color = d3.scaleLinear()
            .domain([minT, (minT / 2 + maxT / 2), maxT])
            .range(["blue", "yellow", "red"]);

        svg.append("g")
            .attr("class", "legendLinear").attr("id", "legend")
            .attr("transform", "translate(" + (100) + "," + (height - padding / 2) + ")");

        var legendLinear = d3.legendColor()
            .shapeWidth(30)
            .cells(10)
            .orient('horizontal')
            .scale(color);

        svg.select(".legendLinear")
            .call(legendLinear);



        //y;
        var dataMonts=[];
for(var x= 0; x<=12;x++)dataMonts.push(parseDateMD(x+"/"+15));




var scaley = d3.scaleTime()
      .domain([d3.min(dataMonts), d3.max(dataMonts)])
      .range([padding,height - padding]);
        var axisy = d3.axisLeft(scaley)
     .tickFormat(d3.timeFormat("%B")).tickSizeOuter(0) 
       ;

        svg
     
            .append("g")
            .attr("transform", "translate(" + padding + "," + 0 + ")")
            .attr("id", "y-axis")
            .attr("class", "tick")
                      .call(axisy);

        

        //x
        var minusyear = d3.timeYear.offset(d3.min(dataYearsArr), -1);
       // var extentx = d3.extent()
       var u=(formatDateY(minusyear));
        var scalex = d3.scaleLinear()
            .domain([(formatDateY(minusyear)), formatDateY(d3.max(dataYearsArr))])
            .range([padding, width - padding]);


        var axisx = d3.axisBottom(scalex).tickSizeOuter(0).tickFormat(d3.format(" ")) ;


        svg

            .append("g")
            .attr("id", "x-axis")
            .attr("class", "tick")
            .attr("transform", "translate("+ (0)+"," + (height - padding) + ")")
            .call(axisx).selectAll("text")
            .style("text-anchor", "end")
            .attr("dx", "-.8em")
            .attr("dy", ".15em")
            .attr("transform", "rotate(-35)");

           

        //tooltip declarations
        var div = d3.select("body").append("div")
            .attr("class", "tooltip")
            .attr("id", "tooltip")
            .style("opacity", 0);
        //rectangles

        svg.selectAll("g.rect")
            .data(data)
            .enter()
            .append("rect")
            .attr("height", (height/12/2))
            .attr("width", "5")
            .attr("transform", "translate(0,-"+(height/12/4)+" )")
            .attr("x", (d) =>
                scalex(formatDateY(d.year)))
            .attr("y", (d) =>
                scaley (d.month))
            .attr("fill", (d) => color(d.variance + middleT))
            .attr("class", "cell")
            .attr("data-year", (d) => formatDateY(d.year))
            .attr("data-month", (d) => formatDateM(d.month)-1)
            .attr("data-temp", (d) => (d.variance+ middleT))

            .on("mouseover", function (d) {
                div.transition()
                    .duration(200)
                    .style("opacity", 0.7);
                div.attr("data-year",formatDateY(d.year));
                div.html(formatDateY(d.year) + "\t/ " + monthFullName(d.month) + 
                      "<br/>" + (d.variance + middleT).toFixed(2)+
                      "<br/>" + d.variance.toFixed(2))
                    .style("left", (d3.event.pageX) + "px")
                    .style("top", (d3.event.pageY - 28) + "px");
            }).on("mouseout", function (d) {
                div.transition()
                    .duration(500)
                    .style("opacity", 0);
            });
        svg.append('text')
            .attr("class", "leftlegend")
            .attr("id", "leftlegend")
            .attr("transform", "translate(25," + (height / 2) + ") rotate(270)")

            .text("Months");

//titles

        svg.append('text')
            .attr("id", "title")
            .attr("transform", "translate(" + (width / 2) + "," + padding / 2 + ")")
            .text("Monthly Global Land-Surface Temperature");
        svg.append('text')
            .attr("id", "description")
            .attr("transform", "translate(" + (width / 2) + "," + (padding / 2 + 22) + ")")
            .text("1753 - 2015: base temperature " + middleT + "℃");
       


    }
};
xhttp.open("GET", dataUrl, true);
xhttp.send();
              
            
!
999px

Console