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

✨ (scatter) horizonal axis labels #4340

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
30 changes: 23 additions & 7 deletions packages/@ourworldindata/grapher/src/axis/AxisViews.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import { VerticalAxis, HorizontalAxis, DualAxis } from "./Axis"
import classNames from "classnames"
import { GRAPHER_DARK_TEXT } from "../color/ColorConstants"
import { ScaleType, DetailsMarker } from "@ourworldindata/types"
import { ScaleType, DetailsMarker, AxisAlign } from "@ourworldindata/types"
import { MarkdownTextWrap } from "@ourworldindata/components"
import { GRAPHER_AXIS_LABEL_PADDING } from "../core/GrapherConstants.js"

Expand Down Expand Up @@ -429,12 +429,14 @@ export class VerticalAxisTickMark extends React.Component<{
export function HorizonalAxisLabel({
textWrap,
dualAxis,
align = AxisAlign.middle,
padding = GRAPHER_AXIS_LABEL_PADDING,
color = GRAPHER_DARK_TEXT,
detailsMarker,
}: {
textWrap: MarkdownTextWrap
dualAxis: DualAxis
align?: AxisAlign
padding?: number
color?: string
detailsMarker?: DetailsMarker
Expand All @@ -443,7 +445,12 @@ export function HorizonalAxisLabel({

const horizontalAxisLabelsOnTop = horizontalAxis.orient === Position.top

const x = horizontalAxis.rangeCenter
const x =
align === AxisAlign.middle
? horizontalAxis.rangeCenter
: AxisAlign.end
? bounds.right
: bounds.left
const y = horizontalAxisLabelsOnTop
? bounds.top - textWrap.height - padding
: bounds.bottom + padding
Expand All @@ -452,7 +459,12 @@ export function HorizonalAxisLabel({
id: makeIdForHumanConsumption("horizontal-axis-label"),
textProps: {
fill: color || GRAPHER_DARK_TEXT,
textAnchor: "middle",
textAnchor:
align === AxisAlign.middle
? "middle"
: align === AxisAlign.end
? "end"
: "start",
},
detailsMarker,
})
Expand All @@ -461,27 +473,31 @@ export function HorizonalAxisLabel({
export function VerticalAxisLabel({
textWrap,
dualAxis,
align = AxisAlign.middle,
padding = GRAPHER_AXIS_LABEL_PADDING,
color = GRAPHER_DARK_TEXT,
detailsMarker,
}: {
textWrap: MarkdownTextWrap
dualAxis: DualAxis
align?: AxisAlign
padding?: number
color?: string
detailsMarker?: DetailsMarker
}): React.ReactElement | null {
const { verticalAxis, bounds } = dualAxis

const x = -verticalAxis.rangeCenter
const y = bounds.left - textWrap.height - padding
const alignMiddle = align === AxisAlign.middle
const x = alignMiddle ? -verticalAxis.rangeCenter : bounds.left
const y =
(alignMiddle ? bounds.left : bounds.top) - textWrap.height - padding

return textWrap.renderSVG(x, y, {
id: makeIdForHumanConsumption("vertical-axis-label"),
textProps: {
transform: "rotate(-90)",
transform: alignMiddle ? "rotate(-90)" : undefined,
fill: color || GRAPHER_DARK_TEXT,
textAnchor: "middle",
textAnchor: alignMiddle ? "middle" : "start",
},
detailsMarker,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ColorSchemeName,
ValueRange,
ColumnSlug,
AxisAlign,
} from "@ourworldindata/types"
import { ComparisonLine } from "../scatterCharts/ComparisonLine"
import { observable, computed, action } from "mobx"
Expand Down Expand Up @@ -359,7 +360,7 @@ export class ScatterPlotChart

@computed private get axisBounds(): Bounds {
return this.innerBounds
.padLeft(this.verticalAxisLabelHeight)
.padTop(this.verticalAxisLabelHeight)
.padBottom(this.horizontalAxisLabelHeight)
}

Expand Down Expand Up @@ -833,6 +834,7 @@ export class ScatterPlotChart
<HorizonalAxisLabel
textWrap={this.horizontalAxisLabelWrap}
dualAxis={dualAxis}
align={AxisAlign.end}
color={manager.secondaryColorInStaticCharts}
detailsMarker={manager.detailsMarkerInSvg}
/>
Expand All @@ -841,6 +843,7 @@ export class ScatterPlotChart
<VerticalAxisLabel
textWrap={this.verticalAxisLabelWrap}
dualAxis={dualAxis}
align={AxisAlign.start}
color={manager.secondaryColorInStaticCharts}
detailsMarker={manager.detailsMarkerInSvg}
/>
Expand Down
Loading