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): display line/bar chart icon depending on whether it's c… #2777

Merged
merged 1 commit into from
Oct 17, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ export class ContentSwitchers extends React.Component<{
return this.manager.type ?? ChartTypeName.LineChart
}

private previousChartIcon: JSX.Element | undefined

private tabIcon(tab: GrapherTabOption): JSX.Element {
const { manager } = this
switch (tab) {
Expand All @@ -48,17 +46,7 @@ export class ContentSwitchers extends React.Component<{
const 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
return chartIcon
}
}

Expand Down
14 changes: 13 additions & 1 deletion packages/@ourworldindata/grapher/src/core/Grapher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1594,7 +1594,19 @@ export class Grapher
}

@computed get isLineChartThatTurnedIntoDiscreteBar(): boolean {
return this.isLineChart && this.areHandlesOnSameTime
if (!this.isLineChart) return false

// This is the easy case: minTime and maxTime are the same, no need to do
// more fancy checks
if (this.minTime === this.maxTime) return true

// We can have cases where minTime = Infinity and/or maxTime = -Infinity,
// but still only a single year is selected.
// To check for that we need to look at the times array.
const times = this.tableAfterAuthorTimelineFilter.timeColumn.uniqValues
const minTime = findClosestTime(times, this.minTime ?? -Infinity)
const maxTime = findClosestTime(times, this.maxTime ?? Infinity)
return minTime !== undefined && minTime === maxTime
}

@computed get supportsMultipleYColumns(): boolean {
Expand Down
Loading