-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implementing oridinal colorscaling (#171)
* 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
Showing
2 changed files
with
62 additions
and
0 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,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; | ||
} | ||
} |
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,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", | ||
], | ||
}; |