Skip to content

Commit

Permalink
feat: implementing oridinal colorscaling (#171)
Browse files Browse the repository at this point in the history
* feat: implementing oridinal colorscaling

* Rewrite ScaleOridinal to fix typing issues

* fix: make `schemeCategorical` start with lower letter

* fix: rename `scale` to `valueAt`
  • Loading branch information
onc authored Jun 2, 2022
1 parent 20df944 commit 7e9d0a3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
36 changes: 36 additions & 0 deletions packages/missing-coordinates/src/color/scaleOrdinal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export default class ScaleOrdinal {
// mapping from domain values to index.
private domainIndex = new Map();
// domain of possible values
private domainItems: string[] = [];
// range to be projected to
private rangeItems: string[] = [];

public valueAt(value: string | number): string | null {
const index = this.domainIndex.get(value.toString());
if (index === undefined) {
return null;
}
return this.rangeItems[index % this.rangeItems.length];
}

public domain(newDomain: string[] | number[]): ScaleOrdinal {
this.domainItems = [];
this.domainIndex = new Map();

newDomain.forEach((value) => {
const stringValue = value.toString();
if (!this.domainIndex.has(stringValue)) {
this.domainIndex.set(stringValue, this.domainItems.length);
this.domainItems.push(stringValue);
}
});

return this;
}

public range(newRange: string[]): ScaleOrdinal {
this.rangeItems = newRange;
return this;
}
}
26 changes: 26 additions & 0 deletions packages/missing-coordinates/src/color/schemes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const schemeCategorical = {
Category10: [
"#1F77B4",
"#FF7F0E",
"#2CA02C",
"#D62728",
"#9467BD",
"#8C564B",
"#E377C2",
"#7F7F7F",
"#BCBD22",
"#17BECF",
],
Tableau10: [
"#4E79A7",
"#F28E2C",
"#E15759",
"#76B7B2",
"#59A14F",
"#EDC949",
"#AF7AA1",
"#FF9DA7",
"#9C755F",
"#BAB0AB",
],
};

0 comments on commit 7e9d0a3

Please # to comment.