Skip to content

Commit

Permalink
✨ (slope) draw tick marks and connecting grid lines
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann committed Mar 6, 2024
1 parent b400bd2 commit 97afcfb
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 14 deletions.
46 changes: 43 additions & 3 deletions packages/@ourworldindata/grapher/src/axis/AxisViews.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,25 @@ export class DualAxisComponent extends React.Component<DualAxisViewProps> {
export class VerticalAxisComponent extends React.Component<{
bounds: Bounds
verticalAxis: VerticalAxis
showTickMarks?: boolean
labelColor?: string
tickColor?: string
}> {
render(): JSX.Element {
const { bounds, verticalAxis, labelColor, tickColor } = this.props
const { bounds, verticalAxis, labelColor, tickColor, showTickMarks } =
this.props
const { tickLabels, labelTextWrap } = verticalAxis

const tickMarks = showTickMarks ? (
<VerticalAxisTickMarks
tickMarkYPositions={tickLabels.map((label) =>
verticalAxis.place(label.value)
)}
tickMarkLeftPosition={bounds.left + verticalAxis.width}
color={SOLID_TICK_COLOR}
/>
) : undefined

return (
<g className="VerticalAxis">
{labelTextWrap &&
Expand All @@ -236,6 +248,7 @@ export class VerticalAxisComponent extends React.Component<{
fill: labelColor || GRAPHER_DARK_TEXT,
}
)}
{tickMarks}
{tickLabels.map((label, i) => {
const { y, xAlign, yAlign, formattedValue } = label
return (
Expand Down Expand Up @@ -309,7 +322,7 @@ export class HorizontalAxisComponent extends React.Component<{
: preferredAxisPosition ?? bounds.bottom

const tickMarks = showTickMarks ? (
<AxisTickMarks
<HorizontalAxisTickMarks
tickMarkTopPosition={tickMarksYPosition}
tickMarkXPositions={tickLabels.map((label): number =>
axis.place(label.value)
Expand Down Expand Up @@ -353,7 +366,7 @@ export class HorizontalAxisComponent extends React.Component<{
}
}

export class AxisTickMarks extends React.Component<{
export class HorizontalAxisTickMarks extends React.Component<{
tickMarkTopPosition: number
tickMarkXPositions: number[]
color: string
Expand All @@ -379,3 +392,30 @@ export class AxisTickMarks extends React.Component<{
})
}
}

export class VerticalAxisTickMarks extends React.Component<{
tickMarkLeftPosition: number
tickMarkYPositions: number[]
color: string
width?: number
}> {
render(): JSX.Element[] {
const { tickMarkYPositions, tickMarkLeftPosition, color, width } =
this.props
const tickSize = 5
const tickRight = tickMarkLeftPosition + tickSize
return tickMarkYPositions.map((tickMarkPosition, index) => {
return (
<line
key={index}
x1={tickMarkLeftPosition}
y1={tickMarkPosition}
x2={tickRight}
y2={tickMarkPosition}
stroke={color}
strokeWidth={width}
/>
)
})
}
}
32 changes: 23 additions & 9 deletions packages/@ourworldindata/grapher/src/slopeCharts/SlopeChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1097,23 +1097,37 @@ class LabelledSlopes
/>
<g className="gridlines">
{this.yAxis.tickLabels.map((tick, i) => {

Check warning on line 1099 in packages/@ourworldindata/grapher/src/slopeCharts/SlopeChart.tsx

View workflow job for this annotation

GitHub Actions / eslint

'i' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 1099 in packages/@ourworldindata/grapher/src/slopeCharts/SlopeChart.tsx

View workflow job for this annotation

GitHub Actions / eslint

'i' is defined but never used. Allowed unused args must match /^_/u
const y = yAxis.place(tick.value)
return (
<line
key={i}
x1={x1}
y1={yAxis.place(tick.value)}
x2={x2}
y2={yAxis.place(tick.value)}
stroke="#eee"
strokeDasharray="3,2"
/>
<g key={y.toString()}>
{/* grid lines connecting the chart area to the axis */}
<line
x1={bounds.left + this.yAxis.width + 8}
y1={y}
x2={x1}
y2={y}
stroke="#eee"
strokeDasharray="3,2"
/>
{/* grid lines within the chart area */}
<line
x1={x1}
y1={y}
x2={x2}
y2={y}
stroke="#ddd"
strokeDasharray="3,2"
/>
</g>
)
})}
</g>
{!isPortrait && (
<VerticalAxisComponent
bounds={bounds}
verticalAxis={this.yAxis}
showTickMarks={true}
labelColor={this.manager.secondaryColorInStaticCharts}
/>
)}
<line x1={x1} y1={y1} x2={x1} y2={y2} stroke="#333" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from "@ourworldindata/utils"
import {
VerticalAxisComponent,
AxisTickMarks,
HorizontalAxisTickMarks,
VerticalAxisGridLines,
} from "../axis/AxisViews"
import { NoDataModal } from "../noDataModal/NoDataModal"
Expand Down Expand Up @@ -457,7 +457,7 @@ export class StackedBarChart
strokeWidth={axisLineWidth}
/>

<AxisTickMarks
<HorizontalAxisTickMarks
tickMarkTopPosition={innerBounds.bottom}
tickMarkXPositions={ticks.map(
(tick) => tick.bounds.centerX
Expand Down

0 comments on commit 97afcfb

Please sign in to comment.