@superset-ui/color
Advanced tools
Comparing version 0.12.14 to 0.12.15
@@ -5,5 +5,9 @@ function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | ||
/* eslint-disable no-dupe-class-members */ | ||
import { ExtensibleFunction } from '@superset-ui/core'; | ||
import stringifyAndTrim from './stringifyAndTrim'; | ||
export default class CategoricalColorScale extends ExtensibleFunction { | ||
import { scaleOrdinal } from 'd3-scale'; | ||
import stringifyAndTrim from './stringifyAndTrim'; // Use type augmentation to correct the fact that | ||
// an instance of CategoricalScale is also a function | ||
class CategoricalColorScale extends ExtensibleFunction { | ||
/** | ||
@@ -20,2 +24,4 @@ * Constructor | ||
_defineProperty(this, "scale", void 0); | ||
_defineProperty(this, "parentForcedColors", void 0); | ||
@@ -25,8 +31,7 @@ | ||
_defineProperty(this, "seen", void 0); | ||
this.colors = colors; | ||
this.scale = scaleOrdinal(); | ||
this.scale.range(colors); | ||
this.parentForcedColors = parentForcedColors; | ||
this.forcedColors = {}; | ||
this.seen = {}; | ||
} | ||
@@ -48,14 +53,3 @@ | ||
const seenColor = this.seen[cleanedValue]; | ||
const { | ||
length | ||
} = this.colors; | ||
if (seenColor !== undefined) { | ||
return this.colors[seenColor % length]; | ||
} | ||
const index = Object.keys(this.seen).length; | ||
this.seen[cleanedValue] = index; | ||
return this.colors[index % length]; | ||
return this.scale(cleanedValue); | ||
} | ||
@@ -81,11 +75,62 @@ /** | ||
const colorMap = {}; | ||
const { | ||
length | ||
} = this.colors; | ||
Object.keys(this.seen).forEach(value => { | ||
colorMap[value] = this.colors[this.seen[value] % length]; | ||
this.scale.domain().forEach(value => { | ||
colorMap[value.toString()] = this.scale(value); | ||
}); | ||
return _extends({}, colorMap, {}, this.forcedColors, {}, this.parentForcedColors); | ||
} | ||
/** | ||
* Returns an exact copy of this scale. Changes to this scale will not affect the returned scale, and vice versa. | ||
*/ | ||
} | ||
copy() { | ||
const copy = new CategoricalColorScale(this.scale.range(), this.parentForcedColors); | ||
copy.forcedColors = _extends({}, this.forcedColors); | ||
copy.domain(this.domain()); | ||
copy.unknown(this.unknown()); | ||
return copy; | ||
} | ||
/** | ||
* Returns the scale's current domain. | ||
*/ | ||
domain(newDomain) { | ||
if (typeof newDomain === 'undefined') { | ||
return this.scale.domain(); | ||
} | ||
this.scale.domain(newDomain); | ||
return this; | ||
} | ||
/** | ||
* Returns the scale's current range. | ||
*/ | ||
range(newRange) { | ||
if (typeof newRange === 'undefined') { | ||
return this.scale.range(); | ||
} | ||
this.colors = newRange; | ||
this.scale.range(newRange); | ||
return this; | ||
} | ||
/** | ||
* Returns the current unknown value, which defaults to "implicit". | ||
*/ | ||
unknown(value) { | ||
if (typeof value === 'undefined') { | ||
return this.scale.unknown(); | ||
} | ||
this.scale.unknown(value); | ||
return this; | ||
} | ||
} | ||
export default CategoricalColorScale; |
import { ExtensibleFunction } from '@superset-ui/core'; | ||
import { ScaleOrdinal } from 'd3-scale'; | ||
import { ColorsLookup } from './types'; | ||
export default class CategoricalColorScale extends ExtensibleFunction { | ||
interface CategoricalColorScale { | ||
(x: { | ||
toString(): string; | ||
}): string; | ||
} | ||
declare class CategoricalColorScale extends ExtensibleFunction { | ||
colors: string[]; | ||
scale: ScaleOrdinal<{ | ||
toString(): string; | ||
}, string>; | ||
parentForcedColors?: ColorsLookup; | ||
forcedColors: ColorsLookup; | ||
seen: { | ||
[key: string]: number; | ||
}; | ||
/** | ||
@@ -29,3 +35,49 @@ * Constructor | ||
getColorMap(): {}; | ||
/** | ||
* Returns an exact copy of this scale. Changes to this scale will not affect the returned scale, and vice versa. | ||
*/ | ||
copy(): CategoricalColorScale; | ||
/** | ||
* Returns the scale's current domain. | ||
*/ | ||
domain(): { | ||
toString(): string; | ||
}[]; | ||
/** | ||
* Expands the domain to include the specified array of values. | ||
*/ | ||
domain(newDomain: { | ||
toString(): string; | ||
}[]): this; | ||
/** | ||
* Returns the scale's current range. | ||
*/ | ||
range(): string[]; | ||
/** | ||
* Sets the range of the ordinal scale to the specified array of values. | ||
* | ||
* The first element in the domain will be mapped to the first element in range, the second domain value to the second range value, and so on. | ||
* | ||
* If there are fewer elements in the range than in the domain, the scale will reuse values from the start of the range. | ||
* | ||
* @param range Array of range values. | ||
*/ | ||
range(newRange: string[]): this; | ||
/** | ||
* Returns the current unknown value, which defaults to "implicit". | ||
*/ | ||
unknown(): string | { | ||
name: 'implicit'; | ||
}; | ||
/** | ||
* Sets the output value of the scale for unknown input values and returns this scale. | ||
* The implicit value enables implicit domain construction. scaleImplicit can be used as a convenience to set the implicit value. | ||
* | ||
* @param value Unknown value to be used or scaleImplicit to set implicit scale generation. | ||
*/ | ||
unknown(value: string | { | ||
name: 'implicit'; | ||
}): this; | ||
} | ||
export default CategoricalColorScale; | ||
//# sourceMappingURL=CategoricalColorScale.d.ts.map |
@@ -8,2 +8,4 @@ "use strict"; | ||
var _d3Scale = require("d3-scale"); | ||
var _stringifyAndTrim = _interopRequireDefault(require("./stringifyAndTrim")); | ||
@@ -29,2 +31,4 @@ | ||
_defineProperty(this, "scale", void 0); | ||
_defineProperty(this, "parentForcedColors", void 0); | ||
@@ -34,8 +38,7 @@ | ||
_defineProperty(this, "seen", void 0); | ||
this.colors = colors; | ||
this.scale = (0, _d3Scale.scaleOrdinal)(); | ||
this.scale.range(colors); | ||
this.parentForcedColors = parentForcedColors; | ||
this.forcedColors = {}; | ||
this.seen = {}; | ||
} | ||
@@ -57,14 +60,3 @@ | ||
const seenColor = this.seen[cleanedValue]; | ||
const { | ||
length | ||
} = this.colors; | ||
if (seenColor !== undefined) { | ||
return this.colors[seenColor % length]; | ||
} | ||
const index = Object.keys(this.seen).length; | ||
this.seen[cleanedValue] = index; | ||
return this.colors[index % length]; | ||
return this.scale(cleanedValue); | ||
} | ||
@@ -90,13 +82,63 @@ /** | ||
const colorMap = {}; | ||
const { | ||
length | ||
} = this.colors; | ||
Object.keys(this.seen).forEach(value => { | ||
colorMap[value] = this.colors[this.seen[value] % length]; | ||
this.scale.domain().forEach(value => { | ||
colorMap[value.toString()] = this.scale(value); | ||
}); | ||
return _extends({}, colorMap, {}, this.forcedColors, {}, this.parentForcedColors); | ||
} | ||
/** | ||
* Returns an exact copy of this scale. Changes to this scale will not affect the returned scale, and vice versa. | ||
*/ | ||
copy() { | ||
const copy = new CategoricalColorScale(this.scale.range(), this.parentForcedColors); | ||
copy.forcedColors = _extends({}, this.forcedColors); | ||
copy.domain(this.domain()); | ||
copy.unknown(this.unknown()); | ||
return copy; | ||
} | ||
/** | ||
* Returns the scale's current domain. | ||
*/ | ||
domain(newDomain) { | ||
if (typeof newDomain === 'undefined') { | ||
return this.scale.domain(); | ||
} | ||
this.scale.domain(newDomain); | ||
return this; | ||
} | ||
/** | ||
* Returns the scale's current range. | ||
*/ | ||
range(newRange) { | ||
if (typeof newRange === 'undefined') { | ||
return this.scale.range(); | ||
} | ||
this.colors = newRange; | ||
this.scale.range(newRange); | ||
return this; | ||
} | ||
/** | ||
* Returns the current unknown value, which defaults to "implicit". | ||
*/ | ||
unknown(value) { | ||
if (typeof value === 'undefined') { | ||
return this.scale.unknown(); | ||
} | ||
this.scale.unknown(value); | ||
return this; | ||
} | ||
} | ||
exports.default = CategoricalColorScale; | ||
var _default = CategoricalColorScale; | ||
exports.default = _default; |
{ | ||
"name": "@superset-ui/color", | ||
"version": "0.12.14", | ||
"version": "0.12.15", | ||
"description": "Superset UI color", | ||
@@ -35,3 +35,3 @@ "sideEffects": false, | ||
}, | ||
"gitHead": "0f0007d4bede03bbe1732587207388ce4e654adc" | ||
"gitHead": "fe91dbac455f6bc26cc8c74f773fe5e69d5b4ca4" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
69543
1256