USIncomeChoropleth

by Jason Baker

HTML

<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css" integrity="sha512-Rksm5RenBEKSKFjgI3a41vrjkw4EVPlJ3+OiI65vTjIdo9brlAacEuKOiQ5OFh7cOI1bkDwLqdLw3Zg0cRJAAQ==" crossorigin="" />
  <script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet.js" integrity="sha512-/Nsx9X4HebavoBvEBuyp3I7od5tA0UzAxs+j83KgC8PU0kgB4XiK4Lfe4y4cgBtaRJQEIFCW+oC506aPT2L1zw==" crossorigin=""></script>
  <script src="https://d3js.org/d3.v3.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/leaflet-search@2.9.0/dist/leaflet-search.src.min.js"></script>
</head>
<div id='container'>
  <div id='header'>
    <h4>
      United States Income Change, 1989-2015
    </h4>
    <h6>
      Inflation adjusted for 2015 US Dollars.
    </h6>
  </div>
  <div id='layers'>
    <h6 style='margin-top: 10px; margin-bottom: 10px;'>
      Click on a subset of data to reshade the map.
    </h6>
    <ul>
      <button class='navoption' v-for='option in fields' @click="current=option; generateMap(current,checkedStates);" :style="style(option)">{{option.charAt(0).toUpperCase()+option.slice(1).replace('-',' ').replace('b','')}}
    </button>
    </ul>
    <hr>
  </div>
  <div id='checkswrap'>
    <h5>
      Add or remove individual states from the map.
    </h5>
    <div id='checks'>
      <label v-for='state in states' class="stateselection">
      <input type='checkbox' :value='state' v-model='checkedStates' @click='generateMap(current,checkedStates);'>
      <span>{{state}}</span>
      </label>
    </div>

  </div>
  <div id="statemap">
    <div id='map'>
    </div>
  </div>
  <div id='footer'><br>Data courtesy <a href="https://github.com/pri-data/50-states">PRI</a></div>
</div>

CSS

#container {
  text-align: center;
  display: grid;
  font-family: helvetica;
  grid-template-areas: "h h h h h h h h h h" "b b b b b b b b b b" "s s m m m m m m m m" "s s m m m m m m m m" "s s m m m m m m m m" "s s m m m m m m m m" "s s m m m m m m m m" "s s m m m m m m m m" "f f f f f f f f f f";
}

#header {
  grid-area: h;
}

#checkswrap {
  grid-area: s;
  max-width: 80%;
  margin: auto;
  max-height: 600px;
  text-align: left;
}

#checkswrap label {
  display: block;
  width: 50px;
}

#checks {
  height: 500px;
  overflow: auto;
}

#statemap {
  grid-area: m;
  width: 800px;
}

#map {
  height: 600px;
  margin: auto;
  grid-area: m;
}

#layers {
  grid-area: b;
}

#footer {
  grid-area: f;
}

/* footer {
  font-size: 8pt;
} */

.leaflet-popup {
  width: 400px;
}

.popupFeature {
  text-align: center;
  margin: auto;
  width: 360px;
  max-height: 400px;
  overflow: auto;
}

.leaflet-popup img {
  width: 240px;
  max-height: 200px;
  text-align: center;
}

.atext {
  text-align: left;
}

#layers ul {
  list-style: none;
}

.navoption {
  border: 2px solid black;
  border-radius: 3px;
  padding: 5px;
  margin: 2px;
  box-shadow: 1px 1px black;
  text-shadow: 1px 1px rgb(255, 255, 255, 0.5);
}

.navoption:hover {
  color: white;
}

#header {
  background-color: grey;
  border: 1px solid black;
}

.stateselection {
  margin: 5px;
}

.stateselection:hover {
  background-color: grey;
  border: 1px solid black;
}

/* 
.info {
  padding: 6px 8px;
  font: 14px/16px Arial, Helvetica, sans-serif;
  background: white;
  background: rgba(255, 255, 255, 0.8);
  box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
  border-radius: 5px;
} */

.leaflet-tooltip.ttip {
  background-color: rgb(255, 255, 255, 1);
  border: 1px solid black;
  opacity: 1;
  border-radius: 1px;
}

.leaflet-tooltip.ttip td {
  text-align: center;
  width: 40px;
  text-align: center;
  border: 1px solid black;
  border-radius: 2px;
}

/* .ptable {
  margin: auto;
} */

.leaflet-tooltip.ttip th {
  text-align:...

JavaScript

var url = "https://raw.githubusercontent.com/jtbaker/data/master/geodata/USCountyMedianIncome19892015.geojson";

var statesJSON = fetch(url)
  .then(response => response.json())

var generateMap = function(field, states) {

  document.getElementById('statemap').innerHTML = "<div id='map'></div>";
  var map = new L.map('map').setView([37.8, -96], 4);
  url = "https://raw.githubusercontent.com/jtbaker/data/master/geodata/USCountyMedianIncome19892015.geojson";


  style1 = function(feature, breakfunc) {
    return {
      'fillColor': breakfunc(feature.properties[field]),
      'weight': 0.5,
      'opacity': 0.5,
      'color': 'black',
      'fillOpacity': 0.8
    }
  }

  statesJSON.then(function(data) {
    var quants = {
      1: 'rgb(214, 101, 36, 0.9)',
      2: 'rgb(239, 197, 45, 0.9)',
      3: 'rgb(249, 249, 9, 0.9)',
      4: 'rgb(52, 193, 9, 0.9)',
    };
    colorscales = Object.keys(quants);
    breakfunc = d3.scale.quantile()
      .domain(data.features.map(item => item.properties[field]))
      .range(Object.values(quants));
    breakObj = {};
    for (var instance of ['change', 'income-2015', 'income-1989b']) {
      breakObj[instance] = d3.scale.quantile().domain(data.features.map(item => item.properties[instance])).range(colorscales);
    };
    changebreak = d3.scale.quantile()
      .domain(data.features.map(item => item.properties.change)).range(colorscales);
    /*    var income2015break = d3.scale.quantile()
          .domain(data.features.map(item => item.properties['income-2015']))
          .range(colorscales);
        var income1989break = d3.scale.quantile()
          .domain(data.features.map(item => item.properties['income-1989b']))
          .range(colorscales); */
    var colorMap = function(item, value) {
      var goodfields = ['change', 'income-2015', 'income-1989b'];
      if (goodfields.includes(item) & typeof(value) == 'number') {
        return breakObj[item](value)
      } else return ''
    };

    var handleNulls = (label,...