Skip to content

Commit

Permalink
🔨 let charts inherit defaults from a root config
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann committed Jul 9, 2024
1 parent 94680ac commit 5ffb9ff
Show file tree
Hide file tree
Showing 8 changed files with 553 additions and 10 deletions.
162 changes: 162 additions & 0 deletions db/migration/1720449698768-AddDefaultChartConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import { MigrationInterface, QueryRunner } from "typeorm"
import {
diffGrapherConfigs,
GrapherInterface,
mergeGrapherConfigs,
} from "@ourworldindata/utils"

const uuid = "d4fd6977-3dc1-11ef-8ef2-0242ac120002"

const defaultConfig = {
$schema: "https://files.ourworldindata.org/schemas/grapher-schema.004.json",
version: 1,
isPublished: false,
type: "LineChart",
logo: "owid",
tab: "chart",
hasChartTab: true,
hasMapTab: false,
minTime: "earliest",
maxTime: "latest",
xAxis: {
scaleType: "linear",
canChangeScaleType: false,
facetDomain: "shared",
removePointsOutsideDomain: false,
},
yAxis: {
scaleType: "linear",
canChangeScaleType: false,
facetDomain: "shared",
removePointsOutsideDomain: false,
},
invertColorScheme: false,
colorScale: {
colorSchemeInvert: false,
binningStrategy: "ckmeans",
binningStrategyBinCount: 5,
equalSizeBins: true,
customNumericColorsActive: false,
},
addCountryMode: "add-country",
entityType: "country or region",
entityTypePlural: "countries",
matchingEntitiesOnly: false,
missingDataStrategy: "auto",
selectedFacetStrategy: "none",
facettingLabelByYVariables: "metric",
stackMode: "absolute",
sortBy: "total",
sortOrder: "desc",
scatterPointLabelStrategy: "year",
compareEndPointsOnly: false,
zoomToSelection: false,
showYearLabels: false,
showNoDataArea: true,
hideLegend: false,
hideLogo: false,
hideTimeline: false,
hideRelativeToggle: true,
hideConnectedScatterLines: false,
hideLinesOutsideTolerance: false,
hideTotalValueLabel: false,
hideScatterLabels: false,
hideFacetControl: true,
hideAnnotationFieldsInTitle: {
entity: false,
time: false,
changeInPrefix: false,
},
map: {
projection: "World",
time: "latest",
timeTolerance: 0,
toleranceStrategy: "closest",
colorScale: {
colorSchemeInvert: false,
binningStrategy: "ckmeans",
binningStrategyBinCount: 5,
equalSizeBins: true,
customNumericColorsActive: false,
},
tooltipUseCustomLabels: false,
hideTimeline: false,
},
} as GrapherInterface

export class AddDefaultChartConfig1720449698768 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const defaultConfigJson = JSON.stringify(defaultConfig)

// add default chart config as a new row in the chart_configs table
await queryRunner.query(
`-- sql
INSERT INTO chart_configs (id, patchConfig, config) VALUES (UUID_TO_BIN(?, 1), ?, ?)
`,
[uuid, defaultConfigJson, defaultConfigJson]
)

const charts = (await queryRunner.query(
`-- sql
SELECT id, uuid, config FROM chart_configs
WHERE uuid != ?
`,
[uuid]
)) as { id: string; uuid: string; config: string }[]

for (const chart of charts) {
const originalConfig = JSON.parse(chart.config)

// if the schema is missing, we assume it's the current one
if (!originalConfig["$schema"]) {
originalConfig["$schema"] =
"https://files.ourworldindata.org/schemas/grapher-schema.004.json"
}

const patchConfig = diffGrapherConfigs(
originalConfig,
defaultConfig
)
const fullConfig = mergeGrapherConfigs(
defaultConfig,
originalConfig
)

await queryRunner.query(
`-- sql
UPDATE chart_configs
SET
patchConfig = ?,
config = ?
WHERE id = ?
`,
[
JSON.stringify(patchConfig),
JSON.stringify(fullConfig),
chart.id,
]
)
}
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`-- sql
DELETE FROM chart_configs
WHERE uuid = ?
`,
[uuid]
)

