Skip to content

Commit 1c60a28

Browse files
committed
implement a custom tooltip for incidents
1 parent bc5943c commit 1c60a28

File tree

2 files changed

+64
-14
lines changed

2 files changed

+64
-14
lines changed

web/src/components/Incidents/IncidentsChart/IncidentsChart.tsx

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useMemo, useEffect, useRef, useCallback } from 'react';
1+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
22

33
import {
44
Chart,
@@ -8,7 +8,6 @@ import {
88
ChartLabel,
99
ChartLegend,
1010
ChartThemeColor,
11-
ChartTooltip,
1211
ChartVoronoiContainer,
1312
} from '@patternfly/react-charts/victory';
1413
import {
@@ -25,16 +24,53 @@ import {
2524
t_global_color_status_warning_default,
2625
} from '@patternfly/react-tokens';
2726
import { useDispatch, useSelector } from 'react-redux';
27+
import { VictoryPortal } from 'victory';
2828
import { setAlertsAreLoading, setChooseIncident } from '../../../actions/observe';
2929
import { MonitoringState } from '../../../reducers/observe';
30+
import '../incidents-styles.css';
3031
import { Incident } from '../model';
3132
import {
3233
createIncidentsChartBars,
3334
formatDate,
3435
generateDateArray,
3536
updateBrowserUrl,
3637
} from '../utils';
37-
import { VictoryPortal } from 'victory';
38+
39+
const TOOLTIP_MAX_HEIGHT = 300;
40+
const TOOLTIP_MAX_WIDTH = 500;
41+
42+
const IncidentsTooltip = ({
43+
x,
44+
y,
45+
x0,
46+
height,
47+
text,
48+
}: {
49+
x?: number;
50+
y?: number;
51+
x0?: number;
52+
height?: number;
53+
text?: string | Array<string>;
54+
}) => {
55+
const posx = x - (x - x0) / 2 - TOOLTIP_MAX_WIDTH / 2;
56+
const posy = y - Math.min(height || 0, TOOLTIP_MAX_HEIGHT) / 2 - 20;
57+
const textArray: Array<string> = Array.isArray(text) ? text : [text];
58+
59+
return (
60+
<VictoryPortal>
61+
<foreignObject height={TOOLTIP_MAX_HEIGHT} width={TOOLTIP_MAX_WIDTH} x={posx} y={posy}>
62+
<div className="incidents__tooltip-wrap">
63+
<div className="incidents__tooltip">
64+
{textArray.map((text, index) => (
65+
<p key={index}>{text}</p>
66+
))}
67+
</div>
68+
<div className="incidents__tooltip-arrow" />
69+
</div>
70+
</foreignObject>
71+
</VictoryPortal>
72+
);
73+
};
3874

3975
const IncidentsChart = ({
4076
incidentsData,
@@ -132,17 +168,7 @@ const IncidentsChart = ({
132168
<Chart
133169
containerComponent={
134170
<ChartVoronoiContainer
135-
labelComponent={
136-
<VictoryPortal>
137-
<ChartTooltip
138-
activateData={false}
139-
orientation="top"
140-
dx={({ x, x0 }: any) => -(x - x0) / 2}
141-
dy={-5} // Position tooltip so pointer appears above bar
142-
labelComponent={<ChartLabel />}
143-
/>
144-
</VictoryPortal>
145-
}
171+
labelComponent={<IncidentsTooltip />}
146172
voronoiPadding={0}
147173
labels={({ datum }) => {
148174
if (datum.nodata) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
11
.expanded-details-text-margin {
22
margin-left: 2px;
33
}
4+
5+
.incidents__tooltip-wrap {
6+
display: flex;
7+
flex-direction: column;
8+
align-items: center;
9+
}
10+
11+
.incidents__tooltip {
12+
background-color: var(--pf-t--global--background--color--inverse--default);
13+
border-radius: var(--pf-t--global--border--radius--small);
14+
color: var(--pf-t--global--text--color--inverse);
15+
font-size: 12px;
16+
overflow-x: hidden;
17+
padding: 10px;
18+
}
19+
20+
.incidents__tooltip-arrow {
21+
border-left: 12px solid transparent;
22+
border-top: 12px solid var(--pf-t--global--background--color--inverse--default);
23+
border-right: 12px solid transparent;
24+
height: 0;
25+
margin-top: -1px;
26+
width: 0;
27+
}

0 commit comments

Comments
 (0)