All files / owid-grapher/grapher/scatterCharts ComparisonLine.tsx

100% Statements 116/116
80% Branches 8/10
100% Functions 5/5
100% Lines 116/116

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 1351x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 2999x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x                                      
import { line as d3_line, curveLinear } from "d3-shape"
import { guid } from "../../clientUtils/Util"
import * as React from "react"
import { computed } from "mobx"
import { observer } from "mobx-react"
import { DualAxis } from "../axis/Axis"
import { generateComparisonLinePoints } from "./ComparisonLineGenerator"
import { Bounds } from "../../clientUtils/Bounds"
import { PointVector } from "../../clientUtils/PointVector"
import { getElementWithHalo } from "./Halos"
 
export interface ComparisonLineConfig {
    label?: string
    yEquals?: string
}
 
@observer
export class ComparisonLine extends React.Component<{
    dualAxis: DualAxis
    comparisonLine: ComparisonLineConfig
}> {
    private renderUid = guid()
 
    @computed private get controlData(): [number, number][] {
        const { comparisonLine, dualAxis } = this.props
        const { horizontalAxis, verticalAxis } = dualAxis
        return generateComparisonLinePoints(
            comparisonLine.yEquals,
            horizontalAxis.domain,
            verticalAxis.domain,
            horizontalAxis.scaleType,
            verticalAxis.scaleType
        )
    }
 
    @computed private get linePath(): string | null {
        const { controlData } = this
        const { horizontalAxis, verticalAxis } = this.props.dualAxis
        const line = d3_line()
            .curve(curveLinear)
            .x((d) => horizontalAxis.place(d[0]))
            .y((d) => verticalAxis.place(d[1]))
        return line(controlData)
    }
 
    @computed private get placedLabel():
        | { x: number; y: number; bounds: Bounds; angle: number; text: string }
        | undefined {
        const { label } = this.props.comparisonLine
        if (!label) return
 
        const { controlData } = this
        const { horizontalAxis, verticalAxis, innerBounds } =
            this.props.dualAxis
 
        // Find the points of the line that are actually placeable on the chart
        const linePoints = controlData
            .map(
                (d) =>
                    new PointVector(
                        horizontalAxis.place(d[0]),
                        verticalAxis.place(d[1])
                    )
            )
            .filter((p) => innerBounds.contains(p))
        if (!linePoints.length) return
 
        const midPoint = linePoints[Math.floor(linePoints.length / 2)]
        const p1 = linePoints[0]
        const p2 = linePoints[linePoints.length - 1]
        const angle = (Math.atan2(p2.y - p1.y, p2.x - p1.x) * 180) / Math.PI
        const bounds = Bounds.forText(label)
        return {
            x: midPoint.x,
            y: midPoint.y,
            bounds: bounds,
            angle: angle,
            text: label,
        }
    }
 
    render(): JSX.Element {
        const { innerBounds } = this.props.dualAxis
        const { linePath, renderUid, placedLabel } = this
 
        return (
            <g className="comparisonLine">
                <defs>
                    <clipPath id={`axisBounds-${renderUid}`}>
                        <rect
                            x={innerBounds.x}
                            y={innerBounds.y}
                            width={innerBounds.width}
                            height={innerBounds.height}
                        />
                    </clipPath>
                </defs>
                <path
                    style={{
                        opacity: 0.9,
                        fill: "none",
                        stroke: "#ccc",
                        strokeDasharray: "2 2",
                    }}
                    id={`path-${renderUid}`}
                    d={linePath || undefined}
                    clipPath={`url(#axisBounds-${renderUid})`}
                />
                {placedLabel && (
                    <text
                        style={{
                            fontSize: "80%",
                            opacity: 0.9,
                            textAnchor: "end",
                            fill: "#999",
                        }}
                        clipPath={`url(#axisBounds-${renderUid})`}
                    >
                        {getElementWithHalo(
                            `path-${renderUid}`,
                            <textPath
                                baselineShift="-0.2rem"
                                href={`#path-${renderUid}`}
                                startOffset="90%"
                            >
                                {placedLabel.text}
                            </textPath>
                        )}
                    </text>
                )}
            </g>
        )
    }
}