All files / owid-grapher/grapher/scatterCharts ScatterUtils.ts

59.39% Statements 98/165
72.22% Branches 13/18
100% Functions 4/4
59.39% Lines 98/165

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 2181x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1224x 1224x 1224x 1224x 1224x 1224x 1224x 1224x 1x 1x 1x 1x 1x 432x 432x 432x 432x 432x 432x 27x 27x 27x 27x         432x 432x 432x 432x 432x 432x 432x 432x 432x 432x 432x 432x 432x 27x 27x 27x 27x 27x 432x 432x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 1x 1x 1x 1x 432x 432x 432x 432x 432x 432x 27x 27x 432x 432x                 432x 432x 432x                                                                                                               432x 1x 1x 1x 1x 1x 1x 432x 432x 432x 432x                                                                                                          
import { Bounds } from "../../clientUtils/Bounds"
import { PointVector } from "../../clientUtils/PointVector"
import { last, maxBy } from "../../clientUtils/Util"
import {
    ScatterLabel,
    ScatterRenderPoint,
    ScatterRenderSeries,
    ScatterLabelFontFamily as fontFamily,
} from "./ScatterPlotChartConstants"
 
export const labelPriority = (label: ScatterLabel): number => {
    let priority = label.fontSize
 
    if (label.series.isHover) priority += 10000
    if (label.series.isFocus) priority += 1000
    if (label.isEnd) priority += 100
 
    return priority
}
 
const FONT_SIZE_WHEN_HIDDEN_LINES = 12
 
// Create the start year label for a series
export const makeStartLabel = (
    series: ScatterRenderSeries,
    isSubtleForeground: boolean,
    hideConnectedScatterLines: boolean
): ScatterLabel | undefined => {
    // No room to label the year if it's a single point
    if (!series.isForeground || series.points.length <= 1) return undefined
 
    const fontSize = hideConnectedScatterLines
        ? FONT_SIZE_WHEN_HIDDEN_LINES
        : series.isForeground
        ? isSubtleForeground
            ? 8
            : 9
        : 7
    const firstValue = series.points[0]
    const nextValue = series.points[1]
    const nextSegment = nextValue.position.subtract(firstValue.position)
 
    const pos = firstValue.position.subtract(nextSegment.normalize().times(5))
    let bounds = Bounds.forText(firstValue.label, {
        x: pos.x,
        y: pos.y,
        fontSize: fontSize,
        fontFamily,
    })
    if (pos.x < firstValue.position.x)
        bounds = new Bounds(
            bounds.x - bounds.width + 2,
            bounds.y,
            bounds.width,
            bounds.height
        )
    if (pos.y > firstValue.position.y)
        bounds = new Bounds(
            bounds.x,
            bounds.y + bounds.height / 2,
            bounds.width,
            bounds.height
        )
 
    return {
        text: firstValue.label,
        fontSize,
        fontWeight: 400,
        color: firstValue.color,
        bounds,
        series,
        isStart: true,
    }
}
 
// Make labels for the points between start and end on a series
// Positioned using normals of the line segments
export const makeMidLabels = (
    series: ScatterRenderSeries,
    isSubtleForeground: boolean,
    hideConnectedScatterLines: boolean
): ScatterLabel[] => {
    if (
        !series.isForeground ||
        series.points.length <= 1 ||
        (!series.isHover && isSubtleForeground)
    )
        return []

    const fontSize = hideConnectedScatterLines
        ? FONT_SIZE_WHEN_HIDDEN_LINES
        : series.isForeground
        ? isSubtleForeground
            ? 8
            : 9
        : 7
    const fontWeight = 400
 
    return series.points.slice(1, -1).map((v, i) => {
        const prevPos = i > 0 && series.points[i - 1].position
        const prevSegment = prevPos && v.position.subtract(prevPos)
        const nextPos = series.points[i + 1].position
        const nextSegment = nextPos.subtract(v.position)

        let pos = v.position
        if (prevPos && prevSegment) {
            const normals = prevSegment
                .add(nextSegment)
                .normalize()
                .normals()
                .map((x) => x.times(5))
            const potentialSpots = normals.map((n) => v.position.add(n))
            pos = maxBy(potentialSpots, (p) => {
                return (
                    PointVector.distance(p, prevPos) +
                    PointVector.distance(p, nextPos)
                )
            }) as PointVector
        } else {
            pos = v.position.subtract(nextSegment.normalize().times(5))
        }

        let bounds = Bounds.forText(v.label, {
            x: pos.x,
            y: pos.y,
            fontSize: fontSize,
            fontWeight: fontWeight,
            fontFamily,
        })
        if (pos.x < v.position.x)
            bounds = new Bounds(
                bounds.x - bounds.width + 2,
                bounds.y,
                bounds.width,
                bounds.height
            )
        if (pos.y > v.position.y)
            bounds = new Bounds(
                bounds.x,
                bounds.y + bounds.height / 2,
                bounds.width,
                bounds.height
            )

        return {
            text: v.label,
            fontSize,
            fontWeight,
            color: v.color,
            bounds,
            series,
            isMid: true,
        }
    })
}
 
// Make the end label (entity label) for a series. Will be pushed
// slightly out based on the direction of the series if multiple values
// are present
// This is also the one label in the case of a single point
export const makeEndLabel = (
    series: ScatterRenderSeries,
    isSubtleForeground: boolean,
    hideConnectedScatterLines: boolean
): ScatterLabel => {
    const lastValue = last(series.points) as ScatterRenderPoint
    const lastPos = lastValue.position
    const fontSize = hideConnectedScatterLines
        ? FONT_SIZE_WHEN_HIDDEN_LINES
        : lastValue.fontSize *
          (series.isForeground ? (isSubtleForeground ? 1.2 : 1.3) : 1.1)
    const fontWeight =
        series.isForeground && !hideConnectedScatterLines ? 700 : 400
 
    let offsetVector = PointVector.up
    if (series.points.length > 1) {
        const prevValue = series.points[series.points.length - 2]
        const prevPos = prevValue.position
        offsetVector = lastPos.subtract(prevPos)
    }
    series.offsetVector = offsetVector
 
    const labelPos = lastPos.add(
        offsetVector
            .normalize()
            .times(series.points.length === 1 ? lastValue.size + 1 : 5)
    )
 
    let labelBounds = Bounds.forText(series.text, {
        x: labelPos.x,
        y: labelPos.y,
        fontSize: fontSize,
        fontFamily,
    })
 
    if (labelPos.x < lastPos.x)
        labelBounds = labelBounds.set({
            x: labelBounds.x - labelBounds.width,
        })
    if (labelPos.y > lastPos.y)
        labelBounds = labelBounds.set({
            y: labelBounds.y + labelBounds.height / 2,
        })
 
    return {
        text:
            hideConnectedScatterLines && series.isForeground
                ? lastValue.label
                : series.text,
        fontSize,
        fontWeight,
        color: lastValue.color,
        bounds: labelBounds,
        series,
        isEnd: true,
    }
}