Skip to content

Commit

Permalink
remove duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasneirynck committed Apr 11, 2018
1 parent f47d899 commit 57c4099
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 61 deletions.
31 changes: 4 additions & 27 deletions src/core_plugins/region_map/public/choropleth_layer.js
Expand Up @@ -6,6 +6,7 @@ import { KibanaMapLayer } from 'ui/vis/map/kibana_map_layer';
import { truncatedColorMaps } from 'ui/vislib/components/color/truncated_colormaps';
import * as topojson from 'topojson-client';
import { toastNotifications } from 'ui/notify';
import * as colorUtil from 'ui/vis/map/color_util';

const EMPTY_STYLE = {
weight: 1,
Expand Down Expand Up @@ -179,7 +180,7 @@ CORS configuration of the server permits requests from the Kibana application on

if (this._metrics && this._metrics.length > 0) {
const { min, max } = getMinMax(this._metrics);
this._legendColors = getLegendColors(this._colorRamp);
this._legendColors = colorUtil.getLegendColors(this._colorRamp);
const quantizeDomain = (min !== max) ? [min, max] : d3.scale.quantize().domain();
this._legendQuantizer = d3.scale.quantize().domain(quantizeDomain).range(this._legendColors);
}
Expand Down Expand Up @@ -420,39 +421,15 @@ function getMinMax(data) {
return { min, max };
}

function getLegendColors(colorRamp) {
const colors = [];
colors[0] = getColor(colorRamp, 0);
colors[1] = getColor(colorRamp, Math.floor(colorRamp.length * 1 / 4));
colors[2] = getColor(colorRamp, Math.floor(colorRamp.length * 2 / 4));
colors[3] = getColor(colorRamp, Math.floor(colorRamp.length * 3 / 4));
colors[4] = getColor(colorRamp, colorRamp.length - 1);
return colors;
}

function getColor(colorRamp, i) {

if (!colorRamp[i]) {
return getColor();
}

const color = colorRamp[i][1];
const red = Math.floor(color[0] * 255);
const green = Math.floor(color[1] * 255);
const blue = Math.floor(color[2] * 255);
return `rgb(${red},${green},${blue})`;
}


function getChoroplethColor(value, min, max, colorRamp) {
if (min === max) {
return getColor(colorRamp, colorRamp.length - 1);
return colorUtil.getColor(colorRamp, colorRamp.length - 1);
}
const fraction = (value - min) / (max - min);
const index = Math.round(colorRamp.length * fraction) - 1;
const i = Math.max(Math.min(colorRamp.length - 1, index), 0);

return getColor(colorRamp, i);
return colorUtil.getColor(colorRamp, i);
}


Expand Down
1 change: 0 additions & 1 deletion src/core_plugins/tile_map/public/geohash_layer.js
Expand Up @@ -108,7 +108,6 @@ export class GeohashLayer extends KibanaMapLayer {
return true;
}


//check if any impacts leaflet styler function
if (this._geohashOptions.colorRamp !== options.colorRamp) {
return false;
Expand Down
35 changes: 2 additions & 33 deletions src/core_plugins/tile_map/public/markers/scaled_circles.js
Expand Up @@ -4,6 +4,7 @@ import d3 from 'd3';
import $ from 'jquery';
import { EventEmitter } from 'events';
import { truncatedColorMaps } from 'ui/vislib/components/color/truncated_colormaps';
import * as colorUtil from 'ui/vis/map/color_util';

export class ScaledCirclesMarkers extends EventEmitter {

Expand Down Expand Up @@ -195,43 +196,11 @@ export class ScaledCirclesMarkers extends EventEmitter {
}


/**
* d3 quantize scale returns a hex color, used for marker fill color
*
* @method quantizeLegendColors
* return {undefined}
*/
function makeLegendColors(colorRampKey) {
const colorRamp = truncatedColorMaps[colorRampKey];
const legendColors = getLegendColors(colorRamp);
return legendColors;
return colorUtil.getLegendColors(colorRamp);
}

function getLegendColors(colorRamp) {
const colors = [];
colors[0] = getColor(colorRamp, 0);
colors[1] = getColor(colorRamp, Math.floor(colorRamp.length * 1 / 4));
colors[2] = getColor(colorRamp, Math.floor(colorRamp.length * 2 / 4));
colors[3] = getColor(colorRamp, Math.floor(colorRamp.length * 3 / 4));
colors[4] = getColor(colorRamp, colorRamp.length - 1);
return colors;
}

function getColor(colorRamp, i) {

if (!colorRamp[i]) {
return getColor();
}

const color = colorRamp[i][1];
const red = Math.floor(color[0] * 255);
const green = Math.floor(color[1] * 255);
const blue = Math.floor(color[2] * 255);
return `rgb(${red},${green},${blue})`;
}



function makeColorDarker(color) {
const amount = 1.3;//magic number, carry over from earlier
return d3.hcl(color).darker(amount).toString();
Expand Down
17 changes: 17 additions & 0 deletions src/ui/public/vis/map/color_util.js
@@ -0,0 +1,17 @@
export function getLegendColors(colorRamp) {
const colors = [];
colors[0] = getColor(colorRamp, 0);
colors[1] = getColor(colorRamp, Math.floor(colorRamp.length * 1 / 4));
colors[2] = getColor(colorRamp, Math.floor(colorRamp.length * 2 / 4));
colors[3] = getColor(colorRamp, Math.floor(colorRamp.length * 3 / 4));
colors[4] = getColor(colorRamp, colorRamp.length - 1);
return colors;
}

export function getColor(colorRamp, i) {
const color = colorRamp[i][1];
const red = Math.floor(color[0] * 255);
const green = Math.floor(color[1] * 255);
const blue = Math.floor(color[2] * 255);
return `rgb(${red},${green},${blue})`;
}

0 comments on commit 57c4099

Please sign in to comment.