Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix broken color scheme selection by reverting interface change #3111

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/@ourworldindata/grapher/src/color/ColorScale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
BinningStrategy,
Color,
CoreValueType,
colorScaleConfigDefaults,
} from "@ourworldindata/types"
import { CoreColumn } from "@ourworldindata/core-table"

Expand All @@ -44,7 +43,7 @@ export class ColorScale {

@computed get config(): ColorScaleConfigDefaults {
return this.manager.colorScaleConfig
? { ...colorScaleConfigDefaults, ...this.manager.colorScaleConfig }
? this.manager.colorScaleConfig
: new ColorScaleConfig()
}

Expand Down
3 changes: 2 additions & 1 deletion packages/@ourworldindata/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"license": "MIT",
"dependencies": {
"@sinclair/typebox": "^0.28.5"
"@sinclair/typebox": "^0.28.5",
"mobx": "^5.15.7"
}
}
106 changes: 90 additions & 16 deletions packages/@ourworldindata/types/src/grapherTypes/GrapherTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ColumnSlugs, EntityName } from "../domainTypes/CoreTableTypes.js"
import { AxisAlign, Position } from "../domainTypes/Layout.js"
import { Integer, QueryParams, TopicId } from "../domainTypes/Various.js"
import { DetailDictionary } from "../gdocTypes/Gdoc.js"
import { observable } from "mobx"

export interface Box {
x: number
Expand Down Expand Up @@ -292,22 +293,95 @@ export enum BinningStrategy {
manual = "manual",
}

export interface ColorScaleConfigInterface {
baseColorScheme?: ColorSchemeName
colorSchemeInvert?: boolean
binningStrategy?: BinningStrategy
binningStrategyBinCount?: number
customNumericMinValue?: number
customNumericValues: number[]
customNumericLabels: (string | undefined | null)[]
customNumericColorsActive?: boolean
customNumericColors: (Color | undefined | null)[]
equalSizeBins?: boolean
customCategoryColors: Record<string, string | undefined>
customCategoryLabels: Record<string, string | undefined>
customHiddenCategories: Record<string, true | undefined>
legendDescription?: string
}
export class ColorScaleConfigDefaults {
// Color scheme
// ============

/** Key for a colorbrewer scheme */
@observable baseColorScheme?: ColorSchemeName

/** Reverse the order of colors in the color scheme (defined by `baseColorScheme`) */
@observable colorSchemeInvert?: boolean = undefined

// Numeric bins
// ============

/** The strategy for generating the bin boundaries */
@observable binningStrategy: BinningStrategy = BinningStrategy.ckmeans
/** The *suggested* number of bins for the automatic binning algorithm */
@observable binningStrategyBinCount?: number

/** The minimum bracket of the first bin */
@observable customNumericMinValue?: number
/** Custom maximum brackets for each numeric bin. Only applied when strategy is `manual`. */
@observable customNumericValues: number[] = []
/**
* Custom labels for each numeric bin. Only applied when strategy is `manual`.
* `undefined` or `null` falls back to default label.
* We need to handle `null` because JSON serializes `undefined` values
* inside arrays into `null`.
*/
@observable customNumericLabels: (string | undefined | null)[] = []

/** Whether `customNumericColors` are used to override the color scheme. */
@observable customNumericColorsActive?: boolean = undefined
/**
* Override some or all colors for the numerical color legend.
* `undefined` or `null` falls back the color scheme color.
* We need to handle `null` because JSON serializes `undefined` values
* inside arrays into `null`.
*/
@observable customNumericColors: (Color | undefined | null)[] = []

/** Whether the visual scaling for the color legend is disabled. */
@observable equalSizeBins?: boolean = true

// Categorical bins
// ================

@observable.ref customCategoryColors: {
[key: string]: string | undefined
} = {}

@observable.ref customCategoryLabels: {
[key: string]: string | undefined
} = {}

// Allow hiding categories from the legend
@observable.ref customHiddenCategories: {
[key: string]: true | undefined
} = {}

// Other
// =====

/** A custom legend description. Only used in ScatterPlot legend titles for now. */
@observable legendDescription?: string = undefined
}

// TODO: It would be nice to replace the type definition below with
// the commented out version below and remove mobx as a depdency on the
// types project - but for some reason the implementation in grapher/src/color/ColorScale.ts
// of the config getter didn't like that change and the drop downs in the admin
// for the base color scheme stopped working. To try again some time.
export type ColorScaleConfigInterface = ColorScaleConfigDefaults

// export interface ColorScaleConfigInterface {
// baseColorScheme?: ColorSchemeName
// colorSchemeInvert?: boolean
// binningStrategy?: BinningStrategy
// binningStrategyBinCount?: number
// customNumericMinValue?: number
// customNumericValues: number[]
// customNumericLabels: (string | undefined | null)[]
// customNumericColorsActive?: boolean
// customNumericColors: (Color | undefined | null)[]
// equalSizeBins?: boolean
// customCategoryColors: Record<string, string | undefined>
// customCategoryLabels: Record<string, string | undefined>
// customHiddenCategories: Record<string, true | undefined>
// legendDescription?: string
// }

export const colorScaleConfigDefaults = {
binningStrategy: BinningStrategy.ckmeans,
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3714,6 +3714,7 @@ __metadata:
eslint: "npm:^8.53.0"
eslint-plugin-react-hooks: "npm:^4.6.0"
jest: "npm:^29.7.0"
mobx: "npm:^5.15.7"
typescript: "npm:~5.2.2"
languageName: unknown
linkType: soft
Expand Down
Loading