Skip to content

Commit

Permalink
🐛 (discrete bar) zero line obscured part of the data bars (#3996)
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann authored Oct 3, 2024
1 parent 215ea00 commit 04c589b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
22 changes: 19 additions & 3 deletions packages/@ourworldindata/grapher/src/axis/AxisViews.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,28 @@ export class HorizontalAxisZeroLine extends React.Component<{
horizontalAxis: HorizontalAxis
bounds: Bounds
strokeWidth?: number
align?: HorizontalAlign
}> {
render(): React.ReactElement {
const { bounds, horizontalAxis, strokeWidth } = this.props
const {
bounds,
horizontalAxis,
align = HorizontalAlign.center,
strokeWidth = 1,
} = this.props
const axis = horizontalAxis.clone()
axis.range = bounds.xRange()

// the zero line is either drawn at the center of the zero tick
// or at the edge of the tick, either to the left or to the right
const offset =
align === HorizontalAlign.center
? 0
: align === HorizontalAlign.right
? -strokeWidth / 2
: strokeWidth / 2
const x = axis.place(0) + offset

return (
<g
id={makeIdForHumanConsumption("vertical-zero-line")}
Expand All @@ -154,9 +170,9 @@ export class HorizontalAxisZeroLine extends React.Component<{
)}
>
<line
x1={axis.place(0)}
x1={x.toFixed(2)}
y1={bounds.bottom.toFixed(2)}
x2={axis.place(0)}
x2={x.toFixed(2)}
y2={bounds.top.toFixed(2)}
stroke={SOLID_TICK_COLOR}
strokeWidth={strokeWidth}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,21 @@ export class DiscreteBarChart
/>
</>
)}
{!this.isLogScale && (
<HorizontalAxisZeroLine
horizontalAxis={yAxis}
bounds={innerBounds}
strokeWidth={axisLineWidth}
// if the chart doesn't have negative values, then we
// move the zero line a little to the left to avoid
// overlap with the bars
align={
this.hasNegative
? HorizontalAlign.center
: HorizontalAlign.right
}
/>
)}
{sizedSeries.map((series) => {
// Todo: add a "placedSeries" getter to get the transformed series, then just loop over the placedSeries and render a bar for each
const isNegative = series.value < 0
Expand Down Expand Up @@ -560,13 +575,6 @@ export class DiscreteBarChart

return result
})}
{!this.isLogScale && (
<HorizontalAxisZeroLine
horizontalAxis={yAxis}
bounds={innerBounds}
strokeWidth={axisLineWidth}
/>
)}
</g>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,14 @@ export class StackedDiscreteBarChart
/>
</>
)}
<HorizontalAxisZeroLine
horizontalAxis={yAxis}
bounds={innerBounds}
strokeWidth={axisLineWidth}
// moves the zero line a little to the left to avoid
// overlap with the bars
align={HorizontalAlign.right}
/>
{this.showLegend && (
<HorizontalCategoricalColorLegend manager={this} />
)}
Expand All @@ -733,11 +741,6 @@ export class StackedDiscreteBarChart
<g>{nodes.map((node) => renderRow(node))}</g>
)}
</NodeGroup>
<HorizontalAxisZeroLine
horizontalAxis={yAxis}
bounds={innerBounds}
strokeWidth={axisLineWidth}
/>
{this.Tooltip}
</g>
)
Expand Down

0 comments on commit 04c589b

Please sign in to comment.