-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDefaultContent.tsx
More file actions
174 lines (157 loc) · 6.72 KB
/
DefaultContent.tsx
File metadata and controls
174 lines (157 loc) · 6.72 KB
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
import React from 'react';
import {dateTime} from '@gravity-ui/date-utils';
import get from 'lodash/get';
import type {PreparedPieSeries, PreparedWaterfallSeries} from '../../hooks';
import {formatNumber} from '../../libs';
import type {
ChartSeriesData,
ChartXAxis,
ChartYAxis,
TooltipDataChunk,
TooltipDataChunkSankey,
TreemapSeriesData,
WaterfallSeriesData,
} from '../../types';
import {block, getDataCategoryValue, getWaterfallPointSubtotal} from '../../utils';
const b = block('tooltip');
type Props = {
hovered: TooltipDataChunk[];
xAxis?: ChartXAxis;
yAxis?: ChartYAxis;
};
const DEFAULT_DATE_FORMAT = 'DD.MM.YY';
const getRowData = (
fieldName: 'x' | 'y',
data: ChartSeriesData,
axis?: ChartXAxis | ChartYAxis,
) => {
switch (axis?.type) {
case 'category': {
const categories = get(axis, 'categories', [] as string[]);
return getDataCategoryValue({axisDirection: fieldName, categories, data});
}
case 'datetime': {
const value = get(data, fieldName);
if (!value) {
return undefined;
}
return dateTime({input: value}).format(DEFAULT_DATE_FORMAT);
}
case 'linear':
default: {
const value = get(data, fieldName) as unknown as number;
return formatNumber(value);
}
}
};
const getXRowData = (data: ChartSeriesData, xAxis?: ChartXAxis) => getRowData('x', data, xAxis);
const getYRowData = (data: ChartSeriesData, yAxis?: ChartYAxis) => getRowData('y', data, yAxis);
const getMeasureValue = (data: TooltipDataChunk[], xAxis?: ChartXAxis, yAxis?: ChartYAxis) => {
if (
data.every((item) => ['pie', 'treemap', 'waterfall', 'sankey'].includes(item.series.type))
) {
return null;
}
if (data.some((item) => item.series.type === 'bar-y')) {
return getYRowData(data[0]?.data, yAxis);
}
return getXRowData(data[0]?.data, xAxis);
};
export const DefaultContent = ({hovered, xAxis, yAxis}: Props) => {
const measureValue = getMeasureValue(hovered, xAxis, yAxis);
return (
<React.Fragment>
{measureValue && <div>{measureValue}</div>}
{hovered.map((seriesItem, i) => {
const {data, series, closest} = seriesItem;
const id = `${get(series, 'id')}_${i}`;
const color = get(series, 'color');
switch (series.type) {
case 'scatter':
case 'line':
case 'area':
case 'bar-x': {
const value = (
<React.Fragment>
{series.name}: {getYRowData(data, yAxis)}
</React.Fragment>
);
return (
<div key={id} className={b('content-row')}>
<div className={b('color')} style={{backgroundColor: color}} />
<div>{closest ? <b>{value}</b> : <span>{value}</span>}</div>
</div>
);
}
case 'waterfall': {
const isTotal = get(data, 'total', false);
const subTotal = getWaterfallPointSubtotal(
data as WaterfallSeriesData,
series as PreparedWaterfallSeries,
);
return (
<div key={`${id}_${get(data, 'x')}`}>
{!isTotal && (
<React.Fragment>
<div key={id} className={b('content-row')}>
<b>{getXRowData(data, xAxis)}</b>
</div>
<div className={b('content-row')}>
<span>{series.name} </span>
<span>{getYRowData(data, yAxis)}</span>
</div>
</React.Fragment>
)}
<div key={id} className={b('content-row')}>
{isTotal ? 'Total' : 'Subtotal'}: {subTotal}
</div>
</div>
);
}
case 'bar-y': {
const value = (
<React.Fragment>
{series.name}: {getXRowData(data, xAxis)}
</React.Fragment>
);
return (
<div key={id} className={b('content-row')}>
<div className={b('color')} style={{backgroundColor: color}} />
<div>{closest ? <b>{value}</b> : <span>{value}</span>}</div>
</div>
);
}
case 'pie':
case 'treemap': {
const seriesData = data as PreparedPieSeries | TreemapSeriesData;
return (
<div key={id} className={b('content-row')}>
<div className={b('color')} style={{backgroundColor: color}} />
<span>{seriesData.name || seriesData.id} </span>
<span>{seriesData.value}</span>
</div>
);
}
case 'sankey': {
const {target, data: source} = seriesItem as TooltipDataChunkSankey;
const value = source.links.find((d) => d.name === target?.name)?.value;
return (
<div key={id} className={b('content-row')}>
<div
className={b('color')}
style={{backgroundColor: source.color}}
/>
<div style={{display: 'flex', gap: 8, verticalAlign: 'center'}}>
{source.name} <span>→</span> {target?.name}: {value}
</div>
</div>
);
}
default: {
return null;
}
}
})}
</React.Fragment>
);
};