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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource


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


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.


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.


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


                #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;



                var path = d3.geoPath();
var tooltip = d3
  .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
  .attr("width", 1400)
  .attr("height", 600)
  .attr("transform", "translate(50, 0)"); //create canvas
var colorScale = d3
  .domain(d3.range(2.6, 75.1, (75.1 - 2.6) / 8))
var counties =
var education =
  .defer(d3.json, counties)
  .defer(d3.json, education)
function drawMap(error, counties, education) {
  if (error) throw error;
  var cantons = topojson.feature(counties, counties["objects"]["counties"])
  cantons.sort(function(a, b) {
    return d3.ascending(a["id"], b["id"]);
  }); //sort the county data
    .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");
        .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");"stroke", "black");
    .on("mouseout", function(d) {
      tooltip.transition().style("opacity", "0");"stroke", "none");
    }); //draw counties
    .attr("transform", "translate(150, 0)")
      topojson.mesh(counties, counties["objects"]["states"], function(a, b) {
        return a !== b;
    .attr("class", "states")
    .attr("d", path); //draw states
  var legendScale = d3
    .domain([2.6, 75.1])
    .rangeRound([600, 860]); //set legend scale
  var legend = canvas
      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;
    .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
    .tickFormat(function(x) {
      return Math.round(x) + "%";
    }); //create x-axis
    .attr("transform", "translate(150, 58)")
    .call(legendXAxis); //display the legend's x-axis