Skip to content

Commit

Permalink
export (color) scales
Browse files Browse the repository at this point in the history
  • Loading branch information
sgratzl committed Jan 17, 2019
1 parent ae314af commit a22edd2
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 42 deletions.
6 changes: 3 additions & 3 deletions src/model/BooleanColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ValueColumn, {dataLoaded} from './ValueColumn';
import {ICategoricalColumn, ICategory, ICategoricalColorMappingFunction} from './ICategoricalColumn';
import {IDataRow, ECompareValueType, IValueColumnDesc} from './interfaces';
import {IEventListener} from '../internal';
import {restoreColorMapping, DEFAULT_COLOR_FUNCTION} from './CategoricalColorMappingFunction';
import {restoreCategoricalColorMapping, DEFAULT_CATEGORICAL_COLOR_FUNCTION} from './CategoricalColorMappingFunction';

export interface IBooleanDesc {
/**
Expand Down Expand Up @@ -70,7 +70,7 @@ export default class BooleanColumn extends ValueColumn<boolean> implements ICate
value: 1
}
];
this.colorMapping = DEFAULT_COLOR_FUNCTION;
this.colorMapping = DEFAULT_CATEGORICAL_COLOR_FUNCTION;
}

protected createEventList() {
Expand Down Expand Up @@ -168,7 +168,7 @@ export default class BooleanColumn extends ValueColumn<boolean> implements ICate

restore(dump: any, factory: (dump: any) => Column | null) {
super.restore(dump, factory);
this.colorMapping = restoreColorMapping(dump.colorMapping, this.categories);
this.colorMapping = restoreCategoricalColorMapping(dump.colorMapping, this.categories);
if (typeof dump.filter !== 'undefined') {
this.currentFilter = dump.filter;
}
Expand Down
6 changes: 3 additions & 3 deletions src/model/BooleansColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ArrayColumn, {IArrayColumnDesc} from './ArrayColumn';
import {ISetColumn, ICategoricalColorMappingFunction} from './ICategoricalColumn';
import {IDataRow, DEFAULT_COLOR} from './interfaces';
import CategoricalColumn from './CategoricalColumn';
import {DEFAULT_COLOR_FUNCTION, restoreColorMapping} from './CategoricalColorMappingFunction';
import {DEFAULT_CATEGORICAL_COLOR_FUNCTION, restoreCategoricalColorMapping} from './CategoricalColorMappingFunction';
import ValueColumn, {dataLoaded} from './ValueColumn';
import Column, {labelChanged, metaDataChanged, dirty, dirtyHeader, dirtyValues, rendererTypeChanged, groupRendererChanged, summaryRendererChanged, visibilityChanged, widthChanged, dirtyCaches} from './Column';
import {IEventListener} from '../internal';
Expand All @@ -28,7 +28,7 @@ export default class BooleansColumn extends ArrayColumn<boolean> implements ISet
constructor(id: string, desc: Readonly<IBooleansColumnDesc>) {
super(id, desc);
this.setDefaultRenderer('upset');
this.colorMapping = DEFAULT_COLOR_FUNCTION;
this.colorMapping = DEFAULT_CATEGORICAL_COLOR_FUNCTION;
}

get categories() {
Expand Down Expand Up @@ -106,6 +106,6 @@ export default class BooleansColumn extends ArrayColumn<boolean> implements ISet

restore(dump: any, factory: (dump: any) => Column | null) {
super.restore(dump, factory);
this.colorMapping = restoreColorMapping(dump.colorMapping, this.categories);
this.colorMapping = restoreCategoricalColorMapping(dump.colorMapping, this.categories);
}
}
16 changes: 7 additions & 9 deletions src/model/CategoricalColorMappingFunction.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import {ICategory, ICategoricalColorMappingFunction} from '.';
import {DEFAULT_COLOR} from './interfaces';

/**
* @internal
*/
export const DEFAULT_COLOR_FUNCTION: ICategoricalColorMappingFunction = {

export const DEFAULT_CATEGORICAL_COLOR_FUNCTION: ICategoricalColorMappingFunction = {
apply: (v) => v ? v.color : DEFAULT_COLOR,
dump: () => null,
clone: () => DEFAULT_COLOR_FUNCTION,
eq: (other) => other === DEFAULT_COLOR_FUNCTION
clone: () => DEFAULT_CATEGORICAL_COLOR_FUNCTION,
eq: (other) => other === DEFAULT_CATEGORICAL_COLOR_FUNCTION
};

export class ReplacmentColorMappingFunction implements ICategoricalColorMappingFunction {
Expand All @@ -18,7 +16,7 @@ export class ReplacmentColorMappingFunction implements ICategoricalColorMappingF
}

apply(v: ICategory) {
return this.map.has(v.name) ? this.map.get(v.name)! : DEFAULT_COLOR_FUNCTION.apply(v);
return this.map.has(v.name) ? this.map.get(v.name)! : DEFAULT_CATEGORICAL_COLOR_FUNCTION.apply(v);
}

dump() {
Expand Down Expand Up @@ -56,9 +54,9 @@ export class ReplacmentColorMappingFunction implements ICategoricalColorMappingF
/**
* @internal
*/
export function restoreColorMapping(dump: any, categories: ICategory[]): ICategoricalColorMappingFunction {
export function restoreCategoricalColorMapping(dump: any, categories: ICategory[]): ICategoricalColorMappingFunction {
if (!dump) {
return DEFAULT_COLOR_FUNCTION;
return DEFAULT_CATEGORICAL_COLOR_FUNCTION;
}
return ReplacmentColorMappingFunction.restore(dump, categories);
}
6 changes: 3 additions & 3 deletions src/model/CategoricalColumn.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {IEventListener, ISequence} from '../internal';
import {Category, toolbar} from './annotations';
import {DEFAULT_COLOR_FUNCTION, restoreColorMapping} from './CategoricalColorMappingFunction';
import {DEFAULT_CATEGORICAL_COLOR_FUNCTION, restoreCategoricalColorMapping} from './CategoricalColorMappingFunction';
import Column, {dirty, dirtyCaches, dirtyHeader, dirtyValues, groupRendererChanged, labelChanged, metaDataChanged, rendererTypeChanged, summaryRendererChanged, visibilityChanged, widthChanged} from './Column';
import {ICategoricalColumn, ICategoricalColumnDesc, ICategoricalFilter, ICategory, ICategoricalColorMappingFunction} from './ICategoricalColumn';
import {IDataRow, IGroup, ICompareValue, DEFAULT_COLOR} from './interfaces';
Expand Down Expand Up @@ -49,7 +49,7 @@ export default class CategoricalColumn extends ValueColumn<string> implements IC
super(id, desc);
this.categories = toCategories(desc);
this.categories.forEach((d) => this.lookup.set(d.name, d));
this.colorMapping = DEFAULT_COLOR_FUNCTION;
this.colorMapping = DEFAULT_CATEGORICAL_COLOR_FUNCTION;
}

protected createEventList() {
Expand Down Expand Up @@ -149,7 +149,7 @@ export default class CategoricalColumn extends ValueColumn<string> implements IC
restore(dump: any, factory: (dump: any) => Column | null) {
super.restore(dump, factory);

this.colorMapping = restoreColorMapping(dump.colorMapping, this.categories);
this.colorMapping = restoreCategoricalColorMapping(dump.colorMapping, this.categories);

if ('filter' in dump) {
this.currentFilter = null;
Expand Down
6 changes: 3 additions & 3 deletions src/model/CategoricalMapColumn.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {ICategoricalDesc, ICategory, ICategoricalLikeColumn, ICategoricalColorMappingFunction} from './ICategoricalColumn';
import {IDataRow, DEFAULT_COLOR} from './interfaces';
import MapColumn, {IMapColumnDesc} from './MapColumn';
import {DEFAULT_COLOR_FUNCTION, restoreColorMapping} from './CategoricalColorMappingFunction';
import {DEFAULT_CATEGORICAL_COLOR_FUNCTION, restoreCategoricalColorMapping} from './CategoricalColorMappingFunction';
import CategoricalColumn from './CategoricalColumn';
import ValueColumn, {dataLoaded} from './ValueColumn';
import Column, {labelChanged, metaDataChanged, dirty, dirtyHeader, dirtyValues, rendererTypeChanged, groupRendererChanged, summaryRendererChanged, visibilityChanged, widthChanged, dirtyCaches} from './Column';
Expand Down Expand Up @@ -33,7 +33,7 @@ export default class CategoricalMapColumn extends MapColumn<string | null> imple
super(id, desc);
this.categories = toCategories(desc);
this.categories.forEach((d) => this.lookup.set(d.name, d));
this.colorMapping = DEFAULT_COLOR_FUNCTION;
this.colorMapping = DEFAULT_CATEGORICAL_COLOR_FUNCTION;
}
protected createEventList() {
return super.createEventList().concat([CategoricalMapColumn.EVENT_COLOR_MAPPING_CHANGED]);
Expand Down Expand Up @@ -112,7 +112,7 @@ export default class CategoricalMapColumn extends MapColumn<string | null> imple

restore(dump: any, factory: (dump: any) => Column | null) {
super.restore(dump, factory);
this.colorMapping = restoreColorMapping(dump.colorMapping, this.categories);
this.colorMapping = restoreCategoricalColorMapping(dump.colorMapping, this.categories);
}

iterCategory(row: IDataRow) {
Expand Down
6 changes: 3 additions & 3 deletions src/model/CategoricalsColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {ICategoricalDesc, ICategory, ICategoricalColorMappingFunction, ICategori
import {IDataRow, DEFAULT_COLOR} from './interfaces';
import {toolbar} from './annotations';
import CategoricalColumn from './CategoricalColumn';
import {DEFAULT_COLOR_FUNCTION, restoreColorMapping} from './CategoricalColorMappingFunction';
import {DEFAULT_CATEGORICAL_COLOR_FUNCTION, restoreCategoricalColorMapping} from './CategoricalColorMappingFunction';
import ValueColumn, {dataLoaded} from './ValueColumn';
import Column, {labelChanged, metaDataChanged, dirty, dirtyHeader, dirtyValues, rendererTypeChanged, groupRendererChanged, summaryRendererChanged, visibilityChanged, widthChanged, dirtyCaches} from './Column';
import {IEventListener} from '../internal';
Expand Down Expand Up @@ -35,7 +35,7 @@ export default class CategoricalsColumn extends ArrayColumn<string | null> imple
super(id, desc);
this.categories = toCategories(desc);
this.categories.forEach((d) => this.lookup.set(d.name, d));
this.colorMapping = DEFAULT_COLOR_FUNCTION;
this.colorMapping = DEFAULT_CATEGORICAL_COLOR_FUNCTION;
}

protected createEventList() {
Expand Down Expand Up @@ -102,6 +102,6 @@ export default class CategoricalsColumn extends ArrayColumn<string | null> imple

restore(dump: any, factory: (dump: any) => Column | null) {
super.restore(dump, factory);
this.colorMapping = restoreColorMapping(dump.colorMapping, this.categories);
this.colorMapping = restoreCategoricalColorMapping(dump.colorMapping, this.categories);
}
}
7 changes: 0 additions & 7 deletions src/model/ColorMappingFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {scaleLinear} from 'd3-scale';
import {IInterpolateColorMappingFunction, IColorMappingFunction, ISolidColorMappingFunction, IQuantizedColorMappingFunction, ICustomColorMappingFunction, IMapAbleDesc} from '.';
import {DEFAULT_COLOR} from './interfaces';

/** @internal */
export class InterpolatingColorFunction implements IInterpolateColorMappingFunction {
constructor(public readonly name: string, public readonly type: 'sequential'|'divergent', public readonly apply: (v: number)=>string) {

Expand All @@ -23,7 +22,6 @@ export class InterpolatingColorFunction implements IInterpolateColorMappingFunct
}
}

/** @internal */
export class SolidColorFunction implements ISolidColorMappingFunction {
constructor(public readonly color: string) {

Expand All @@ -50,7 +48,6 @@ export class SolidColorFunction implements ISolidColorMappingFunction {
}
}

/** @internal */
export class QuantizedColorFunction implements IQuantizedColorMappingFunction {
constructor(public readonly base: IColorMappingFunction, public readonly steps: number) {

Expand Down Expand Up @@ -80,7 +77,6 @@ export class QuantizedColorFunction implements IQuantizedColorMappingFunction {
}
}

/** @internal */
export class CustomColorMappingFunction implements ICustomColorMappingFunction {
private readonly scale = scaleLinear<string>();

Expand Down Expand Up @@ -145,9 +141,6 @@ export function asColorFunction(color: string) {
return s;
}

/**
* @internal
*/
export const DEFAULT_COLOR_FUNCTION = asColorFunction(DEFAULT_COLOR);

/**
Expand Down
6 changes: 3 additions & 3 deletions src/model/HierarchyColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {colorPool} from './internal';
import {missingGroup} from './missing';
import ValueColumn, {dataLoaded} from './ValueColumn';
import {IEventListener} from '../internal';
import {restoreColorMapping, DEFAULT_COLOR_FUNCTION} from './CategoricalColorMappingFunction';
import {restoreCategoricalColorMapping, DEFAULT_CATEGORICAL_COLOR_FUNCTION} from './CategoricalColorMappingFunction';

export interface ICategoryNode extends ICategory {
children: Readonly<ICategoryNode>[];
Expand Down Expand Up @@ -78,7 +78,7 @@ export default class HierarchyColumn extends ValueColumn<string> implements ICat
this.updateCaches();

this.setDefaultRenderer('categorical');
this.colorMapping = DEFAULT_COLOR_FUNCTION;
this.colorMapping = DEFAULT_CATEGORICAL_COLOR_FUNCTION;
}

private initHierarchy(root: IPartialCategoryNode) {
Expand Down Expand Up @@ -153,7 +153,7 @@ export default class HierarchyColumn extends ValueColumn<string> implements ICat

restore(dump: any, factory: (dump: any) => Column | null) {
super.restore(dump, factory);
this.colorMapping = restoreColorMapping(dump.colorMapping, this.categories);
this.colorMapping = restoreCategoricalColorMapping(dump.colorMapping, this.categories);
if (typeof dump.maxDepth !== 'undefined') {
this.currentMaxDepth = dump.maxDepth;
}
Expand Down
3 changes: 3 additions & 0 deletions src/model/MappingFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ export class ScriptMappingFunction implements IMappingFunction {
}
}

/**
* @internal
*/
export function createMappingFunction(dump: any): IMappingFunction {
if (dump.type === 'script') {
const s = new ScriptMappingFunction();
Expand Down
4 changes: 2 additions & 2 deletions src/model/OrdinalColumn.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {equalArrays, extent, IEventListener} from '../internal';
import {Category, toolbar} from './annotations';
import {DEFAULT_COLOR_FUNCTION} from './CategoricalColorMappingFunction';
import {DEFAULT_CATEGORICAL_COLOR_FUNCTION} from './CategoricalColorMappingFunction';
import CategoricalColumn from './CategoricalColumn';
import Column, {dirty, dirtyCaches, dirtyHeader, dirtyValues, groupRendererChanged, labelChanged, metaDataChanged, rendererTypeChanged, summaryRendererChanged, visibilityChanged, widthChanged} from './Column';
import {ICategoricalColumn, ICategoricalDesc, ICategoricalFilter, ICategory, ICategoricalColorMappingFunction} from './ICategoricalColumn';
Expand Down Expand Up @@ -59,7 +59,7 @@ export default class OrdinalColumn extends ValueColumn<number> implements INumbe
this.categories.forEach((d) => this.lookup.set(d.name, d));
this.setDefaultRenderer('number');
this.setDefaultGroupRenderer('boxplot');
this.colorMapping = DEFAULT_COLOR_FUNCTION;
this.colorMapping = DEFAULT_CATEGORICAL_COLOR_FUNCTION;
}

protected createEventList() {
Expand Down
6 changes: 3 additions & 3 deletions src/model/SetColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {ICategoricalDesc, ICategoricalFilter, ICategory, ISetColumn, ICategorica
import {IDataRow, ECompareValueType, IValueColumnDesc, IGroup, DEFAULT_COLOR} from './interfaces';
import ValueColumn, {dataLoaded} from './ValueColumn';
import {IEventListener} from '../internal';
import {DEFAULT_COLOR_FUNCTION, restoreColorMapping} from './CategoricalColorMappingFunction';
import {DEFAULT_CATEGORICAL_COLOR_FUNCTION, restoreCategoricalColorMapping} from './CategoricalColorMappingFunction';
import {toCategories, isCategoryIncluded} from './internalCategorical';
import {chooseUIntByDataLength} from './internal';

Expand Down Expand Up @@ -62,7 +62,7 @@ export default class SetColumn extends ValueColumn<string[]> implements IArrayCo
this.setDefaultRenderer('upset');
this.setDefaultGroupRenderer('upset');
this.setSummaryRenderer('set');
this.colorMapping = DEFAULT_COLOR_FUNCTION;
this.colorMapping = DEFAULT_CATEGORICAL_COLOR_FUNCTION;
}

protected createEventList() {
Expand Down Expand Up @@ -188,7 +188,7 @@ export default class SetColumn extends ValueColumn<string[]> implements IArrayCo

restore(dump: any, factory: (dump: any) => Column | null) {
super.restore(dump, factory);
this.colorMapping = restoreColorMapping(dump.colorMapping, this.categories);
this.colorMapping = restoreCategoricalColorMapping(dump.colorMapping, this.categories);
if (!('filter' in dump)) {
this.currentFilter = null;
return;
Expand Down
4 changes: 4 additions & 0 deletions src/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export * from './INumberColumn';
export * from './IDateColumn';
export * from './IArrayColumn';

export {ScaleMappingFunction, ScriptMappingFunction} from './MappingFunction';
export {DEFAULT_CATEGORICAL_COLOR_FUNCTION, ReplacmentColorMappingFunction} from './CategoricalColorMappingFunction';
export {CustomColorMappingFunction, DEFAULT_COLOR_FUNCTION, InterpolatingColorFunction, QuantizedColorFunction, SolidColorFunction} from './ColorMappingFunction';

export {default as ActionColumn} from './ActionColumn';
export * from './ActionColumn';
export {default as AggregateGroupColumn} from './AggregateGroupColumn';
Expand Down
6 changes: 3 additions & 3 deletions src/ui/dialogs/CategoricalColorMappingDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {uniqueId} from './utils';
import {cssClass} from '../../styles';
import {color} from 'd3-color';
import {schemeCategory10, schemeAccent, schemeDark2, schemePastel1, schemePastel2, schemeSet1, schemeSet2, schemeSet3} from 'd3-scale-chromatic';
import {DEFAULT_COLOR_FUNCTION, ReplacmentColorMappingFunction} from '../../model/CategoricalColorMappingFunction';
import {DEFAULT_CATEGORICAL_COLOR_FUNCTION, ReplacmentColorMappingFunction} from '../../model/CategoricalColorMappingFunction';

const sets: {[key: string]: ReadonlyArray<string>} = {schemeCategory10, schemeAccent, schemeDark2, schemePastel1, schemePastel2, schemeSet1, schemeSet2, schemeSet3};

Expand Down Expand Up @@ -57,7 +57,7 @@ export default class CategoricalColorMappingDialog extends ADialog {
this.forEach('[data-cat]', (n: HTMLInputElement, i) => {
n.value = color(cats[i]!.color)!.hex();
});
this.column.setColorMapping(DEFAULT_COLOR_FUNCTION);
this.column.setColorMapping(DEFAULT_CATEGORICAL_COLOR_FUNCTION);
}

submit() {
Expand All @@ -70,7 +70,7 @@ export default class CategoricalColorMappingDialog extends ADialog {
}
});
if (map.size === 0) {
this.column.setColorMapping(DEFAULT_COLOR_FUNCTION);
this.column.setColorMapping(DEFAULT_CATEGORICAL_COLOR_FUNCTION);
} else {
this.column.setColorMapping(new ReplacmentColorMappingFunction(map));
}
Expand Down

0 comments on commit a22edd2

Please sign in to comment.