-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from seroanalytics/colors
i7 dynamic colors by variable
- Loading branch information
Showing
17 changed files
with
8,331 additions
and
5,252 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,35 @@ | ||
name: 🔎 Test | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.ref }} | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
lint: | ||
name: 🔎 Test | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: ⬇️ Checkout repo | ||
uses: actions/checkout@v4 | ||
|
||
- name: ⎔ Setup node | ||
uses: actions/setup-node@v4 | ||
|
||
- name: 📥 Install deps | ||
run: npm install --frozen-lockfile | ||
|
||
- name: 🔎 Test | ||
run: npm run test | ||
|
||
- name: Upload coverage | ||
uses: codecov/codecov-action@v4 | ||
with: | ||
fail_ci_if_error: true | ||
env: | ||
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} |
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
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 @@ | ||
declare module 'd3-scale-chromatic'; |
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,39 @@ | ||
type PlotDisplay = "facet" | "trace" | ||
|
||
export interface Dict<T> { | ||
[index: string]: T | ||
} | ||
|
||
export interface PlotOptions { | ||
[index: string]: PlotDisplay | ||
} | ||
|
||
export interface PlotConfig { | ||
key: string | ||
displayName: string | ||
type: string | ||
} | ||
|
||
export interface Model { | ||
key: string | ||
displayName: string | ||
datasets: Dataset[] | ||
regressionModels: Covariate[] | ||
plots: PlotConfig[] | ||
variables: Covariate[] | ||
} | ||
|
||
export interface Dataset { | ||
key: string | ||
displayName: string | ||
} | ||
|
||
export interface Covariate { | ||
displayName: string | ||
key: string | ||
} | ||
|
||
export interface AppState { | ||
models: Model[] | ||
selectedPlotOptions: { [index: string]: PlotOptions } | ||
} |
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,62 @@ | ||
import {interpolateBlues, interpolateOranges, interpolatePurples, interpolateGreens, interpolateReds, interpolateGreys} from "d3-scale-chromatic"; | ||
import {Covariate, Dict} from "~/types"; | ||
|
||
// @ts-expect-error this function doesn't lend itself well to typing | ||
export function permuteArrays(first, next, ...rest) { | ||
if (!first) return []; | ||
if (!next) return first.map(a => [a]); | ||
if (rest.length) next = permuteArrays(next, ...rest); | ||
return first.flatMap(a => next.map(b => [a, b].flat())); | ||
} | ||
|
||
const colorFunctions = [ | ||
interpolateBlues, | ||
interpolateOranges, | ||
interpolatePurples, | ||
interpolateGreens, | ||
interpolateReds, | ||
interpolateGreys | ||
] | ||
|
||
export function addOpacity(color: string) { | ||
return color.replace(')', ', 0.3)').replace('rgb', 'rgba'); | ||
} | ||
|
||
export interface ColorOptions { | ||
line: string | ||
fill: string | ||
} | ||
|
||
export function getColors(traces: Dict<string[]>, | ||
traceVariables: Covariate[], | ||
index: number, | ||
traceDefinition: string[], | ||
numTraces: number): ColorOptions { | ||
if (traceVariables.length == 0) { | ||
const color = colorFunctions[0](0.5); | ||
return {line: color, fill: addOpacity(color)} | ||
} | ||
if (traceVariables.length == 1) { | ||
const colorIndex = index % colorFunctions.length; | ||
const color = colorFunctions[colorIndex](0.2 * (Math.floor(index / colorFunctions.length) + 1)); | ||
return {line: color, fill: addOpacity(color)} | ||
} | ||
if (traceVariables.length == 2) { | ||
const firstTraceVariable = traceDefinition[0]; | ||
const levels = traces[traceVariables[0].key]; | ||
const levelIndex = levels.indexOf(firstTraceVariable); | ||
|
||
const secondTraceVariable = traceDefinition[1]; | ||
const secondLevels = traces[traceVariables[1].key]; | ||
const secondLevelIndex = secondLevels.indexOf(secondTraceVariable); | ||
|
||
const color = colorFunctions[levelIndex]((secondLevelIndex + 1) / secondLevels.length); | ||
return {line: color, fill: addOpacity(color)} | ||
} | ||
if (traceVariables.length > 2) { | ||
// at this point the graph becomes quite unreadable anyway, so just let all traces | ||
// be variations on a color scale | ||
const color = colorFunctions[0]((index + 1) / numTraces) | ||
return {line: color, fill: addOpacity(color)} | ||
} | ||
} |
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,14 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} **/ | ||
export default { | ||
testEnvironment: "node", | ||
transform: { | ||
"^.+.tsx?$": ["ts-jest",{ | ||
diagnostics: false, | ||
}], | ||
}, | ||
moduleNameMapper: { | ||
'^d3-scale-chromatic': '<rootDir>/node_modules/d3-scale-chromatic/dist/d3-scale-chromatic.js', | ||
'^d3-interpolate': '<rootDir>/node_modules/d3-interpolate/dist/d3-interpolate.js', | ||
'^d3-color': '<rootDir>/node_modules/d3-color/dist/d3-color.js', | ||
}, | ||
} |
Oops, something went wrong.