Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

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

              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.onload = () => {
    const mapData = JSON.parse(req.responseText);
    const secondreq = new XMLHttpRequest();
    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')
    .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")
    .data(topojson.feature(mapData, mapData.objects.counties).features)
    .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();
        .attr('data-education', place(d).bachelorsOrHigher)
        .style('opacity', 1)
        .style('left', (d3.event.pageX + 20)+'px')
        .style('top', (d3.event.pageY - 20)+'px')
        place(d).area_name + ", " 
        + place(d).state + "<br>"
        + place(d).bachelorsOrHigher
        + "% of adults with Bachelor's degree or higher"
    .on('mouseout', () => {
        .style('opacity', 0);
  const legend = svg.append('g')
    .attr('id', 'legend');
  const boxWidth = 35;
  for (let i = 0; i < color.length; i++) {
      .attr('x', 450+(i*boxWidth)+'px')
      .attr('y', '5px')
      .attr('width', boxWidth+'px')
      .attr('height', boxWidth+'px')
      .attr('fill', color[i]);
      .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;

🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.