Skip to content
Snippets Groups Projects
Commit a850674f authored by Ryan Smith's avatar Ryan Smith
Browse files

Use kmeans to automatically group map data

See http://roadtolarissa.com/coloring-maps/ and
http://bl.ocks.org/thadk/37ff4c03358b414acd1e as to why this is a better
solution than quantize.
parent 94bedbc1
No related merge requests found
......@@ -17,6 +17,7 @@ import LeafletChoropleth from "./LeafletChoropleth.jsx";
import { computeMinimalBounds } from "metabase/visualizations/lib/mapping";
import d3 from "d3";
import ss from "simple-statistics";
import _ from "underscore";
// const HEAT_MAP_COLORS = [
......@@ -162,15 +163,20 @@ export default class ChoroplethMap extends Component {
}
const valuesMap = {};
const domain = []
for (const row of rows) {
valuesMap[getRowKey(row)] = (valuesMap[getRowKey(row)] || 0) + getRowValue(row);
domain.push(getRowValue(row));
}
var colorScale = d3.scale.quantize().domain(d3.extent(rows, getRowValue)).range(HEAT_MAP_COLORS);
const groups = ss.ckmeans(domain, HEAT_MAP_COLORS.length);
var colorScale = d3.scale.quantile().domain(groups.map((cluster) => cluster[0])).range(HEAT_MAP_COLORS);
let legendColors = HEAT_MAP_COLORS.slice();
let legendTitles = HEAT_MAP_COLORS.map((color, index) => {
let [min, max] = colorScale.invertExtent(color);
const min = groups[index][0];
const max = groups[index].slice(-1)[0];
return index === HEAT_MAP_COLORS.length - 1 ?
formatNumber(min) + " +" :
formatNumber(min) + " - " + formatNumber(max)
......
......@@ -7120,6 +7120,10 @@ signal-exit@^3.0.0:
version "3.0.2"
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"
simple-statistics@^2.5.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/simple-statistics/-/simple-statistics-2.5.0.tgz#7cd77b04288402f17fe21fd2b63c5e1a51ef3f51"
simple-swizzle@^0.2.2:
version "0.2.2"
resolved "https://registry.yarnpkg.com/simple-swizzle/-/simple-swizzle-0.2.2.tgz#a4da6b635ffcccca33f70d17cb92592de95e557a"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment