Skip to content

Commit

Permalink
🔨 (grapher) remove WorldMap from ChartTypeNames
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann committed Nov 20, 2024
1 parent 4d8d67a commit 20b2590
Show file tree
Hide file tree
Showing 16 changed files with 50 additions and 44 deletions.
4 changes: 2 additions & 2 deletions adminSiteClient/ColorSchemeDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react"
import { computed, action } from "mobx"
import Select from "react-select"
import { ChartTypeName } from "@ourworldindata/types"
import { GrapherChartOrMapType } from "@ourworldindata/types"
import {
ColorScheme,
getColorSchemeForChartType,
Expand All @@ -21,7 +21,7 @@ interface ColorSchemeDropdownProps {
value?: string
gradientColorCount: number
invertedColorScheme: boolean
chartType: ChartTypeName
chartType: GrapherChartOrMapType
onChange: (selected: ColorSchemeOption) => void
onBlur?: () => void
}
Expand Down
6 changes: 1 addition & 5 deletions adminSiteClient/EditorBasicTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,7 @@ export class EditorBasicTab<
value: string
label: string
}[] {
const allChartTypes = Object.keys(ChartTypeName).filter(
(chartType) => chartType !== ChartTypeName.WorldMap
)

const chartTypeOptions = allChartTypes.map((key) => ({
const chartTypeOptions = Object.keys(ChartTypeName).map((key) => ({
value: key,
label: startCase(key),
}))
Expand Down
6 changes: 3 additions & 3 deletions adminSiteClient/EditorColorScaleSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { faPlus, faMinus } from "@fortawesome/free-solid-svg-icons"
import {
ColorSchemeName,
BinningStrategy,
ChartTypeName,
GrapherChartOrMapType,
Color,
} from "@ourworldindata/types"
import {
Expand Down Expand Up @@ -43,7 +43,7 @@ interface EditorColorScaleSectionFeatures {
@observer
export class EditorColorScaleSection extends React.Component<{
scale: ColorScale
chartType: ChartTypeName
chartType: GrapherChartOrMapType
features: EditorColorScaleSectionFeatures
showLineChartColors: boolean
onChange?: () => void
Expand Down Expand Up @@ -132,7 +132,7 @@ class ColorLegendSection extends React.Component<{
@observer
class ColorsSection extends React.Component<{
scale: ColorScale
chartType: ChartTypeName
chartType: GrapherChartOrMapType
showLineChartColors: boolean
onChange?: () => void
}> {
Expand Down
4 changes: 2 additions & 2 deletions adminSiteClient/EditorMapTab.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
ChartTypeName,
GrapherInterface,
MapProjectionName,
GRAPHER_MAP_TYPE,
} from "@ourworldindata/types"
import {
ChartDimension,
Expand Down Expand Up @@ -202,7 +202,7 @@ export class EditorMapTab<
<TimelineSection mapConfig={mapConfig} />
<EditorColorScaleSection
scale={colorScale}
chartType={ChartTypeName.WorldMap}
chartType={GRAPHER_MAP_TYPE}
showLineChartColors={false}
features={{
visualScaling: true,
Expand Down
4 changes: 2 additions & 2 deletions adminSiteClient/GrapherConfigGridEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import { BaseEditorComponent, HotColumn, HotTable } from "@handsontable/react"
import { AdminAppContext, AdminAppContextType } from "./AdminAppContext.js"

import Handsontable from "handsontable"
import { ChartTypeName } from "@ourworldindata/types"
import { ChartTypeName, GRAPHER_MAP_TYPE } from "@ourworldindata/types"
import {
Grapher,
GrapherProgrammaticInterface,
Expand Down Expand Up @@ -488,7 +488,7 @@ export class GrapherConfigGridEditor extends React.Component<GrapherConfigGridEd
return colorScale ? (
<EditorColorScaleSection
scale={colorScale}
chartType={ChartTypeName.WorldMap}
chartType={GRAPHER_MAP_TYPE}
features={{
visualScaling: true,
legendDescription: false,
Expand Down
3 changes: 2 additions & 1 deletion adminSiteServer/testPageRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
GrapherTabOption,
StackMode,
parseChartConfig,
GRAPHER_MAP_TYPE,
} from "@ourworldindata/types"
import { ExplorerAdminServer } from "../explorerAdminServer/ExplorerAdminServer.js"
import { GIT_CMS_DIR } from "../gitCms/GitCmsConstants.js"
Expand Down Expand Up @@ -148,7 +149,7 @@ async function propsFromQueryParams(
let tab = params.tab

if (params.type) {
if (params.type === ChartTypeName.WorldMap) {
if (params.type === GRAPHER_MAP_TYPE) {
query = query.andWhereRaw(`cc.full->>"$.hasMapTab" = "true"`)
tab = tab || GrapherTabOption.map
} else {
Expand Down
2 changes: 0 additions & 2 deletions devTools/svgTester/chart-configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ const VIEW_MATRIX_BY_CHART_TYPE: Record<ChartTypeName, ViewMatrix> = {
facet: facetOptions,
uniformYAxis: booleanOptions,
},
[ChartTypeName.WorldMap]: {},
}

// the above view matrix is used to generate all possible combinations of query params
Expand Down Expand Up @@ -129,7 +128,6 @@ const EXCLUDE_VIEWS_BY_CHART_TYPE: Record<
// sharing an axis only makes sense if a chart is faceted
{ facet: FacetStrategy.none, uniformYAxis: Boolean.true },
],
[ChartTypeName.WorldMap]: [],
}

export const queryStringsByChartType = Object.fromEntries(
Expand Down
4 changes: 2 additions & 2 deletions devTools/svgTester/dump-chart-ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import parseArgs from "minimist"

import { TransactionCloseMode, knexReadonlyTransaction } from "../../db/db.js"
import { getMostViewedGrapherIdsByChartType } from "../../db/model/Chart.js"
import { CHART_TYPES } from "./utils.js"
import { allChartTypes } from "./utils.js"

const DEFAULT_OUT_FILE = "../owid-grapher-svgs/most-viewed-charts.txt"
const CHART_COUNT_PER_TYPE = 25
Expand All @@ -15,7 +15,7 @@ async function main(parsedArgs: parseArgs.ParsedArgs) {
const outFile = parsedArgs["o"] ?? DEFAULT_OUT_FILE

const chartIds = await knexReadonlyTransaction(async (trx) => {
const promises = CHART_TYPES.flatMap((chartType) =>
const promises = allChartTypes.flatMap((chartType) =>
getMostViewedGrapherIdsByChartType(
trx,
chartType,
Expand Down
11 changes: 4 additions & 7 deletions devTools/svgTester/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ import { getHeapStatistics } from "v8"
import { queryStringsByChartType } from "./chart-configurations.js"
import * as d3 from "d3"

// ignore WorldMaps
export const CHART_TYPES = Object.values(ChartTypeName).filter(
(chartType) => chartType !== ChartTypeName.WorldMap
)
export const allChartTypes = Object.values(ChartTypeName)

// the owid-grapher-svgs repo is usually cloned as a sibling to the owid-grapher repo
export const DEFAULT_CONFIGS_DIR = "../owid-grapher-svgs/configs"
Expand Down Expand Up @@ -303,14 +300,14 @@ export async function findValidChartIds(
export function validateChartTypes(chartTypes: string[]): ChartTypeName[] {
const validChartTypes = chartTypes.filter(
(chartType): chartType is ChartTypeName =>
CHART_TYPES.includes(chartType as any)
allChartTypes.includes(chartType as any)
)
const invalidChartTypes = chartTypes.filter(
(chartType) => !CHART_TYPES.includes(chartType as any)
(chartType) => !allChartTypes.includes(chartType as any)
)
if (invalidChartTypes.length) {
console.warn(
`Invalid chart types given: ${invalidChartTypes}. Valid chart types are: ${CHART_TYPES}`
`Invalid chart types given: ${invalidChartTypes}. Valid chart types are: ${allChartTypes}`
)
}
return _.uniq(validChartTypes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import {
RelatedQuestionsConfig,
Color,
GrapherTabName,
GRAPHER_MAP_TYPE,
GrapherChartOrMapType,
} from "@ourworldindata/types"
import { DataTable, DataTableManager } from "../dataTable/DataTable"
import {
Expand Down Expand Up @@ -189,10 +191,10 @@ export class CaptionedChart extends React.Component<CaptionedChartProps> {
return !this.manager.isOnMapTab && hasStrategy
}

@computed get activeChartType(): ChartTypeName | undefined {
@computed get activeChartOrMapType(): GrapherChartOrMapType | undefined {
const { manager } = this
if (manager.isOnTableTab) return undefined
if (manager.isOnMapTab) return ChartTypeName.WorldMap
if (manager.isOnMapTab) return GRAPHER_MAP_TYPE
if (manager.isOnChartTab) {
return manager.isLineChartThatTurnedIntoDiscreteBar
? ChartTypeName.DiscreteBar
Expand All @@ -202,20 +204,21 @@ export class CaptionedChart extends React.Component<CaptionedChartProps> {
}

renderChart(): React.ReactElement | void {
const { manager, activeChartType, containerElement } = this
const { manager, activeChartOrMapType, containerElement } = this

if (!activeChartType) return
if (!activeChartOrMapType) return

const bounds = this.boundsForChartArea
const ChartClass =
ChartComponentClassMap.get(activeChartType) ?? DefaultChartClass
ChartComponentClassMap.get(activeChartOrMapType) ??
DefaultChartClass

// Todo: make FacetChart a chart type name?
if (this.isFaceted)
if (this.isFaceted && activeChartOrMapType !== GRAPHER_MAP_TYPE)
return (
<FacetChart
bounds={bounds}
chartTypeName={activeChartType}
chartTypeName={activeChartOrMapType}
manager={manager}
/>
)
Expand Down
10 changes: 7 additions & 3 deletions packages/@ourworldindata/grapher/src/chart/ChartTypeMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { LineChart } from "../lineCharts/LineChart"
import { StackedAreaChart } from "../stackedCharts/StackedAreaChart"
import { DiscreteBarChart } from "../barCharts/DiscreteBarChart"
import { StackedBarChart } from "../stackedCharts/StackedBarChart"
import { ChartTypeName } from "@ourworldindata/types"
import {
ChartTypeName,
GrapherChartOrMapType,
GRAPHER_MAP_TYPE,
} from "@ourworldindata/types"
import { MapChart } from "../mapCharts/MapChart"
import { ChartInterface } from "./ChartInterface"
import { ChartManager } from "./ChartManager"
Expand All @@ -24,7 +28,7 @@ interface ChartComponentClass extends ComponentClass<ChartComponentProps> {
}

export const ChartComponentClassMap = new Map<
ChartTypeName,
GrapherChartOrMapType,
ChartComponentClass
>([
[ChartTypeName.DiscreteBar, DiscreteBarChart],
Expand All @@ -35,7 +39,7 @@ export const ChartComponentClassMap = new Map<
[ChartTypeName.StackedDiscreteBar, StackedDiscreteBarChart],
[ChartTypeName.ScatterPlot, ScatterPlotChart],
[ChartTypeName.Marimekko, MarimekkoChart],
[ChartTypeName.WorldMap, MapChart],
[GRAPHER_MAP_TYPE, MapChart],
])

export const DefaultChartClass = LineChart as ChartComponentClass
8 changes: 6 additions & 2 deletions packages/@ourworldindata/grapher/src/color/ColorSchemes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import {
ChartTypeName,
ColorSchemeInterface,
ColorSchemeName,
GRAPHER_MAP_TYPE,
GrapherChartOrMapType,
} from "@ourworldindata/types"
import { getColorBrewerScheme } from "./ColorBrewerSchemes.js"

function getPreferredSchemesByType(type: ChartTypeName): ColorSchemeName[] {
function getPreferredSchemesByType(
type: GrapherChartOrMapType
): ColorSchemeName[] {
// This function could also be a Map<ChartTypeName, ColorName[]> but
// by doing it as a function usign ts-pattern.match we get compile
// time safety that all enum cases in ChartTypeName are always handled here
Expand Down Expand Up @@ -84,7 +88,7 @@ function getPreferredSchemesByType(type: ChartTypeName): ColorSchemeName[] {
ColorSchemeName.SingleColorGradientDustyCoral,
ColorSchemeName.SingleColorGradientDarkCopper,
])
.with(ChartTypeName.WorldMap, () => [
.with(GRAPHER_MAP_TYPE, () => [
ColorSchemeName.BinaryMapPaletteA,
ColorSchemeName.BinaryMapPaletteB,
ColorSchemeName.BinaryMapPaletteC,
Expand Down
3 changes: 0 additions & 3 deletions packages/@ourworldindata/grapher/src/controls/ChartIcons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,4 @@ export const chartIcons: Record<ChartTypeName, React.ReactElement> = {
</g>
</svg>
),

// world map (will never be invoked but included for completeness)
[ChartTypeName.WorldMap]: <FontAwesomeIcon icon={faEarthAmericas} />,
}
2 changes: 1 addition & 1 deletion packages/@ourworldindata/types/src/dbTypes/ChartConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface DbInsertChartConfig {
full: JsonString
fullMd5?: Base64String
slug?: string | null
chartType?: ChartTypeName | null // TODO: exclude WorldMap
chartType?: ChartTypeName | null
createdAt?: Date
updatedAt?: Date | null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ export enum ToleranceStrategy {
backwards = "backwards",
forwards = "forwards",
}

export const GRAPHER_MAP_TYPE = "WorldMap"
type GrapherMapType = typeof GRAPHER_MAP_TYPE

export enum ChartTypeName {
LineChart = "LineChart",
ScatterPlot = "ScatterPlot",
Expand All @@ -128,10 +132,10 @@ export enum ChartTypeName {
SlopeChart = "SlopeChart",
StackedBar = "StackedBar",
Marimekko = "Marimekko",
// special map type that can't be selected by authors
WorldMap = "WorldMap",
}

export type GrapherChartOrMapType = ChartTypeName | GrapherMapType

export enum AxisMinMaxValueStr {
auto = "auto",
}
Expand Down
2 changes: 2 additions & 0 deletions packages/@ourworldindata/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ export {
type ColorScaleConfigInterface,
ColorSchemeName,
ChartTypeName,
type GrapherChartOrMapType,
GRAPHER_MAP_TYPE,
GrapherTabOption,
GrapherTabName,
GrapherTabQueryParam,
Expand Down

0 comments on commit 20b2590

Please sign in to comment.