Skip to content

Commit

Permalink
Merge pull request #2777 from owid/line-chart-collapsed-icon
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelgerber authored Oct 17, 2023
2 parents b5ccd01 + 23eaa28 commit e074163
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
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

0 comments on commit e074163

Please sign in to comment.