-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
316 additions
and
27 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import {ICategory} from './ICategoricalColumn'; | ||
import Column from './Column'; | ||
|
||
|
||
export interface ICategoricalColorMappingFunction { | ||
apply(v: ICategory): string; | ||
|
||
dump(): any; | ||
|
||
clone(): ICategoricalColorMappingFunction; | ||
|
||
eq(other: ICategoricalColorMappingFunction): boolean; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const DEFAULT_COLOR_FUNCTION: ICategoricalColorMappingFunction = { | ||
apply: (v) => v ? v.color : Column.DEFAULT_COLOR, | ||
dump: () => null, | ||
clone: () => DEFAULT_COLOR_FUNCTION, | ||
eq: (other) => other === DEFAULT_COLOR_FUNCTION | ||
}; | ||
|
||
export class ReplacmentColorMappingFunction implements ICategoricalColorMappingFunction { | ||
constructor(private readonly map: Map<ICategory, string>) { | ||
// nothing to do | ||
} | ||
|
||
apply(v: ICategory) { | ||
return this.map.has(v) ? this.map.get(v)! : DEFAULT_COLOR_FUNCTION.apply(v); | ||
} | ||
|
||
dump() { | ||
const r: any = {}; | ||
this.map.forEach((v, k) => r[k.name] = v); | ||
return r; | ||
} | ||
|
||
clone() { | ||
return new ReplacmentColorMappingFunction(new Map(this.map.entries())); | ||
} | ||
|
||
eq(other: ICategoricalColorMappingFunction): boolean { | ||
if (!(other instanceof ReplacmentColorMappingFunction)) { | ||
return false; | ||
} | ||
if (other.map.size !== this.map.size) { | ||
return false; | ||
} | ||
return Array.from(this.map.keys()).every((k) => this.map.get(k) === other.map.get(k)); | ||
} | ||
|
||
static restore(dump: any, categories: ICategory[]) { | ||
const lookup = new Map(categories.map((d) => <[string, ICategory]>[d.name, d])); | ||
const r = new Map<ICategory, string>(); | ||
for (const key of Object.keys(dump)) { | ||
if (lookup.has(key)) { | ||
r.set(lookup.get(key)!, dump[key]); | ||
} | ||
} | ||
return new ReplacmentColorMappingFunction(r); | ||
} | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function restoreColorMapping(dump: any, categories: ICategory[]): ICategoricalColorMappingFunction { | ||
if (!dump) { | ||
return DEFAULT_COLOR_FUNCTION; | ||
} | ||
return ReplacmentColorMappingFunction.restore(dump, categories); | ||
} |
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
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
Oops, something went wrong.