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>
              
            
!

CSS

              
                html {
  overflow: hidden;
}

html, body {
  padding: 0;
  margin: 0;
}

body {
  display: flex;
  justify-content: center;
  font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
}

.main {
  max-width: 1000px;
  width: 100%;
  display: flex;
  padding-top: 1rem;
  flex-direction: column;
  align-items: center;
}

.counties {
  fill: none;
}

.states {
  fill: none;
  stroke: #fff;
  stroke-linejoin: round;
}
.county:hover {
    fill: maroon;
}
#tooltip {
  position: absolute;
  padding: 10px;                
  font-size: 14px;        
  background: rgb(233,122,110);
  color: white;
  box-shadow: 1px 1px 10px rgba(128, 128, 128, 0.6);
  border: 0px;        
  border-radius: 10px 0 10px 0;
  pointer-events: none;
}

#title {
  font-size: 3.5rem;
  margin: 0;
}

#description {
  text-align: center;
}

#source {
  align-self: flex-end;
  margin-top: 1rem;
  margin-right: 15px;
}

a {
  text-decoration: none;
  color: #1C5BA6;
}

a:hover {
  text-decoration: underline;
}


@media screen and (max-width: 720px) {
  #title {
    display: block;
    line-height: 1.2;
    text-align: center;
  }
  
  #description {
    max-width: 400px;
  }
}

              
            
!

JS

              
                // Created by HIC 

function responsivefy(svg) {
    var container = d3.select(svg.node().parentNode),
        width = parseInt(svg.style("width")),
        height = parseInt(svg.style("height")),
        aspect = width / height;

    svg.attr("viewBox", "0 0 " + width + " " + height)
        .attr("perserveAspectRatio", "xMinYMid")
        .call(resize);
    d3.select(window).on("resize." + container.attr("id"), resize);

    function resize() {
        var targetWidth = parseInt(container.style("width"));
        svg.attr("width", targetWidth);
        svg.attr("height", Math.round(targetWidth / aspect));
    }
}


const EDUCATION_URL = 'https://raw.githubusercontent.com/no-stack-dub-sack/testable-projects-fcc/master/src/data/choropleth_map/for_user_education.json';
const COUNTY_URL = 'https://raw.githubusercontent.com/no-stack-dub-sack/testable-projects-fcc/master/src/data/choropleth_map/counties.json';

const body = d3.select('body');

const mainPage = d3.select('body')
  .append('div')
  .attr('class', 'main');
const main = d3.select('.main');

// title
const title = main.append('h1')
  .text('Choropleth Map')
  .attr('id', 'title');
//description
const description = main.append('div')
  .text(`Percentage of adults age 25 and older with a bachelor's degree or higher (2010-2014)`)
  .attr('id', 'description');

const tooltip = body.append('div')
  .attr('id', 'tooltip')
  .style('opacity', 0);

const width = 1560;
const height = 1200;

const canvas = main.append('svg')
  .attr('id', 'canvas')
  .attr('width', width)
  .attr('height', height)
  .call(responsivefy);
const source = main.append('div')
  .attr('id', 'source');

const linkPrefix = source.append('span')
  .text('Source: ');
  
const link = source.append('a')
  .text('USDA Economic Research Service')
  .attr('href', 'https://www.ers.usda.gov/data-products/county-level-data-sets/download-data.aspx')
  .attr('target', '_blank');
const start = 2.6;
const end = 75.1;
const length = 8;


const x = d3.scaleLinear()
  .domain([start, end])
  .rangeRound([600, 860]);


const colorDomain = d3.range(start, end, (start - end) / length);

const color = d3.scaleThreshold()
  .domain(d3.range(start, end, (end - start) / length))
  .range(d3.schemeReds[length + 1]);


const legend = canvas.append('g')
  .attr('id', 'legend')
  .attr('transform', 'translate(0, 40)');

const colorRange = color.range()
  .map(d => {
    const inverted = color.invertExtent(d);
    
    if (!inverted[0]) {
      inverted[0] = x.domain()[0];
    }
    
    if (!inverted[1]) {
      inverted[1] = x.domain()[1];
    }
    
    return inverted;
  });

// color to legend
const legendColors = legend.selectAll('rect')
  .data(colorRange)
  .enter()
  .append('rect')
  .attr('height', 8)
  .attr('x', d => x(d[0]))
  .attr('width', d => x(d[1]) - x(d[0]))
  .attr('fill', d => color(d[0]));

//Okayyyyyyyyyy
const legendCaption = legend.append('text')
  .attr('class', 'caption')
  .attr('x', x.range()[0])
  .attr('y', -6)
  .attr('fill', 'black')
  .attr('text-anchor', 'start')
  .attr('font-weight', 'bold');
//Ehhhhhhhh
const formatting = d3.axisBottom(x)
  .tickSize(13)
  .tickFormat(x => `${Math.round(x)}%`)
  .tickValues(color.domain());

const legendFormat = legend.call(formatting)
  .select('.domain')
  .remove();

const path = d3.geoPath();

const q = d3.queue()
  .defer(d3.json, COUNTY_URL)
  .defer(d3.json, EDUCATION_URL)
  .await(createGraph);

function createGraph(error, county, education) {
  // Display error if cannot reach JSON data
  if (error) {
    const errorMessage = canvas.append('text')
      .attr('x', 150)
      .attr('y', 50)
      .attr('fill', 'red')
      .text("Sorry, there was an error in trying to reach the education and county data. Please try again later.");
    return; // Bhaoo
  }
  
  // Filter education results based on inputs
  const filterEducationById = (id) => education
    .filter(ed => ed.fips === id);
  
  // showing data
  const data = canvas.append('g')
    .attr('class', 'counties')
    .selectAll('path')
    .data(topojson.feature(county, county.objects.counties).features)
    .enter()
    .append('path')
    .attr('class', 'county')
    .attr('data-fips', d => {
      const result = filterEducationById(d.id);
      
      return result[0].fips;
    })
    .attr('data-education', d => {
      const result = filterEducationById(d.id);
      
      return result[0].bachelorsOrHigher;
    })
    .attr('fill', d => {
      const result = filterEducationById(d.id);
      
      return color(result[0].bachelorsOrHigher);
    })
    .attr('d', path)
    .on('mouseover', d => {
      const tooltipResult = filterEducationById(d.id);
      
      //  tooltip
      tooltip
        .transition()
        .duration(100)
        .style('opacity', 0.9);
      
      // Add data in tooltip
      tooltip
        .html(`
            ${tooltipResult[0]['area_name']},
            ${tooltipResult[0].state}:
            ${tooltipResult[0].bachelorsOrHigher}%
          `)
        .attr('data-education', tooltipResult[0].bachelorsOrHigher)
        .style('left', `${d3.event.pageX}px`)
        .style('top', `${d3.event.pageY - 30}px`)
    })
    .on('mouseout', d => {
      // Add tooltip mouseout transition
      tooltip
        .transition()
        .duration(200)
        .style('opacity', 0);      
    });
  
  // borders 
  const statesBorders = canvas.append('path')
    .datum(topojson.mesh(county, county.objects.states), (a, b) => a !== b)
    .attr('class', 'states')
    .attr('d', path);
}

              
            
!
999px

Console