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

              
                
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Domine');

              
            
!

JS

              
                const URL = "https://raw.githubusercontent.com/no-stack-dub-sack/testable-projects-fcc/master/src/data/choropleth_map/"
const COUNTY_GEOMETRY = URL+"counties.json";
const EDUCATION = URL+"for_user_education.json";
// const color = "#49306B #635380 #90708C #BBDB9B #ABC4A1".split(" ").reverse();
const color = "#E87461 #E0C879 #D5D887 #C9ED87 #A1CF6B #7FD153 #68C436 #4FB21A #3C930D #297500".split(" ");
let mapFlex = window.innerWidth > 1015;

function init() {
  const req = new XMLHttpRequest();
  req.open(
    "GET",
    COUNTY_GEOMETRY,
    true
  );
  req.send();
  req.onload = () => {
    const mapData = JSON.parse(req.responseText);
    const secondreq = new XMLHttpRequest();
    secondreq.open(
      "GET",
      EDUCATION,
      true
    );
    secondreq.send();
    secondreq.onload = () => {
      const eduData = JSON.parse(secondreq.responseText);
      displayMap(mapData, eduData);
    };
  };
}

function displayMap(mapData, eduData) {
  const place = datum => eduData.filter(e => e.fips === datum.id)[0];
  
  const lowestPercent = d3.min(eduData, e => e.bachelorsOrHigher);
  const highestPercent = d3.max(eduData, e => e.bachelorsOrHigher);
  
  const shadow = "10px 10px 5px 0px rgba(0,0,0,0.75)";
  
  const colorScale = d3.scaleLinear()
    .domain([lowestPercent, highestPercent])
    .range([0, color.length]);
  
  const body = d3.select('body')
    .style('text-align', 'center')
    .style('font-family', "'Domine', serif");
  
  const title = body.append('h1')
    .attr('id', 'title')
    .text('US Academic Achievement Choropleth Map');
  
  const subtitle = body.append('h2')
    .attr('id', 'description')
    .text('Percentage of 25+ year olds with Bachelor\'s degree or higher');
  
  const tooltip = body.append('div')
    .attr('id', 'tooltip')
    .style('position', 'absolute')
    .style('pointer-events', 'none')
    .style('display', 'flex')
    .style('justify-content', 'center')
    .style('align-items', 'center')
    .style('width', '200px')
    .style('height', '200px')
    .style('left', 'calc(50vw - 100px)')
    .style('opacity', 0)
    .style('background-color', 'rgba(255,255,255,0.8)')
    .style("box-shadow", shadow)
    .style("-moz-box-shadow", shadow)
    .style("-webkit-box-shadow", shadow);
  
  let mapDisplay = mapFlex ? 'flex' : 'block';
  
  const mapContainer = body.append('div')
    .style('display', mapDisplay)
    .style('justify-content', 'center')
    .attr('id', 'mapContainer')
    .append('div')
    .style('width', '980px')
    .style('height', '600px')
    .style('border', '5px solid #639B99')
    .style('background-color', '#DAFFED')
    .style("box-shadow", shadow)
    .style("-moz-box-shadow", shadow)
    .style("-webkit-box-shadow", shadow);
  
  const svg = mapContainer.append('svg')
    .attr('id', 'map')
    .attr('width', '980px')
    .attr('height', '600px');
  
  const counties = svg.append("g")
    .attr("class", "counties")
    .selectAll("path")
    .data(topojson.feature(mapData, mapData.objects.counties).features)
    .enter()
    .append("path")
    .attr('class', 'county')
    .attr('data-fips', d => d.id)
    .attr('data-education', d => place(d).bachelorsOrHigher)
    .attr("fill", d => {
      let colorCode = Math.round(colorScale(place(d).bachelorsOrHigher));
      return color[colorCode];
    })
    .attr("d", d3.geoPath())
    .on('mouseover', d => {
      let offset = $("#map").offset();
      tooltip
        .attr('data-education', place(d).bachelorsOrHigher)
        .transition()
        .duration(200)
        .style('opacity', 1)
        .style('left', (d3.event.pageX + 20)+'px')
        .style('top', (d3.event.pageY - 20)+'px')
      tooltip.html(
        place(d).area_name + ", " 
        + place(d).state + "<br>"
        + place(d).bachelorsOrHigher
        + "% of adults with Bachelor's degree or higher"
      );
    })
    .on('mouseout', () => {
      tooltip.transition()
        .duration(200)
        .style('opacity', 0);
    });
  
  const legend = svg.append('g')
    .attr('id', 'legend');
  
  const boxWidth = 35;
  
  for (let i = 0; i < color.length; i++) {
    legend.append('rect')
      .attr('x', 450+(i*boxWidth)+'px')
      .attr('y', '5px')
      .attr('width', boxWidth+'px')
      .attr('height', boxWidth+'px')
      .attr('fill', color[i]);
    legend.append('text')
      .attr('x', 450+(i*boxWidth)+'px')
      .attr('y', '50px')
      .style('font-size', '10px')
      .text((lowestPercent + (highestPercent - lowestPercent) / color.length * i).toFixed(1)+"%");
  }
};

$( window ).resize(() => {
  if (mapFlex && window.innerWidth < 1015 || !mapFlex && window.innerWidth >= 1015) {
    mapFlex = !mapFlex;
    $("body").empty();
    init();
  }
});

init();
              
            
!
999px

Console