-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add config option to map values to colors #4894
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6ba8626
Add config option to map values to colors
lukasolson d0c0403
Merge branch 'master' into color-mapping-config
lukasolson 1609cba
Better variable name and load color from config dynamically
lukasolson 7b14699
Merge branch 'master' into color-mapping-config
lukasolson ad07d65
Move color mapping logic to mapped_colors and protect against duplica…
lukasolson 46096e8
Remove unused config argument and change to fat arrow functions
lukasolson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
define(function (require) { | ||
return function ColorUtilService(Private) { | ||
return function ColorUtilService(Private, config) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure why the config param was added since it isn't used |
||
var _ = require('lodash'); | ||
|
||
var createColorPalette = Private(require('ui/vislib/components/color/color_palette')); | ||
var MappedColors = Private(require('ui/vislib/components/color/mapped_colors')); | ||
var mappedColors = new MappedColors(); | ||
var mappedColors = Private(require('ui/vislib/components/color/mapped_colors')); | ||
|
||
/* | ||
* Accepts an array of strings or numbers that are used to create a | ||
|
@@ -24,15 +21,9 @@ define(function (require) { | |
} | ||
}); | ||
|
||
var arrayLength = arrayOfStringsOrNumbers.length; | ||
var colors = createColorPalette(arrayLength + mappedColors.count()); | ||
var uniqueColors = _.difference(colors, mappedColors.all()).slice(0, arrayLength + 1); | ||
var colorObj = _.zipObject(arrayOfStringsOrNumbers, uniqueColors); | ||
mappedColors.mapKeys(arrayOfStringsOrNumbers); | ||
|
||
return function (value) { | ||
if (!mappedColors.get(value)) { | ||
mappedColors.add(value, colorObj[value]); | ||
} | ||
return mappedColors.get(value); | ||
}; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,50 @@ | ||
define(function () { | ||
return function MappedColorFactory() { | ||
|
||
var _ = require('lodash'); | ||
/* | ||
* Maintains a lookup table that associates the value (key) with a hex color (value) | ||
* across the visualizations. | ||
* Provides functions to interact with the lookup table | ||
*/ | ||
|
||
var MappedColorService = function () { | ||
}; | ||
|
||
MappedColorService.colors = {}; | ||
/** | ||
* Allows to add value (key) and color (value) to the lookup table | ||
* | ||
* @method add | ||
* @param {String} key - the value in a visualization | ||
* @param {String} value - the color associated with the value | ||
*/ | ||
MappedColorService.prototype.add = function (key, value) { | ||
MappedColorService.colors[key] = value; | ||
}; | ||
|
||
/** | ||
* Provides the color (value) associated with the value (key) | ||
* | ||
* @method get | ||
* @param {String} key - the value for which color is required | ||
* @returns the color associated with the value | ||
*/ | ||
MappedColorService.prototype.get = function (key) { | ||
return MappedColorService.colors[key]; | ||
}; | ||
|
||
/** | ||
* Size of the mapped colors | ||
* | ||
* @method count | ||
* @returns the number of values (keys) stored in the lookup table | ||
*/ | ||
MappedColorService.prototype.count = function () { | ||
return _.keys(MappedColorService.colors).length; | ||
}; | ||
|
||
/** | ||
* All the colors of in the lookup table | ||
* | ||
* @method all | ||
* @returns all the colors used in the lookup table | ||
*/ | ||
MappedColorService.prototype.all = function () { | ||
return _.values(MappedColorService.colors); | ||
}; | ||
|
||
MappedColorService.prototype.reset = function () { | ||
MappedColorService.colors = {}; | ||
}; | ||
|
||
return MappedColorService; | ||
}; | ||
define((require) => (Private, config) => { | ||
const _ = require('lodash'); | ||
const d3 = require('d3'); | ||
const createColorPalette = Private(require('ui/vislib/components/color/color_palette')); | ||
|
||
const standardizeColor = (color) => d3.rgb(color).toString(); | ||
function getConfigColorMapping() { | ||
return _.mapValues(config.get('visualization:colorMapping'), standardizeColor); | ||
} | ||
|
||
/* | ||
* Maintains a lookup table that associates the value (key) with a hex color (value) | ||
* across the visualizations. | ||
* Provides functions to interact with the lookup table | ||
*/ | ||
class MappedColors { | ||
constructor() { | ||
this.mapping = {}; | ||
} | ||
|
||
get(key) { | ||
return getConfigColorMapping()[key] || this.mapping[key]; | ||
} | ||
|
||
mapKeys(keys) { | ||
const configMapping = getConfigColorMapping(); | ||
const configColors = _.values(configMapping); | ||
|
||
const keysToMap = []; | ||
_.each(keys, (key) => { | ||
// If this key is mapped in the config, it's unnecessary to have it mapped here | ||
if (configMapping[key]) delete this.mapping[key]; | ||
|
||
// If this key is mapped to a color used by the config color mapping, we need to remap it | ||
if (_.contains(configColors, this.mapping[key])) keysToMap.push(key); | ||
|
||
// If this key isn't mapped, we need to map it | ||
if (this.get(key) == null) keysToMap.push(key); | ||
}); | ||
|
||
// Generate a color palette big enough that all new keys can have unique color values | ||
const allColors = _(this.mapping).values().union(configColors).value(); | ||
const colorPalette = createColorPalette(allColors.length + keysToMap.length); | ||
const newColors = _.difference(colorPalette, allColors); | ||
_.merge(this.mapping, _.zipObject(keysToMap, newColors)); | ||
} | ||
} | ||
|
||
return new MappedColors(); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick - kind of inconsistent here in that you use fat arrow syntax in other new code