Styling Leaflet circleMarkers by numerical property with a color ramp (including undefined values)

Synthetic GeoJSON data are created with a quasi-normal spatial distribution and displayed as circle markers on a Leaflet map. The markers are styled using a color ramp that is tied to a numerical property ("year" in this case).

by nathansnider

HTML

<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.css">
<script src="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.js"></script>
<div id="map"></div>

CSS

html, body, #map {
      height: 100%;
      width:100%;
      padding:0px;
      margin:0px;
   } 

.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;
}
.legend {
    text-align: left;
    line-height: 18px;
    color: #555;
}
.legend i {
    width: 18px;
    height: 18px;
    float: left;
    margin-right: 8px;
    opacity: 0.7;
}
.legend .colorcircle {
    border-radius: 50%;
    width: 15px;
    height: 15px;
    margin-top: 0px;
}

JavaScript

/////////////////////////////////////////////////////////////////////////////////////////////
//setting up the map//
/////////////////////////////////////////////////////////////////////////////////////////////

// set center coordinates
var centerlat = 34.05;
var centerlon = -118.25;

// set default zoom level
var zoomLevel = 10; 

// initialize map
var map = L.map('map').setView([centerlat,centerlon], zoomLevel);
 
// set source for map tiles
ATTR = '&copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a> | ' +
'&copy; <a href="http://cartodb.com/attributions">CartoDB</a>';

CDB_URL = 'http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png';

// add tiles to map
L.tileLayer(CDB_URL, {attribution: ATTR}).addTo(map);

/////////////////////////////////////////////////////////////////////////////////////////////
//creating some synthetic GeoJSON data//
/////////////////////////////////////////////////////////////////////////////////////////////

//initialize
var dotlayer;
var dots;
var dotcount = 500;
make_dots();

/////////////////////////////////////////////////////////////////////////////////////////////
//styling and displaying the data as circle markers//
/////////////////////////////////////////////////////////////////////////////////////////////

//highlight style
var dotStyleHighlight = {
    radius: 8,
    fillColor: "#102040",
    color: "#116",
    weight: 1,
    opacity: 1,
    fillOpacity: 0.9
};

//create marker layer and display it on the map
dotlayer = L.geoJson(dots, {
    pointToLayer: function (feature, latlng) {
        return L.circleMarker(latlng, style(feature));
        },
    onEachFeature: onEachDot
}).addTo(map);

/////////////////////////////////////////////////////////////////////////////////////////////
//legend//
/////////////////////////////////////////////////////////////////////////////////////////////

//create circle color legend
var...