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 class="text-center">United States Educational Attainment</h1>
<h4 class="text-center">Percentage of adults age 25 and older with a bachelor's degree or higher (2010-2014)</h4>
<div id="main"></div>
              
            
!

CSS

              
                #main {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #f2f2f2;
  width: 1400px;
  margin-top: 150px;
}
.states {
  fill: none;
  stroke: #fff;
  stroke-linejoin: round;
}

              
            
!

JS

              
                var path = d3.geoPath();
var tooltip = d3
  .select("body")
  .append("div")
  .style("width", "200px")
  .style("position", "relative")
  .style("background", "#d1e0e0")
  .style("border", "1px solid grey")
  .style("border-radius", "5px")
  .style("opacity", "0"); //create tooltip div
var canvas = d3
  .select("#main")
  .append("svg")
  .attr("width", 1400)
  .attr("height", 600)
  .attr("transform", "translate(50, 0)"); //create canvas
var colorScale = d3
  .scaleThreshold()
  .domain(d3.range(2.6, 75.1, (75.1 - 2.6) / 8))
  .range(d3.schemeGreens[9]);
var counties =
  "https://raw.githubusercontent.com/no-stack-dub-sack/testable-projects-fcc/master/src/data/choropleth_map/counties.json";
var education =
  "https://raw.githubusercontent.com/no-stack-dub-sack/testable-projects-fcc/master/src/data/choropleth_map/for_user_education.json";
d3
  .queue()
  .defer(d3.json, counties)
  .defer(d3.json, education)
  .await(drawMap);
function drawMap(error, counties, education) {
  if (error) throw error;
  var cantons = topojson.feature(counties, counties["objects"]["counties"])
    .features;
  cantons.sort(function(a, b) {
    return d3.ascending(a["id"], b["id"]);
  }); //sort the county data
  console.log(cantons[0]);
  canvas
    .append("g")
    .selectAll("path")
    .data(cantons)
    .enter()
    .append("path")
    .attr("transform", "translate(150, 0)")
    .attr("fill", function(d, i) {
      return colorScale(education[i]["bachelorsOrHigher"]);
    })
    .attr("d", path)
    .on("mouseover", function(d) {
      tooltip.transition().style("opacity", "1");
      tooltip
        .html(function() {
          var result = education.filter(function(obj) {
            return obj["fips"] == d["id"];
          });
          if (result[0]) {
            return (
              result[0]["area_name"] +
              ", " +
              result[0]["state"] +
              ": " +
              result[0]["bachelorsOrHigher"] +
              "%"
            );
          }
          return 0;
        })
        .style("left", d3.event.pageX + 7 + "px")
        .style("top", d3.event.pageY - 70 + "px");
      d3.select(this).attr("stroke", "black");
    })
    .on("mouseout", function(d) {
      tooltip.transition().style("opacity", "0");
      d3.select(this).attr("stroke", "none");
    }); //draw counties
  canvas
    .append("path")
    .attr("transform", "translate(150, 0)")
    .datum(
      topojson.mesh(counties, counties["objects"]["states"], function(a, b) {
        return a !== b;
      })
    )
    .attr("class", "states")
    .attr("d", path); //draw states
  var legendScale = d3
    .scaleLinear()
    .domain([2.6, 75.1])
    .rangeRound([600, 860]); //set legend scale
  var legend = canvas
    .selectAll("rect")
    .data(
      colorScale.range().map(function(d) {
        d = colorScale.invertExtent(d);
        if (d[0] == null) d[0] = legendScale.domain()[0];
        if (d[1] == null) d[1] = legendScale.domain()[1];
        return d;
      })
    )
    .enter()
    .append("rect")
    .attr("height", 8)
    .attr("x", function(d) {
      return 150 + legendScale(d[0]);
    })
    .attr("y", 50)
    .attr("width", function(d) {
      return legendScale(d[1]) - legendScale(d[0]);
    })
    .attr("fill", function(d) {
      return colorScale(d[0]);
    })
    .style("fill", function(d, i) {
      return colorScale[i];
    }); //create the legend's rectangles
  var legendXAxis = d3
    .axisBottom(legendScale)
    .tickArguments([5])
    .tickValues(colorScale.domain())
    .tickFormat(function(x) {
      return Math.round(x) + "%";
    }); //create x-axis
  canvas
    .append("g")
    .attr("transform", "translate(150, 58)")
    .call(legendXAxis); //display the legend's x-axis
}

              
            
!
999px

Console