All files / owid-grapher/grapher/mapCharts MapTooltip.tsx

93.4% Statements 184/197
55.17% Branches 16/29
100% Functions 14/14
93.4% Lines 184/197

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 2271x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x     1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                  
import * as React from "react"
import { computed } from "mobx"
import { observer } from "mobx-react"
import { Tooltip } from "../tooltip/Tooltip"
import { takeWhile, last, first, isMobile } from "../../clientUtils/Util"
import {
    SparkBars,
    SparkBarsDatum,
    SparkBarsProps,
} from "../sparkBars/SparkBars"
import { SparkBarTimeSeriesValue } from "../sparkBars/SparkBarTimeSeriesValue"
import { MapChartManager, ChoroplethSeries } from "./MapChartConstants"
import { ColorScale } from "../color/ColorScale"
import { Time } from "../../coreTable/CoreTableConstants"
import { ChartTypeName } from "../core/GrapherConstants"
import { OwidTable } from "../../coreTable/OwidTable"
import { CoreColumn } from "../../coreTable/CoreTableColumns"
 
interface MapTooltipProps {
    tooltipDatum?: ChoroplethSeries
    tooltipTarget?: { x: number; y: number; featureId: string }
    isEntityClickable?: boolean
    manager: MapChartManager
    colorScale?: ColorScale
    targetTime?: Time
}
 
@observer
export class MapTooltip extends React.Component<MapTooltipProps> {
    private sparkBarsDatumXAccessor = (d: SparkBarsDatum): number => d.time
 
    @computed private get sparkBarsToDisplay(): number {
        return isMobile() ? 13 : 20
    }
 
    @computed private get sparkBarsProps(): SparkBarsProps<SparkBarsDatum> {
        return {
            data: this.sparkBarsData,
            x: this.sparkBarsDatumXAccessor,
            y: (d: SparkBarsDatum): number => d.value,
            xDomain: this.sparkBarsDomain,
        }
    }
 
    @computed get inputTable(): OwidTable {
        return this.props.manager.table
    }
 
    @computed private get mapColumnSlug(): string | undefined {
        return this.props.manager.mapColumnSlug
    }
 
    // Uses the rootTable because if a target year is set, we filter the years at the grapher level.
    // Todo: might want to do all filtering a step below the Grapher level?
    @computed private get sparkBarColumn(): CoreColumn {
        return this.inputTable.rootTable.get(this.mapColumnSlug)
    }
 
    @computed private get sparkBarsData(): SparkBarsDatum[] {
        const tooltipDatum = this.props.tooltipDatum
        if (!tooltipDatum) return []
 
        const sparkBarValues: SparkBarsDatum[] = []
        this.sparkBarColumn.valueByEntityNameAndOriginalTime
            .get(tooltipDatum.seriesName)
            ?.forEach((value, key) => {
                sparkBarValues.push({
                    time: key,
                    value: value as number,
                })
            })
 
        return takeWhile(
            sparkBarValues,
            (d) => d.time <= tooltipDatum.time
        ).slice(-this.sparkBarsToDisplay)
    }
 
    @computed private get sparkBarsDomain(): [number, number] {
        const lastVal = last(this.sparkBarsData)
 
        const end = lastVal ? this.sparkBarsDatumXAccessor(lastVal) : 0
        const start = end > 0 ? end - this.sparkBarsToDisplay + 1 : 0
 
        return [start, end]
    }
 
    @computed private get currentSparkBar(): number | undefined {
        const lastVal = last(this.sparkBarsData)
        return lastVal ? this.sparkBarsDatumXAccessor(lastVal) : undefined
    }
 
    colorScale = this.props.colorScale ?? new ColorScale()
 
    @computed private get renderSparkBars(): boolean | undefined {
        return this.props.manager.mapIsClickable
    }
 
    @computed private get darkestColorInColorScheme(): string | undefined {
        const { colorScale } = this
        return colorScale.isColorSchemeInverted
            ? first(colorScale.baseColors)
            : last(colorScale.baseColors)
    }
 
    @computed private get barColor(): string | undefined {
        const { colorScale } = this
        return colorScale.singleColorScale &&
            !colorScale.customNumericColorsActive
            ? this.darkestColorInColorScheme
            : undefined
    }
 
    @computed private get tooltipTarget(): {
        x: number
        y: number
        featureId: string
    } {
        return (
            this.props.tooltipTarget ?? {
                x: 0,
                y: 0,
                featureId: "Default Tooltip",
            }
        )
    }
 
    render(): JSX.Element {
        const { tooltipDatum, isEntityClickable, targetTime, manager } =
            this.props
 
        const clickToSelectMessage =
            manager.type === ChartTypeName.LineChart
                ? "Click for change over time"
                : "Click to select"
 
        const { timeColumn } = this.inputTable
        const { renderSparkBars, barColor, tooltipTarget } = this
 
        const displayTime = !timeColumn.isMissing
            ? timeColumn.formatValue(targetTime)
            : targetTime
        const displayDatumTime =
            timeColumn && tooltipDatum
                ? timeColumn.formatValue(tooltipDatum.time)
                : tooltipDatum?.time.toString() ?? ""
 
        return (
            <Tooltip
                tooltipManager={this.props.manager}
                key="mapTooltip"
                x={tooltipTarget.x}
                y={tooltipTarget.y}
                style={{ textAlign: "center", padding: "8px" }}
                offsetX={15}
                offsetY={10}
                offsetYDirection={"upward"}
            >
                <h3
                    style={{
                        padding: "0.3em 0.3em",
                        margin: 0,
                        fontWeight: "normal",
                        fontSize: "1em",
                    }}
                >
                    {tooltipTarget.featureId ||
                        tooltipTarget.featureId.replace(/_/g, " ")}
                </h3>
                <div
                    style={{
                        margin: 0,
                        padding: "0.3em 0.3em",
                    }}
                >
                    {tooltipDatum ? (
                        <div className="map-tooltip">
                            <div className="trend">
                                {renderSparkBars && (
                                    <div className="plot">
                                        <SparkBars<SparkBarsDatum>
                                            {...this.sparkBarsProps}
                                            currentX={this.currentSparkBar}
                                            color={barColor}
                                        />
                                    </div>
                                )}
                                <div
                                    className={
                                        "value" +
                                        (renderSparkBars ? "" : " no-plot")
                                    }
                                >
                                    <SparkBarTimeSeriesValue
                                        className="current"
                                        value={tooltipDatum.displayValue}
                                        displayDate={displayDatumTime}
                                        valueColor={
                                            renderSparkBars ? barColor : "black"
                                        }
                                    />
                                </div>
                            </div>
                        </div>
                    ) : (
                        `No data for ${displayTime}`
                    )}
                </div>
                {isEntityClickable && (
                    <div>
                        <p
                            style={{
                                margin: 0,
                                padding: "0.3em 0.9em",
                                fontSize: "13px",
                                opacity: 0.6,
                            }}
                        >
                            {clickToSelectMessage}
                        </p>
                    </div>
                )}
            </Tooltip>
        )
    }
}