Skip to content

Commit 6407eed

Browse files
Merge pull request #513 from jgbernalp/fix-incidents-tooltip
OU-769: implement a custom tooltip for incidents
2 parents bc5943c + 241f429 commit 6407eed

File tree

4 files changed

+79
-32
lines changed

4 files changed

+79
-32
lines changed

web/src/components/Incidents/AlertsChart/AlertsChart.tsx

Lines changed: 14 additions & 18 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,
@@ -7,21 +7,26 @@ import {
77
ChartGroup,
88
ChartLabel,
99
ChartLegend,
10-
ChartTooltip,
1110
ChartVoronoiContainer,
1211
} from '@patternfly/react-charts/victory';
13-
import { Card, CardBody, CardTitle, EmptyState, EmptyStateBody } from '@patternfly/react-core';
14-
import { createAlertsChartBars, formatDate, generateDateArray } from '../utils';
15-
import { getResizeObserver } from '@patternfly/react-core';
16-
import { useDispatch, useSelector } from 'react-redux';
17-
import { setAlertsAreLoading } from '../../../actions/observe';
12+
import {
13+
Card,
14+
CardBody,
15+
CardTitle,
16+
EmptyState,
17+
EmptyStateBody,
18+
getResizeObserver,
19+
} from '@patternfly/react-core';
1820
import {
1921
t_global_color_status_danger_default,
2022
t_global_color_status_info_default,
2123
t_global_color_status_warning_default,
2224
} from '@patternfly/react-tokens';
25+
import { useDispatch, useSelector } from 'react-redux';
26+
import { setAlertsAreLoading } from '../../../actions/observe';
2327
import { MonitoringState } from '../../../reducers/observe';
24-
import { VictoryPortal } from 'victory';
28+
import { IncidentsTooltip } from '../IncidentsTooltip';
29+
import { createAlertsChartBars, formatDate, generateDateArray } from '../utils';
2530

2631
const AlertsChart = ({ chartDays, theme }: { chartDays: number; theme: 'light' | 'dark' }) => {
2732
const dispatch = useDispatch();
@@ -96,16 +101,7 @@ const AlertsChart = ({ chartDays, theme }: { chartDays: number; theme: 'light' |
96101
<Chart
97102
containerComponent={
98103
<ChartVoronoiContainer
99-
labelComponent={
100-
<VictoryPortal>
101-
<ChartTooltip
102-
orientation="top"
103-
dx={({ x, x0 }: any) => -(x - x0) / 2}
104-
dy={-5} // Position tooltip so pointer appears above bar
105-
labelComponent={<ChartLabel />}
106-
/>
107-
</VictoryPortal>
108-
}
104+
labelComponent={<IncidentsTooltip />}
109105
labels={({ datum }) => {
110106
if (datum.nodata) {
111107
return null;

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

Lines changed: 4 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 {
@@ -27,14 +26,15 @@ import {
2726
import { useDispatch, useSelector } from 'react-redux';
2827
import { setAlertsAreLoading, setChooseIncident } from '../../../actions/observe';
2928
import { MonitoringState } from '../../../reducers/observe';
29+
import '../incidents-styles.css';
30+
import { IncidentsTooltip } from '../IncidentsTooltip';
3031
import { Incident } from '../model';
3132
import {
3233
createIncidentsChartBars,
3334
formatDate,
3435
generateDateArray,
3536
updateBrowserUrl,
3637
} from '../utils';
37-
import { VictoryPortal } from 'victory';
3838

3939
const IncidentsChart = ({
4040
incidentsData,
@@ -132,17 +132,7 @@ const IncidentsChart = ({
132132
<Chart
133133
containerComponent={
134134
<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-
}
135+
labelComponent={<IncidentsTooltip />}
146136
voronoiPadding={0}
147137
labels={({ datum }) => {
148138
if (datum.nodata) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { VictoryPortal } from 'victory';
2+
import './incidents-styles.css';
3+
4+
const TOOLTIP_MAX_HEIGHT = 300;
5+
const TOOLTIP_MAX_WIDTH = 500;
6+
export const IncidentsTooltip = ({
7+
x,
8+
y,
9+
x0,
10+
height,
11+
text,
12+
}: {
13+
x?: number;
14+
y?: number;
15+
x0?: number;
16+
height?: number;
17+
text?: string | Array<string>;
18+
}) => {
19+
const posx = x - (x - x0) / 2 - TOOLTIP_MAX_WIDTH / 2;
20+
const posy = y - Math.min(height || 0, TOOLTIP_MAX_HEIGHT) / 2 - 20;
21+
const textArray: Array<string> = Array.isArray(text) ? text : [text];
22+
23+
return (
24+
<VictoryPortal>
25+
<foreignObject height={TOOLTIP_MAX_HEIGHT} width={TOOLTIP_MAX_WIDTH} x={posx} y={posy}>
26+
<div className="incidents__tooltip-wrap">
27+
<div className="incidents__tooltip">
28+
{textArray.map((text, index) => (
29+
<p key={index}>{text}</p>
30+
))}
31+
</div>
32+
<div className="incidents__tooltip-arrow" />
33+
</div>
34+
</foreignObject>
35+
</VictoryPortal>
36+
);
37+
};
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)