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(grapher): fix chart icon on first load for line charts #2769

Closed
wants to merge 1 commit into from
Closed
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
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