UNPKG

sg-heatmap

Version:

Open-source all-in-one Swiss Army knife tool for creating Choropleth maps

194 lines (151 loc) 5.47 kB
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); import chroma from 'chroma-js'; export default function getColorScale(colorArray) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; Object.assign({ bezierInterpolate: false, correctLightness: true, interpolationMode: 'lab' }, options); var scale = options.bezierInterpolate ? chroma.bezier(colorArray).scale() : chroma.scale(colorArray).mode(options.interpolationMode); if (options.correctLightness) scale = scale.correctLightness(); return function (value) { return scale(value).css(); }; } export function optimizePointSpread(stat, options) { options = Object.assign({ epsilon: 0.000001, gamma: 0.3, maxIteration: 300 }, options); var values = stat.values, min = stat.min, max = stat.max; var domain = options.domain || [min, max]; var v = Object.keys(values).map(function (key) { return (values[key] - domain[0]) / (domain[1] - domain[0]); }); v.sort(function (a, b) { return a - b; }); var k = 1; var kHistory = []; var scoreHistory = []; var _loop = function _loop() { var vk = v.map(function (item) { return Math.pow(item, k); }); var lnV = v.map(function (item) { return item ? Math.log(item) : 0; }); var deltaVk = []; var deltalnVvk = []; for (var i = 1; i < v.length; i++) { deltaVk.push(vk[i] - vk[i - 1]); deltalnVvk.push(lnV[i] * vk[i] - lnV[i - 1] * vk[i - 1]); } var beta = deltaVk.reduce(function (sum, dVk, i) { return sum + 2 * dVk * deltalnVvk[i]; }, 0); var score = deltaVk.reduce(function (sum, dVn) { return sum + Math.pow(dVn, 2); }, 0); kHistory.push(k); scoreHistory.push(score); k = Math.exp(Math.log(k) - options.gamma * beta * k); if (scoreHistory.length >= 2) { var _scoreHistory$slice = scoreHistory.slice(-2), _scoreHistory$slice2 = _slicedToArray(_scoreHistory$slice, 2), prevScore = _scoreHistory$slice2[0], lastScore = _scoreHistory$slice2[1]; if (Math.abs(lastScore - prevScore) < options.epsilon) return 'break'; } }; while (kHistory.length < options.maxIteration) { var _ret = _loop(); if (_ret === 'break') break; } return { domain: domain, transform: k, kHistory: kHistory, scoreHistory: scoreHistory }; } export function BuGn() { return getColorScale('BuGn', { correctLightness: false }); } export function BuPu() { return getColorScale('BuPu', { correctLightness: false }); } export function GnBu() { return getColorScale('GnBu', { correctLightness: false }); } export function OrRd() { return getColorScale('OrRd', { correctLightness: false }); } export function PuBu() { return getColorScale('PuBu', { correctLightness: false }); } export function PuBuGn() { return getColorScale('PuBuGn', { correctLightness: false }); } export function PuRd() { return getColorScale('PuRd', { correctLightness: false }); } export function RdPu() { return getColorScale('RdPu', { correctLightness: false }); } export function YlGn() { return getColorScale('YlGn', { correctLightness: false }); } export function YlGnBu() { return getColorScale('YlGnBu', { correctLightness: false }); } export function YlOrBr() { return getColorScale('YlOrBr', { correctLightness: false }); } export function YlOrRd() { return getColorScale('YlOrRd', { correctLightness: false }); } export function Blues() { return getColorScale('Blues', { correctLightness: false }); } export function Greens() { return getColorScale('Greens', { correctLightness: false }); } export function Greys() { return getColorScale('Greys', { correctLightness: false }); } export function Oranges() { return getColorScale('Oranges', { correctLightness: false }); } export function Purples() { return getColorScale('Purples', { correctLightness: false }); } export function Reds() { return getColorScale('Reds', { correctLightness: false }); } export function BrBG() { return getColorScale('BrBG', { correctLightness: false }); } export function PiYG() { return getColorScale('PiYG', { correctLightness: false }); } export function PRGn() { return getColorScale('PRGn', { correctLightness: false }); } export function PuOr() { return getColorScale('PuOr', { correctLightness: false }); } export function RdBu() { return getColorScale('RdBu', { correctLightness: false }); } export function RdGy() { return getColorScale('RdGy', { correctLightness: false }); } export function RdYlBu() { return getColorScale('RdYlBu', { correctLightness: false }); } export function RdYlGn() { return getColorScale('RdYlGn', { correctLightness: false }); } export function Spectral() { return getColorScale('Spectral', { correctLightness: false }); }