Skip to content

Commit

Permalink
fix(grapher): fix chart icon on first load for line charts
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann committed Oct 16, 2023
1 parent fc2f6b2 commit 565d485
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
25 changes: 17 additions & 8 deletions packages/@ourworldindata/grapher/src/controls/ContentSwitchers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface ContentSwitchersManager {
isNarrow?: boolean
type?: ChartTypeName
isLineChartThatTurnedIntoDiscreteBar?: boolean
isAuthoredAsLineChartThatTurnedIntoDiscreteBar?: boolean
}

@observer
Expand Down Expand Up @@ -45,20 +46,28 @@ export class ContentSwitchers extends React.Component<{
case GrapherTabOption.map:
return <FontAwesomeIcon icon={faEarthAmericas} />
case GrapherTabOption.chart:
const chartIcon = manager.isLineChartThatTurnedIntoDiscreteBar
let chartIcon = manager.isLineChartThatTurnedIntoDiscreteBar
? chartIcons[ChartTypeName.DiscreteBar]
: chartIcons[this.chartType]
// If we're switching from a line chart to the map, then the timeline
// is automatically set to a single year, and the underlying chart switches to
// a discrete bar chart, which makes the line chart icon change into a bar chart icon.
// To prevent that, we hold onto the previous chart icon if we're not currently on the chart tab.
const newChartIcon =
this.previousChartIcon &&
manager.tab !== GrapherTabOption.chart
? this.previousChartIcon
: chartIcon
this.previousChartIcon = newChartIcon
return newChartIcon
if (manager.tab !== GrapherTabOption.chart) {
// make sure we're showing the line chart icon on first load
// if the chart is configured to be a line chart initially
if (
!this.previousChartIcon &&
this.chartType === ChartTypeName.LineChart &&
!manager.isAuthoredAsLineChartThatTurnedIntoDiscreteBar
) {
chartIcon = chartIcons[ChartTypeName.LineChart]
} else if (this.previousChartIcon) {
chartIcon = this.previousChartIcon
}
}
this.previousChartIcon = chartIcon
return chartIcon
}
}

Expand Down
10 changes: 10 additions & 0 deletions packages/@ourworldindata/grapher/src/core/Grapher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2676,6 +2676,16 @@ export class Grapher
})
}

@computed
get isAuthoredAsLineChartThatTurnedIntoDiscreteBar(): boolean {
const {
type = ChartTypeName.LineChart,
minTime,
maxTime,
} = this.legacyConfigAsAuthored
return type === ChartTypeName.LineChart && minTime === maxTime
}

@computed get queryStr(): string {
return queryParamsToStr(this.changedParams)
}
Expand Down

0 comments on commit 565d485

Please sign in to comment.