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

              
                <h1 id="title">United States Educational Attainment</h1>
<h2 id="description">Percentage of adults age 25 and older with a bachelor's degree or higher (2010-2014)</h2>
<div id="chart"></div>
              
            
!

CSS

              
                svg {
  background-color: #F5E1E1;
}
body {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
#tooltip {
  display: flex;
  align-items: center;
  justify-content: center;
  position: absolute;
  width: 150px;
  overflow: auto;
  padding: 10px;
  font: 12px;
  background-color: white;
  box-shadow: 1px 1px 10px;
  border-radius: 20px;
  pointer-events: none;
}

              
            
!

JS

              
                //Set important variables and functions to simplify code later on
const countyURL = "https://cdn.freecodecamp.org/testable-projects-fcc/data/choropleth_map/counties.json";
const educationURL = "https://cdn.freecodecamp.org/testable-projects-fcc/data/choropleth_map/for_user_education.json";

let countyData
let educationData

//Retrieve data for chart
d3.json(countyURL).then(
  (data,error) => {
    if(error) {
      //console.log(log)     
    } else {
      countyData = topojson.feature(data, data.objects.counties).features
      //console.log(countyData)

      d3.json(educationURL).then(
        (data,error) => {
          if(error) {
            //console.log(log)     
          } else { 
            educationData = data;
            //console.log(educationData);


            //Set svg (where graph will be) dimensions
            const w = 1000;
            const h = 750;
            const padding = 60;


            //Create svg area (where graph will be)
            const svg = d3.select("#chart")
            .append("svg")
            .attr("width", w)
            .attr("height", h);


            //Draw paths on svg area
            svg.selectAll("path")
              .data(countyData)
              .enter()
              .append("path")
              .attr("d", d3.geoPath())
              .attr("class","county")
              .attr("fill", (countyDataItem) => {
              let matchedEducationDataItem = educationData.find((educationDataItem) => { return educationDataItem["fips"] === countyDataItem["id"]});
              let percentageBorHigher = matchedEducationDataItem["bachelorsOrHigher"];
              switch(true) {
                case (percentageBorHigher <= 15):
                  return "red"
                  break;
                case (percentageBorHigher <= 30):
                  return "orange"
                  break;
                case (percentageBorHigher <= 45):
                  return "yellow"
                  break;
                case (percentageBorHigher > 45):
                  return "green"
                  break;
              }
            }
                   )
              .attr("data-fips", (countyDataItem) => countyDataItem["id"])
              .attr("data-education", (countyDataItem) => {
              let matchedEducationDataItem = educationData.find((educationDataItem) => { 
                return educationDataItem["fips"] === countyDataItem["id"]
              })
              let percentageBorHigher = matchedEducationDataItem["bachelorsOrHigher"]
              return percentageBorHigher
            })
              .on("mouseover", handleMouseOver)
              .on("mouseout", handleMouseOut)


            //Create tooltip   
            const tooltip = d3.select('#chart')
            .append('div')
            .attr('id', 'tooltip')
            .style('opacity', 0);

            
            // Create Event Handlers for mouse
            function handleMouseOver(d,countyDataItem) {  
              let matchedEducationDataItem = educationData.find((educationDataItem) => { 
                return educationDataItem["fips"] === countyDataItem["id"]
              })
              let percentageBorHigher = matchedEducationDataItem["bachelorsOrHigher"]

              tooltip
                .style("opacity", 0.9)
                .attr("data-education", (d,i) => {
                console.log(countyDataItem)
                let matchedEducationDataItem = educationData.find((educationDataItem) => { 
                  return educationDataItem["fips"] === countyDataItem["id"]
                })
                let percentageBorHigher = matchedEducationDataItem["bachelorsOrHigher"]
                return percentageBorHigher
              })
                .html(matchedEducationDataItem["area_name"] + '<br>' + matchedEducationDataItem["bachelorsOrHigher"] + '%')
                .style('left', d.pageX + 10 + 'px')
                .style('top', d.pageY - 28 + 'px');
              d3.select(this)
                .attr("fill", "white");
            }

            function handleMouseOut(d, countyDataItem) {  
              tooltip.style("opacity", 0);
              d3.select(this)
                .attr("fill", (countyDataItem) => {
                let matchedEducationDataItem = educationData.find((educationDataItem) => { 
                  return educationDataItem["fips"] === countyDataItem["id"]});
                let percentageBorHigher = matchedEducationDataItem["bachelorsOrHigher"];
                switch(true) {
                  case (percentageBorHigher <= 15):
                    return "red"
                    break;
                  case (percentageBorHigher <= 30):
                    return "orange"
                    break;
                  case (percentageBorHigher <= 45):
                    return "yellow"
                    break;
                  case (percentageBorHigher > 45):
                    return "green"
                    break;
                }
              }
                     )
            }


            // Create legend
            // Handmade legend
            const legend = svg.append("g")
            .attr("id", "legend")

            legend.append("rect")
              .attr("x",(w - padding)*0.6)
              .attr("y",h - padding*2)
              .attr("width", 20)
              .attr("height", 20)
              .style("fill", "red")

            legend.append("rect")
              .attr("x",(w - padding)*0.6)
              .attr("y",h - padding*2 + 20)
              .attr("width", 20)
              .attr("height", 20)
              .style("fill", "orange")

            legend.append("rect")
              .attr("x",(w - padding)*0.6)
              .attr("y",h - padding*2 + 40)
              .attr("width", 20)
              .attr("height", 20)
              .style("fill", "yellow")

            legend.append("rect")
              .attr("x",(w - padding)*0.6)
              .attr("y",h - padding*2 + 60)
              .attr("width", 20)
              .attr("height", 20)
              .style("fill", "green")

            legend.append("text")
              .attr("x",(w - padding)*0.6 + 25)
              .attr("y",h - padding*2 + 13)
              .text("% <= 15%")
              .style("font-size", "10px")
              .attr("alignment-baseline","middle")

            legend.append("text")
              .attr("x",(w - padding)*0.6 +25)
              .attr("y",h - padding*2 + 33)
              .text("15% < % <= 30%")
              .style("font-size", "10px")
              .attr("alignment-baseline","middle")

            legend.append("text")
              .attr("x",(w - padding)*0.6 + 25)
              .attr("y",h - padding*2 + 53)
              .text("30% < % <= 45%")
              .style("font-size", "10px")
              .attr("alignment-baseline","middle")


            legend.append("text")
              .attr("x",(w - padding)*0.6 + 25)
              .attr("y",h - padding*2 + 73)
              .text("45% < %")
              .style("font-size", "10px")
              .attr("alignment-baseline","middle")
            













          }
        }
      )
    }
  }
);
              
            
!
999px

Console