// recover the original configs from the charts table
await queryRunner.query(
`-- sql
UPDATE chart_configs cc
JOIN charts c
SET
cc.patchConfig = c.config,
cc.config = c.config
`
)
}
}
3 changes: 1 addition & 2 deletions explorer/GrapherGrammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,7 @@ export const GrapherGrammar: Grammar = {
hideRelativeToggle: {
...BooleanCellDef,
keyword: "hideRelativeToggle",
description:
"Whether to hide the relative mode UI toggle. Default depends on the chart type.",
description: "Whether to hide the relative mode UI toggle",
},
timelineMinTime: {
...IntegerCellDef,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ $defs:
baseColorScheme:
type: string
description: One of the predefined base color schemes
default: default
enum:
- YlGn
- YlGnBu
Expand Down Expand Up @@ -259,7 +258,9 @@ properties:
- earliest
columnSlug:
# TODO: remove this once we have a convention of using the first y dimension instead
description: "Column to show in the map tab. Can be a column slug (e.g. in explorers) or a variable ID (as string)."
description: |
Column to show in the map tab. Can be a column slug (e.g. in explorers) or a variable ID (as string).
If not provided, the first y dimension is used.
type: string
additionalProperties: false
maxTime:
Expand All @@ -282,7 +283,6 @@ properties:
- string
baseColorScheme:
type: string
default: default
description: The default color scheme if no color overrides are specified
yAxis:
$ref: "#/$defs/axis"
Expand Down Expand Up @@ -357,7 +357,8 @@ properties:
description: Reverse the order of colors in the color scheme
hideRelativeToggle:
type: boolean
description: Whether to hide the relative mode UI toggle. Default depends on the chart type
default: true
description: Whether to hide the relative mode UI toggle
comparisonLines:
description: List of vertical comparison lines to draw
type: array
Expand Down Expand Up @@ -491,13 +492,16 @@ properties:
type: integer
description: Number of decimal places to show
minimum: 0
default: 2
numSignificantFigures:
type: integer
description: Number of significant figures to show
minimum: 1
default: 3
zeroDay:
type: string
description: Iso date day string for the starting date if yearIsDay is used
default: "2020-01-21"
additionalProperties: false
variableId:
type: integer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,12 +509,12 @@ export enum MapProjectionName {

export interface MapConfigInterface {
columnSlug?: ColumnSlug
time?: Time
time?: Time | "earliest" | "latest"
timeTolerance?: number
toleranceStrategy?: ToleranceStrategy
hideTimeline?: boolean
projection?: MapProjectionName
colorScale?: ColorScaleConfigInterface
colorScale?: Partial<ColorScaleConfigInterface>
tooltipUseCustomLabels?: boolean
}

Expand All @@ -532,8 +532,8 @@ export interface GrapherInterface extends SortConfig {
sourceDesc?: string
note?: string
hideAnnotationFieldsInTitle?: AnnotationFieldsInTitle
minTime?: TimeBound
maxTime?: TimeBound
minTime?: TimeBound | "earliest" | "latest"
maxTime?: TimeBound | "earliest" | "latest"
timelineMinTime?: Time
timelineMaxTime?: Time
dimensions?: OwidChartDimensionInterface[]
Expand Down
4 changes: 4 additions & 0 deletions packages/@ourworldindata/utils/src/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
maxBy,
memoize,
merge,
mergeWith,
min,
minBy,
noop,
Expand Down Expand Up @@ -102,6 +103,7 @@ export {
isNil,
isNull,
isNumber,
isPlainObject,
isString,
isUndefined,
keyBy,
Expand All @@ -110,6 +112,7 @@ export {
maxBy,
memoize,
merge,
mergeWith,
min,
minBy,
noop,
Expand Down Expand Up @@ -1748,6 +1751,7 @@ export function filterValidStringValues<ValidValue extends string>(
}

// TODO: type this correctly once we have moved types into their own top level package
// TODO: remove in favour of the new function
export function mergePartialGrapherConfigs<T extends Record<string, any>>(
...grapherConfigs: (T | undefined)[]
): T {
Expand Down
Loading

0 comments on commit 5ffb9ff

Please sign in to comment.