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>

<script src="https://d3js.org/topojson.v2.min.js"></script>

  <h1 id="title">United States Educational Attainment</h1>
    <h3 id="description">Percentage of adults aged 25+ with a bachelor's degree or higher (2010-2014)</h3>
    <div id="svg-container"></div>
              
            
!

CSS

              
                 body {
    font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif
  }

#svg-container {
  padding-left: 50px;
}

  h1, h3{
    text-align: center;
  }
  
  #tooltip {
    font-size: 1em;
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    text-align: center;
    width: 150px;
    height: 100px;
    padding: 2px;
    background:rgb(251, 251, 252);
    box-shadow: 0px 0px 6px 2px #A9A5AA;
    border-radius: 2px;
    }

  .county-border {
    fill: none;
    stroke: rgba(23, 10, 58, 0.432);
    stroke-width: 0.5px;
    stroke-linejoin: round;
    stroke-linecap: round;
    pointer-events: none;
    }
  .counties :hover {
    fill: black;
  }
              
            
!

JS

              
                // set up svg area
    const width = 1200;
    const height = 600;
    
    const svgContainer = d3.select('#svg-container')
      .append("svg")
      .attr('width', width )
      .attr('height', height);

    const path = d3.geoPath();

    // tooltip
    let tooltip = d3.select("#svg-container")
        .append("div")
        .style("opacity", 0) 
        .attr("class", "tooltip")
        .attr("id", "tooltip");
                                
      // when mouse is moved away from bar, tooltip is hidden.
      let mouseleave = function(d) {
        tooltip
        .transition()
        .duration(100)
        .style("opacity", 0)
        }

    Promise.all([
      d3.json("https://raw.githubusercontent.com/no-stack-dub-sack/testable-projects-fcc/master/src/data/choropleth_map/for_user_education.json"),
      d3.json("https://raw.githubusercontent.com/no-stack-dub-sack/testable-projects-fcc/master/src/data/choropleth_map/counties.json")
    ]).then(([educationData, mapData])=> {
  
      let counties = topojson.feature(mapData, mapData.objects.counties).features;

      counties.sort(function(a, b){
        return a.id - b.id;
      })

      // assign objects from ed info json to US counties.properties object
      for (let i = 0; i < counties.length; i++) {
        if (educationData.find(item => item.fips == counties[i].id )) {
          Object.assign(counties[i].properties, educationData[i]);
        }
      }

      // legend
      const colors = d3.scaleThreshold()
        .range(d3.schemePurples[9])
        .domain([0, 10, 20, 30, 40, 50, 60, 70, 80]);

      const colorX = d3.scaleLinear()
        .domain([0, 10, 20, 30, 40, 50, 60, 70, 80])
        .range([0, 40]);

      const legendAxis = d3.axisBottom(colorX)
        .tickValues(colors.domain())
        .tickFormat(d3.format(10));
      
      const legend = svgContainer.append("g")
        .call(legendAxis)
        .attr("id", "legend")
        .attr("transform", "translate(500, 20)");

      legend.selectAll("rect")
        .data(colors.range().map(function (d) {
          d = colors.invertExtent(d);
          if (d[0] == null) d[0] = colorX.domain()[0];
          if (d[1] == null) d[1] = colorX.domain()[1];
          return d;
        }))
        .enter()
        .append('rect')
        .attr("height", 8)
        .attr("x", function(d) { return colorX(d[0]); })
        .attr("width", function(d) { return colorX(d[1]) - colorX(d[0]); })
        .attr("fill", function(d) { return colors(d[0]); });

        // Draw map
        svgContainer.append("g")
          .selectAll("path")
          .data(counties)
          .enter()
          .append("path")
          .attr("d", path)
          .attr("class", "county")
          .attr("data-fips", d => d.properties.fips)
          .attr("data-education", d => d.properties.bachelorsOrHigher)
          .attr("fill", d => colors(d.properties.bachelorsOrHigher))
          // MOUSEOVER
          .on("mouseover", function(d, i){
          tooltip.transition()
            .duration(200)
            .style('opacity', 0.9);
          tooltip.html(d.properties.area_name + ", State: " + d.properties.state + "<br>" 
          + "Bachelors degree or higher: " + d.properties.bachelorsOrHigher + "%")
            .style("left", (d3.event.pageX+ 10) + "px")
            .style("top", (d3.event.pageY + 10) + "px")
            .attr("data-education", d.properties.bachelorsOrHigher);
            })
            // MOUSELEAVE
          .on("mouseleave", mouseleave);
        
        // draw county borders
        svgContainer.append("path")
          .attr("class", "county-border")
          .attr("d", path(topojson.mesh(mapData, mapData.objects.counties, function(a,b){
            return a !== b;
          })));
      
    })
              
            
!
999px

Console