Skip to content

Instantly share code, notes, and snippets.

@KatiRG
Created September 22, 2015 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KatiRG/87b95d8e3c1827fd0a72 to your computer and use it in GitHub Desktop.
Save KatiRG/87b95d8e3c1827fd0a72 to your computer and use it in GitHub Desktop.
Modification to dc.leaflet.js to a) plot colorscale in choropleth in the right order and b) to accept colorbrewer.js scales
dc.leafletChoroplethChart = function(parent, chartGroup) {
var _chart = dc.colorChart(dc.leafletChart({}));
var _geojsonLayer = false;
var _dataMap = [];
var _geojson = false;
var _renderPopup = true;
var _brushOn = true;
var _featureOptions = {
'fillColor':'black',
'color':'gray',
'opacity':0.4,
'fillOpacity':0.6,
'weight':1
};
var _renderTitle = true;
//-----------------katiRG edits--------------------
var colorPalette = colorbrewer.YlGn[7]; //default: d3.scale.category20c().range();
//modified to accept minmaxValue which is previously calculated in the choro chart
var _colorCalculator = function(value, minmaxValue) {
//orig code:
// var domain = _colorDomain;
// if (typeof _colorDomain === 'function')
// domain = _colorDomain.call(_chart);
// var minValue = domain[0];
// var maxValue = domain[1];
var minValue = minmaxValue[0];
var maxValue = minmaxValue[1];
// What is this? maybe can put back in:
// if (isNaN(value)) value = 0;
// if (!dc.utils.isNumber(maxValue)) return _colors(value);
//var colorsLength = _chart.colors().range().length; //orig
var colorsLength = colorPalette.length;
var denominator = (maxValue - minValue) / colorsLength;
var colorValue = Math.abs(Math.min(colorsLength - 1, Math.round((value - minValue) / denominator)));
// return _chart.colors()(colorValue); //orig
return colorPalette[colorValue];
};
var _colorAccessor = function(d, i){return i;};
_chart.getColor = function(d, i){
//return _colorCalculator(_colorAccessor(d, i)); //orig
//Pass in _chart.colorDomain(), the min and max value previously calculated in the choro chart:
return _colorCalculator(_colorAccessor(d, i), _chart.colorDomain());
};
_chart.colorAccessor = function(_){
if(!arguments.length) return _colorAccessor;
_colorAccessor = _;
return _chart;
};
//-----------------end katiRG edits--------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment