forked from recharts/recharts
/
DefaultTooltipContent.tsx
143 lines (128 loc) · 4.47 KB
/
DefaultTooltipContent.tsx
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
/**
* @fileOverview Default Tooltip Content
*/
import _ from 'lodash';
import React, { PureComponent, CSSProperties, ReactNode } from 'react';
import classNames from 'classnames';
import { isNumOrStr } from '../util/DataUtils';
function defaultFormatter<T>(value: T) {
return _.isArray(value) && isNumOrStr(value[0]) && isNumOrStr(value[1]) ? value.join(' ~ ') : value;
}
export type TooltipType = 'none';
export type ValueType = number | string | Array<number | string>;
export type NameType = number | string;
export type Formatter<TValue extends ValueType, TName extends NameType> = (
value: TValue,
name: TName,
item: Payload<TValue, TName>,
index: number,
payload: Array<Payload<TValue, TName>>,
) => [ReactNode, ReactNode] | ReactNode;
export interface Payload<TValue extends ValueType, TName extends NameType> {
type?: TooltipType;
color?: string;
formatter?: Formatter<TValue, TName>;
name?: TName;
value?: TValue;
unit?: ReactNode;
dataKey?: string | number;
payload?: any;
chartType?: string;
}
export interface Props<TValue extends ValueType, TName extends NameType> {
separator?: string;
wrapperClassName?: string;
labelClassName?: string;
formatter?: Formatter<TValue, TName>;
contentStyle?: CSSProperties;
itemStyle?: CSSProperties;
labelStyle?: CSSProperties;
labelFormatter?: (label: any, payload: Array<Payload<TValue, TName>>) => ReactNode;
label?: any;
payload?: Array<Payload<TValue, TName>>;
itemSorter?: (item: Payload<TValue, TName>) => number | string;
}
export class DefaultTooltipContent<TValue extends ValueType, TName extends NameType> extends PureComponent<
Props<TValue, TName>
> {
static displayName = 'DefaultTooltipContent';
static defaultProps = {
separator: ' : ',
contentStyle: {},
itemStyle: {},
labelStyle: {},
};
renderContent() {
const { payload, separator, formatter, itemStyle, itemSorter } = this.props;
if (payload && payload.length) {
const listStyle = { padding: 0, margin: 0 };
const items = (itemSorter ? _.sortBy(payload, itemSorter) : payload).map((entry, i) => {
if (entry.type === 'none') {
return null;
}
const finalItemStyle = {
display: 'block',
paddingTop: 4,
paddingBottom: 4,
color: entry.color || '#000',
...itemStyle,
};
const finalFormatter = entry.formatter || formatter || defaultFormatter;
let { value, name } = entry;
if (finalFormatter && value != null && name != null) {
const formatted = finalFormatter(value, name, entry, i, payload);
if (Array.isArray(formatted)) {
[value, name] = formatted;
} else {
value = formatted;
}
}
return (
// eslint-disable-next-line react/no-array-index-key
<li className="recharts-tooltip-item" key={`tooltip-item-${i}`} style={finalItemStyle}>
{isNumOrStr(name) ? <span className="recharts-tooltip-item-name">{name}</span> : null}
{isNumOrStr(name) ? <span className="recharts-tooltip-item-separator">{separator}</span> : null}
<span className="recharts-tooltip-item-value">{value}</span>
<span className="recharts-tooltip-item-unit">{entry.unit || ''}</span>
</li>
);
});
return (
<ul className="recharts-tooltip-item-list" style={listStyle}>
{items}
</ul>
);
}
return null;
}
render() {
const { wrapperClassName, contentStyle, labelClassName, labelStyle, label, labelFormatter, payload } = this.props;
const finalStyle: CSSProperties = {
margin: 0,
padding: 10,
backgroundColor: '#fff',
border: '1px solid #ccc',
whiteSpace: 'nowrap',
...contentStyle,
};
const finalLabelStyle = {
margin: 0,
...labelStyle,
};
const hasLabel = !_.isNil(label);
let finalLabel = hasLabel ? label : '';
const wrapperCN = classNames('recharts-default-tooltip', wrapperClassName);
const labelCN = classNames('recharts-tooltip-label', labelClassName);
if (hasLabel && labelFormatter && payload !== undefined && payload !== null) {
finalLabel = labelFormatter(label, payload);
}
return (
<div className={wrapperCN} style={finalStyle}>
<p className={labelCN} style={finalLabelStyle}>
{React.isValidElement(finalLabel) ? finalLabel : `${finalLabel}`}
</p>
{this.renderContent()}
</div>
);
}
}