File size: 3,026 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { memo } from 'react'
import { Chip } from '@nivo/tooltip'
import { useTheme, Theme } from '@nivo/theming'
import { BoxPlotSummaryFormatted, BoxPlotTooltipProps, BoxPlotSummary } from './types'

interface BoxPlotSummaryTooltipProps {
    label: string
    formatted: BoxPlotSummaryFormatted
    color?: string
    enableChip?: boolean
}

type Translation = Record<string, number | string>

export const defaultTranslation = {
    n: 'n',
    mean: 'mean',
    min: 'min',
    max: 'max',
    Summary: 'Summary',
    Quantiles: 'Quantiles',
}

type ExtendedTheme = Theme & {
    translation: Translation
}

const hasTranslation = (theme: Theme | ExtendedTheme): theme is ExtendedTheme => {
    return 'translation' in theme
}

export const BoxPlotSummaryTooltip = memo<BoxPlotSummaryTooltipProps>(
    ({ label, formatted, enableChip = false, color }) => {
        const theme = useTheme()
        let translation = defaultTranslation
        if (hasTranslation(theme)) {
            translation = {
                ...defaultTranslation,
                ...theme.translation,
            }
        }

        const quantiles = formatted.quantiles.map((q, i) => (
            <div key={'quantile.' + i}>
                {q}%: <strong>{formatted.values[i]}</strong>
            </div>
        ))
        return (
            <div style={theme.tooltip.container}>
                <div style={theme.tooltip.basic}>
                    {enableChip && <Chip color={color ?? ''} style={theme.tooltip.chip} />}
                    {label}
                </div>
                <div style={{ display: 'flex', marginTop: '1rem' }}>
                    <div style={{ marginRight: '2rem' }}>
                        <div>
                            {translation.n}: <strong>{formatted.n}</strong>
                        </div>
                        <div style={{ marginTop: '1rem' }}>{translation.Summary}</div>
                        <div>
                            {translation.mean}: <strong>{formatted.mean}</strong>
                        </div>
                        <div>
                            {translation.min}: <strong>{formatted.extrema[0]}</strong>
                        </div>
                        <div>
                            {translation.max}: <strong>{formatted.extrema[1]}</strong>
                        </div>
                    </div>
                    <div>
                        <div>{translation.Quantiles}</div>
                        {quantiles}
                    </div>
                </div>
            </div>
        )
    }
)

export const BoxPlotTooltip = ({ color, label, formatted }: BoxPlotTooltipProps) => {
    return (
        <BoxPlotSummaryTooltip
            label={label}
            formatted={formatted}
            enableChip={true}
            color={color}
        />
    )
}

export const BoxPlotTooltipLabel = (datum: BoxPlotSummary) => {
    if (datum.subGroup) {
        return datum.group + ' - ' + datum.subGroup
    }
    return datum.group
}