-
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.
Merge branch 'sgratzl/color' into sgratzl/sort
# Conflicts: # src/model/BooleansColumn.ts # src/model/CategoricalColumn.ts # src/model/CategoricalsColumn.ts # src/model/OrdinalColumn.ts # src/model/SetColumn.ts # src/model/index.ts # src/provider/utils.ts
- Loading branch information
Showing
18 changed files
with
604 additions
and
44 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
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
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 { | ||
public readonly map: ReadonlyMap<string, string>; | ||
constructor(map: Map<ICategory | string, string>) { | ||
this.map = new Map(Array.from(map.entries()).map(([k, v]) => <[string, string]>[typeof k === 'string' ? k : k.name, v])); | ||
} | ||
|
||
apply(v: ICategory) { | ||
return this.map.has(v.name) ? this.map.get(v.name)! : DEFAULT_COLOR_FUNCTION.apply(v); | ||
} | ||
|
||
dump() { | ||
const r: any = {}; | ||
this.map.forEach((v, k) => r[k] = 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
Oops, something went wrong.