All files / owid-grapher/grapher/axis AxisViews.tsx

99.49% Statements 194/195
88% Branches 22/25
100% Functions 6/6
99.49% Lines 194/195

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 3141x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 440x 440x 440x 440x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 58x 58x 58x 58x 58x 58x 58x 346x   346x 319x 319x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 27x 27x 27x 27x 346x 346x 346x 346x 58x 58x 58x 1x 1x 1x 1x 1x 1x 1x 1x 19x 19x 1x 1x 19x 19x 19x 19x 19x 19x 19x 19x 135x 117x 117x 103x 103x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 14x 14x 14x 14x 135x 135x 135x 135x 19x 19x 19x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 1x 1x 1x 1x 1x 1x 1x 1x 53x 53x 53x 53x 53x 53x 53x 15x 15x 15x 53x 53x 53x 322x 322x 322x 322x 322x 322x 322x 322x 322x 322x                                                                                                                                                                                                                                              
import * as React from "react"
import { computed } from "mobx"
import { observer } from "mobx-react"
import { Bounds, DEFAULT_BOUNDS } from "../../clientUtils/Bounds"
import { VerticalAxis, HorizontalAxis, DualAxis } from "./Axis"
import classNames from "classnames"
import { ScaleType } from "../core/GrapherConstants"
import {
    HorizontalAlign,
    Position,
    VerticalAlign,
} from "../../clientUtils/owidTypes"
import { dyFromAlign, textAnchorFromAlign } from "../../clientUtils/Util"
 
const dasharrayFromFontSize = (fontSize: number): string => {
    const dashLength = Math.round((fontSize / 16) * 3)
    const spaceLength = Math.round((dashLength * 2) / 3)
    return `${dashLength},${spaceLength}`
}
 
const TICK_COLOR = "#ddd"
const FAINT_TICK_COLOR = "#eee"
const DOMAIN_TICK_COLOR = "#999"
 
@observer
export class VerticalAxisGridLines extends React.Component<{
    verticalAxis: VerticalAxis
    bounds: Bounds
}> {
    render(): JSX.Element {
        const { bounds, verticalAxis } = this.props
        const axis = verticalAxis.clone()
        axis.range = bounds.yRange()
 
        return (
            <g className={classNames("AxisGridLines", "horizontalLines")}>
                {axis.getTickValues().map((t, i) => {
                    const color = t.faint
                        ? FAINT_TICK_COLOR
                        : t.value === 0
                        ? DOMAIN_TICK_COLOR
                        : TICK_COLOR
 
                    return (
                        <line
                            key={i}
                            x1={bounds.left.toFixed(2)}
                            y1={axis.place(t.value)}
                            x2={bounds.right.toFixed(2)}
                            y2={axis.place(t.value)}
                            stroke={color}
                            strokeDasharray={
                                t.value !== 0
                                    ? dasharrayFromFontSize(
                                          verticalAxis.tickFontSize
                                      )
                                    : undefined
                            }
                        />
                    )
                })}
            </g>
        )
    }
}
 
@observer
export class HorizontalAxisGridLines extends React.Component<{
    horizontalAxis: HorizontalAxis
    bounds?: Bounds
}> {
    @computed get bounds(): Bounds {
        return this.props.bounds ?? DEFAULT_BOUNDS
    }
 
    render(): JSX.Element {
        const { horizontalAxis } = this.props
        const { bounds } = this
        const axis = horizontalAxis.clone()
        axis.range = bounds.xRange()
 
        return (
            <g className={classNames("AxisGridLines", "verticalLines")}>
                {axis.getTickValues().map((t, i) => {
                    const color = t.faint
                        ? FAINT_TICK_COLOR
                        : t.value === 0
                        ? DOMAIN_TICK_COLOR
                        : TICK_COLOR
 
                    return (
                        <line
                            key={i}
                            x1={axis.place(t.value)}
                            y1={bounds.bottom.toFixed(2)}
                            x2={axis.place(t.value)}
                            y2={bounds.top.toFixed(2)}
                            stroke={color}
                            strokeDasharray={
                                t.value !== 0
                                    ? dasharrayFromFontSize(
                                          horizontalAxis.tickFontSize
                                      )
                                    : undefined
                            }
                        />
                    )
                })}
            </g>
        )
    }
}
 
interface DualAxisViewProps {
    dualAxis: DualAxis
    highlightValue?: { x: number; y: number }
    showTickMarks?: boolean
}
 
