Skip to content

Commit

Permalink
✨ (grapher) disallow switching to a slope chart for projections
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann committed Nov 21, 2024
1 parent 60d5738 commit bbf1d39
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
15 changes: 14 additions & 1 deletion packages/@ourworldindata/grapher/src/chart/ChartUtils.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react"
import { Box, getCountryByName } from "@ourworldindata/utils"
import { areSetsEqual, Box, getCountryByName } from "@ourworldindata/utils"
import {
SeriesStrategy,
EntityName,
Expand All @@ -15,6 +15,7 @@ import {
GRAPHER_SIDE_PANEL_CLASS,
GRAPHER_TIMELINE_CLASS,
GRAPHER_SETTINGS_CLASS,
validChartTypeCombinations,
} from "../core/GrapherConstants"

export const autoDetectYColumnSlugs = (manager: ChartManager): string[] => {
Expand Down Expand Up @@ -175,3 +176,15 @@ export function mapChartTypeNameToQueryParam(
return GRAPHER_TAB_QUERY_PARAMS.marimekko
}
}

export function findValidChartTypeCombination(
chartTypes: GrapherChartType[]
): GrapherChartType[] | undefined {
const chartTypeSet = new Set(chartTypes)
for (const validCombination of validChartTypeCombinations) {
const validCombinationSet = new Set(validCombination)
if (areSetsEqual(chartTypeSet, validCombinationSet))
return validCombination
}
return undefined
}
27 changes: 18 additions & 9 deletions packages/@ourworldindata/grapher/src/core/Grapher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ import {
extractDetailsFromSyntax,
omit,
isTouchDevice,
areSetsEqual,
} from "@ourworldindata/utils"
import {
MarkdownTextWrap,
Expand Down Expand Up @@ -138,7 +137,6 @@ import {
GRAPHER_FRAME_PADDING_HORIZONTAL,
GRAPHER_FRAME_PADDING_VERTICAL,
latestGrapherConfigSchema,
validChartTypeCombinations,
} from "../core/GrapherConstants"
import { loadVariableDataAndMetadata } from "./loadVariable"
import Cookies from "js-cookie"
Expand Down Expand Up @@ -200,6 +198,7 @@ import { ScatterPlotManager } from "../scatterCharts/ScatterPlotChartConstants"
import {
autoDetectSeriesStrategy,
autoDetectYColumnSlugs,
findValidChartTypeCombination,
mapChartTypeNameToQueryParam,
mapQueryParamToChartTypeName,
} from "../chart/ChartUtils"
Expand Down Expand Up @@ -1525,21 +1524,31 @@ export class Grapher
})
}

@computed get hasProjectedData(): boolean {
return this.inputTable.numericColumnSlugs.some(
(slug) => this.inputTable.get(slug).isProjection
)
}

@computed get validChartTypes(): GrapherChartType[] {
const { chartTypes } = this

// all single-chart Graphers are valid
if (chartTypes.length <= 1) return chartTypes

const chartTypeSet = new Set(chartTypes)
for (const validCombination of validChartTypeCombinations) {
const validCombinationSet = new Set(validCombination)
if (areSetsEqual(chartTypeSet, validCombinationSet))
return validCombination
}
// find valid combination in a pre-defined list
const validChartTypes = findValidChartTypeCombination(chartTypes)

// if the given combination is not valid, then ignore all but the first chart type
return chartTypes.slice(0, 1)
if (!validChartTypes) return chartTypes.slice(0, 1)

// projected data is only supported for line charts
const isLineChart = validChartTypes[0] === GRAPHER_CHART_TYPES.LineChart
if (isLineChart && this.hasProjectedData) {
return [GRAPHER_CHART_TYPES.LineChart]
}

return validChartTypes
}

@computed get validChartTypeSet(): Set<GrapherChartType> {
Expand Down

0 comments on commit bbf1d39

Please sign in to comment.