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

100% Statements 109/109
100% Branches 9/9
100% Functions 9/9
100% Lines 109/109

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 1331x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 4x 4x 1x 4x 4x 1x 4x 4x 1x 1x 4x 4x 4x 4x 4x 4x 4x 1x 1x 4x 4x 4x 4x 4x 4x 4x 1x 1x 4x 4x 1x 1x 4x 4x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x                                                
import * as React from "react"
import { computed } from "mobx"
import { Triangle } from "./Triangle"
import { TextWrap } from "../text/TextWrap"
import { BASE_FONT_SIZE } from "../core/GrapherConstants"
 
export interface ConnectedScatterLegendManager {
    sidebarWidth: number
    displayStartTime: string
    displayEndTime: string
    fontSize?: number
    compareEndPointsOnly?: boolean
}
 
export class ConnectedScatterLegend {
    manager: ConnectedScatterLegendManager
    constructor(manager: ConnectedScatterLegendManager) {
        this.manager = manager
    }
 
    @computed get fontSize(): number {
        return 0.7 * (this.manager.fontSize ?? BASE_FONT_SIZE)
    }
    @computed get fontColor(): string {
        return "#333"
    }
    @computed get maxLabelWidth(): number {
        return this.manager.sidebarWidth / 3
    }
 
    @computed get startLabel(): TextWrap {
        const { manager, maxLabelWidth, fontSize } = this
        return new TextWrap({
            text: manager.displayStartTime,
            fontSize,
            maxWidth: maxLabelWidth,
        })
    }
 
    @computed get endLabel(): TextWrap {
        const { manager, maxLabelWidth, fontSize } = this
        return new TextWrap({
            text: manager.displayEndTime,
            fontSize,
            maxWidth: maxLabelWidth,
        })
    }
 
    @computed get width(): number {
        return this.manager.sidebarWidth
    }
 
    @computed get height(): number {
        return Math.max(this.startLabel.height, this.endLabel.height)
    }
 
    render(
        targetX: number,
        targetY: number,
        renderOptions: React.SVGAttributes<SVGGElement> = {}
    ): JSX.Element {
        const { manager, startLabel, endLabel, fontColor } = this
 
        const lineLeft = targetX + startLabel.width + 5
        const lineRight = targetX + manager.sidebarWidth - endLabel.width - 5
        const lineY = targetY + this.height / 2 - 0.5
 
        return (
            <g className="ConnectedScatterLegend" {...renderOptions}>
                <rect
                    x={targetX}
                    y={targetY}
                    width={this.width}
                    height={this.height}
                    fill="#fff"
                    opacity={0}
                />
                {startLabel.render(targetX, targetY, { fill: fontColor })}
                {endLabel.render(
                    targetX + manager.sidebarWidth - endLabel.width,
                    targetY,
                    { fill: fontColor }
                )}
                <line
                    x1={lineLeft}
                    y1={lineY}
                    x2={lineRight}
                    y2={lineY}
                    stroke="#666"
                    strokeWidth={1}
                />
                <circle
                    cx={lineLeft}
                    cy={lineY}
                    r={2}
                    fill="#666"
                    stroke="#ccc"
                    strokeWidth={0.2}
                />
                {!manager.compareEndPointsOnly && (
                    <React.Fragment>
                        <circle
                            cx={lineLeft + (lineRight - lineLeft) / 3}
                            cy={lineY}
                            r={2}
                            fill="#666"
                            stroke="#ccc"
                            strokeWidth={0.2}
                        />
                        <circle
                            cx={lineLeft + (2 * (lineRight - lineLeft)) / 3}
                            cy={lineY}
                            r={2}
                            fill="#666"
                            stroke="#ccc"
                            strokeWidth={0.2}
                        />
                    </React.Fragment>
                )}
                <Triangle
                    cx={lineRight}
                    cy={lineY}
                    r={3}
                    fill="#666"
                    stroke="#ccc"
                    strokeWidth={0.2}
                    transform={`rotate(${90}, ${lineRight}, ${lineY})`}
                />
            </g>
        )
    }
}