forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtooltip.tsx
More file actions
102 lines (95 loc) · 3.86 KB
/
tooltip.tsx
File metadata and controls
102 lines (95 loc) · 3.86 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
import defaultsDeep from 'lodash/defaultsDeep';
import { ChartsOptionProps } from '../Charts';
import { getMarker, getSymbol } from './symbol';
/**
* Returns a custom legend tooltip for Line charts
*
* @param series
* @param option
* @param echart
* @private Not intended as public API and subject to change
*/
export const getLegendTooltip = (series: any[], option: ChartsOptionProps, echart: any) => {
const tooltip = option?.tooltip as any;
const valueFormatter = tooltip?.valueFormatter ? tooltip.valueFormatter : (value: number | string) => value;
const defaults = {
confine: true,
formatter: (params: any) => {
let result = '';
params?.map((param, index) => {
if (index === 0) {
result += `<p style="text-align:left; padding-bottom: 5px;">${param.name}</p>`;
}
const symbol = getSymbol(series, param.seriesIndex, echart);
const marker = getMarker(series[param.seriesIndex], symbol, param.color);
const formattedValue = valueFormatter(param.value, param.dataIndex);
const seriesName = `<span style="margin-left: 10px;">${param.seriesName}</span>`;
const value = `<strong style="float:right; margin-left: 20px;">${formattedValue}</strong>`;
result += `<p style="text-align:left;">${marker}${seriesName}${value}</p>`;
});
return result;
},
trigger: 'axis'
};
return defaultsDeep(tooltip, defaults);
};
/**
* Returns source and target colors from given series
*
* @param series
* @param formatterParams
* @private Not intended as public API and subject to change
*/
const getItemColor = (series: any[], formatterParams: any) => {
const serie = series[formatterParams.seriesIndex];
const sourceData = serie?.data.find((datum: any) => datum.name === formatterParams.data?.source);
const targetData = serie?.data.find((datum: any) => datum.name === formatterParams.data?.target);
const sourceColor = sourceData?.itemStyle?.color;
const targetColor = targetData?.itemStyle?.color;
return { sourceColor, targetColor };
};
/**
* Returns a custom legend tooltip for Sankey chart
*
* @param series
* @param option
* @private Not intended as public API and subject to change
*/
export const getSankeyTooltip = (series: any[], option: ChartsOptionProps) => {
const symbolSize = '10px';
const tooltip = option?.tooltip as any;
const sourceLabel = tooltip?.sourceLabel ? tooltip.sourceLabel : '';
const destinationLabel = tooltip?.destinationLabel ? tooltip.destinationLabel : '';
const valueFormatter = tooltip?.valueFormatter ? tooltip.valueFormatter : (value: number | string) => value;
const defaults = {
confine: true,
formatter: (params: any) => {
let result;
if (params?.data?.source && params?.data?.target) {
const { sourceColor, targetColor } = getItemColor(series, params);
result = `
<p>${sourceLabel}</p>
<div style="display: inline-block; background-color: ${sourceColor}; height: ${symbolSize}; width: ${symbolSize};"></div>
${params.data.source}
<p style="padding-top: 10px;">${destinationLabel}</p>
<p style="text-align:left;">
<div style="display: inline-block; background-color: ${targetColor}; height: ${symbolSize}; width: ${symbolSize};"></div>
${params.data.target}
<strong style="float:right;">
${valueFormatter(params.value, params.dataIndex)}
</strong>
</p>
`;
} else {
result = `
<div style="display: inline-block; background-color: ${params.color}; height: ${symbolSize}; width: ${symbolSize};"></div>
${params.name} ${valueFormatter(params.value, params.dataIndex)}
`;
}
return result.replace(/\s\s+/g, ' ');
},
trigger: 'item',
triggerOn: 'mousemove'
};
return defaultsDeep(tooltip, defaults);
};