@observer
export class DualAxisComponent extends React.Component<DualAxisViewProps> {
    render(): JSX.Element {
        const { dualAxis, showTickMarks } = this.props
        const { bounds, horizontalAxis, verticalAxis, innerBounds } = dualAxis
 
        const verticalGridlines = verticalAxis.hideGridlines ? null : (
            <VerticalAxisGridLines
                verticalAxis={verticalAxis}
                bounds={innerBounds}
            />
        )
 
        const horizontalGridlines = horizontalAxis.hideGridlines ? null : (
            <HorizontalAxisGridLines
                horizontalAxis={horizontalAxis}
                bounds={innerBounds}
            />
        )
 
        const verticalAxisComponent = verticalAxis.hideAxis ? null : (
            <VerticalAxisComponent
                bounds={bounds}
                verticalAxis={verticalAxis}
            />
        )
 
        const horizontalAxisComponent = horizontalAxis.hideAxis ? null : (
            <HorizontalAxisComponent
                bounds={bounds}
                axis={horizontalAxis}
                showTickMarks={showTickMarks}
                preferredAxisPosition={innerBounds.bottom}
            />
        )
 
        return (
            <g className="DualAxisView">
                {horizontalAxisComponent}
                {verticalAxisComponent}
                {verticalGridlines}
                {horizontalGridlines}
            </g>
        )
    }
}
 
@observer
export class VerticalAxisComponent extends React.Component<{
    bounds: Bounds
    verticalAxis: VerticalAxis
}> {
    render(): JSX.Element {
        const { bounds, verticalAxis } = this.props
        const { tickLabels, labelTextWrap } = verticalAxis
        const textColor = "#666"
 
        return (
            <g className="VerticalAxis">
                {labelTextWrap &&
                    labelTextWrap.render(
                        -bounds.centerY - labelTextWrap.width / 2,
                        bounds.left,
                        { transform: "rotate(-90)" }
                    )}
                {tickLabels.map((label, i) => {
                    const { y, xAlign, yAlign, formattedValue } = label
                    return (
                        <text
                            key={i}
                            x={(
                                bounds.left +
                                verticalAxis.width -
                                verticalAxis.labelPadding
                            ).toFixed(2)}
                            y={y}
                            dy={dyFromAlign(yAlign ?? VerticalAlign.middle)}
                            textAnchor={textAnchorFromAlign(
                                xAlign ?? HorizontalAlign.right
                            )}
                            fill={textColor}
                            fontSize={verticalAxis.tickFontSize}
                        >
                            {formattedValue}
                        </text>
                    )
                })}
            </g>
        )
    }
}
 
export class HorizontalAxisComponent extends React.Component<{
    bounds: Bounds
    axis: HorizontalAxis
    showTickMarks?: boolean
    preferredAxisPosition?: number
}> {
    @computed get scaleType(): ScaleType {
        return this.props.axis.scaleType
    }
 
    set scaleType(scaleType: ScaleType) {
        this.props.axis.config.scaleType = scaleType
    }
 
    // for scale selector. todo: cleanup
    @computed get bounds(): Bounds {
        const { bounds, axis } = this.props
        if (axis.orient === Position.top)
            return new Bounds(bounds.right, bounds.top + 30, 100, 100)
        else return new Bounds(bounds.right, bounds.bottom - 30, 100, 100)
    }
 
    render(): JSX.Element {
        const { bounds, axis, showTickMarks, preferredAxisPosition } =
            this.props
        const { tickLabels, labelTextWrap: label, labelOffset, orient } = axis
        const horizontalAxisLabelsOnTop = orient === Position.top
        const textColor = "#666"
        const labelYPosition = horizontalAxisLabelsOnTop
            ? bounds.top
            : bounds.bottom - (label?.height ?? 0)
 
        const tickMarksYPosition = horizontalAxisLabelsOnTop
            ? bounds.top + axis.height - 5
            : preferredAxisPosition ?? bounds.bottom
 
        const tickMarks = showTickMarks ? (
            <AxisTickMarks
                tickMarkTopPosition={tickMarksYPosition}
                tickMarkXPositions={tickLabels.map((label): number =>
                    axis.place(label.value)
                )}
                color={DOMAIN_TICK_COLOR}
            />
        ) : undefined
 
        const tickLabelYPlacement = horizontalAxisLabelsOnTop
            ? bounds.top + labelOffset + 10
            : bounds.bottom - labelOffset
        return (
            <g className="HorizontalAxis">
                {label &&
                    label.render(
                        bounds.centerX - label.width / 2,
                        labelYPosition
                    )}
                {tickMarks}
                {tickLabels.map((label, i) => {
                    const { x, xAlign, formattedValue } = label
                    return (
                        <text
                            key={i}
                            x={x}
                            y={tickLabelYPlacement}
                            fill={textColor}
                            textAnchor={textAnchorFromAlign(
                                xAlign ?? HorizontalAlign.center
                            )}
                            fontSize={axis.tickFontSize}
                        >
                            {formattedValue}
                        </text>
                    )
                })}
            </g>
        )
    }
}
 
export class AxisTickMarks extends React.Component<{
    tickMarkTopPosition: number
    tickMarkXPositions: number[]
    color: string
}> {
    render(): JSX.Element[] {
        const { tickMarkTopPosition, tickMarkXPositions, color } = this.props
        const tickSize = 5
        const tickBottom = tickMarkTopPosition + tickSize
        return tickMarkXPositions.map((tickMarkPosition, index) => {
            return (
                <line
                    key={index}
                    x1={tickMarkPosition}
                    y1={tickMarkTopPosition}
                    x2={tickMarkPosition}
                    y2={tickBottom}
                    stroke={color}
                />
            )
        })
    }
}