Skip to content

ramjoshi/sf-realty

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 

Repository files navigation

San Francisco Pavement Quality and Housing Prices

A visualization using D3 + Leaflet + Backbone + RequireJS

SF Pavements and Housing screenshot SF Pavements and Housing screenshot SF Pavements and Housing screenshot

Live demo https://sfrealty.ramjoshi.com/

Projecting GeoJson data onto a Leaflet map using D3

See [D3 + Leaflet by Mike Bostock] leaflet-d3.

The core logic for projecting geojson using D3 is in src/js/common/views/geo.d3.js

This Backbone View manges the following functions

  • Rendering geojson on a map
  • Repositioning geojson svg when map bounds change
  • D3 update pattern for binding data to geojson and animation
  • Binding mouse events to geojson svg elements

src/js/common/views/geo.d3.js is an abstract base class. Each visualization extends geo.d3.js

geo.d3.js defines update, enter and exit methods that can be overriden to create a D3 animation for geojson svg paths. For example, the pavement quality colors for streets are assigned in the D3 enter method of sf-streets-geo.d3.js. The entire sf-streets-geo.d3.js module is less than 40 lines of code.

define([
  'js/common/views/geo.d3',
  'd3'
], function(GeoD3View,
            d3) {

  return GeoD3View.extend({

    _colors: null,

    initialize: function(options) {
      GeoD3View.prototype.initialize.apply(this, arguments);

      this._colors = d3.scale.linear()
        .domain([0, 50, 70, 100])
        .range(this.constructor.PALETTE);
    },

    getFeatureKey: function(feature) {

      // Return a unique id for this geojson feature
      return feature && feature.properties.CNN;
    },

    enter: function(selection) {
      var self = this;
      selection.each(function(data) {

        // Color each entering selection using a PCI score that indicates
        // the quality of a pavement on a scale of 0 through 100
        var pci = data.properties.PCI;
        d3.select(this).attr('stroke', self._colors(pci));
      });
    }
  }, {
    PALETTE: ["#F00", "#930", "#FC0", "#3B0"]
  });
});

Data

The data files referenced in this project can be found at https://github.com/ramjoshi/data

Inspired by

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 92.6%
  • CSS 6.2%
  • HTML 1.2%