Skip to content

Commit bef1fc7

Browse files
authored
feat: hide tooltip when over line annotation (#339)
This commit add a new prop for the line annotation to disable the tooltip when hovering over a line annotation. This will keep showing the tooltip over the marker, if available. fix #324
1 parent e94dab9 commit bef1fc7

File tree

5 files changed

+67
-42
lines changed

5 files changed

+67
-42
lines changed

.playground/playgroud.tsx

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ import {
99
Position,
1010
ScaleType,
1111
Settings,
12-
LineSeries,
12+
BarSeries,
13+
LineAnnotation,
14+
getAnnotationId,
15+
AnnotationDomainTypes,
1316
} from '../src';
1417
import { KIBANA_METRICS } from '../src/utils/data_samples/test_dataset_kibana';
1518
import { CursorEvent } from '../src/specs/settings';
1619
import { CursorUpdateListener } from '../src/chart_types/xy_chart/store/chart_state';
20+
import { Icon } from '../src/components/icons/icon';
1721

1822
export class Playground extends React.Component {
1923
ref1 = React.createRef<Chart>();
@@ -65,6 +69,7 @@ function renderChart(
6569
legendPosition={Position.Right}
6670
showLegend={true}
6771
onCursorUpdate={onCursorUpdate}
72+
rotation={0}
6873
/>
6974
<Axis
7075
id={getAxisId('timestamp')}
@@ -73,21 +78,31 @@ function renderChart(
7378
tickFormat={niceTimeFormatter([1555819200000, 1555905600000])}
7479
/>
7580
<Axis id={getAxisId('count')} title="count" position={Position.Left} tickFormat={(d) => d.toFixed(2)} />
76-
<LineSeries
77-
id={getSpecId('dataset A with a really really really really long title')}
81+
<LineAnnotation
82+
annotationId={getAnnotationId('annotation1')}
83+
domainType={AnnotationDomainTypes.XDomain}
84+
dataValues={[
85+
{
86+
dataValue: KIBANA_METRICS.metrics.kibana_os_load[1].data[5][0],
87+
details: 'tooltip 1',
88+
},
89+
{
90+
dataValue: KIBANA_METRICS.metrics.kibana_os_load[1].data[9][0],
91+
details: 'tooltip 2',
92+
},
93+
]}
94+
hideLinesTooltips={true}
95+
marker={<Icon type="alert" />}
96+
/>
97+
<BarSeries
98+
id={getSpecId('dataset A with long title')}
7899
xScaleType={timeSeries ? ScaleType.Time : ScaleType.Linear}
79100
yScaleType={ScaleType.Linear}
80101
data={data}
81102
xAccessor={0}
82-
lineSeriesStyle={{
83-
line: {
84-
stroke: 'red',
85-
opacity: 1,
86-
},
87-
}}
88103
yAccessors={[1]}
89104
/>
90-
<LineSeries
105+
<BarSeries
91106
id={getSpecId('dataset B')}
92107
xScaleType={ScaleType.Time}
93108
yScaleType={ScaleType.Linear}
@@ -96,15 +111,6 @@ function renderChart(
96111
yAccessors={[1]}
97112
stackAccessors={[0]}
98113
/>
99-
<LineSeries
100-
id={getSpecId('dataset C')}
101-
xScaleType={ScaleType.Time}
102-
yScaleType={ScaleType.Linear}
103-
data={KIBANA_METRICS.metrics.kibana_os_load[1].data.slice(0, 15)}
104-
xAccessor={0}
105-
yAccessors={[1]}
106-
stackAccessors={[0]}
107-
/>
108114
</Chart>
109115
</div>
110116
);

src/chart_types/xy_chart/annotations/annotation_utils.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,18 @@ describe('annotation utils', () => {
12941294
};
12951295

12961296
expect(rectTooltipState).toEqual(expectedRectTooltipState);
1297+
annotationRectangle.hideTooltips = true;
1298+
1299+
const rectHideTooltipState = computeAnnotationTooltipState(
1300+
{ x: 3, y: 4 },
1301+
annotationDimensions,
1302+
rectAnnotations,
1303+
chartRotation,
1304+
localAxesSpecs,
1305+
chartDimensions,
1306+
);
1307+
1308+
expect(rectHideTooltipState).toBe(null);
12971309
});
12981310

12991311
test('should get associated axis for an annotation', () => {

src/chart_types/xy_chart/annotations/annotation_utils.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ export function isWithinLineBounds(
573573
chartDimensions: Dimensions,
574574
domainType: AnnotationDomainType,
575575
marker?: AnnotationMarker,
576+
hideLinesTooltips?: boolean,
576577
): boolean {
577578
const [startX, startY, endX, endY] = linePosition;
578579
const isXDomainAnnotation = isXDomain(domainType);
@@ -584,26 +585,26 @@ export function isWithinLineBounds(
584585
const isHorizontalChartRotation = isHorizontalRotation(chartRotation);
585586
const chartWidth = chartDimensions.width;
586587
const chartHeight = chartDimensions.height;
587-
588-
if (isXDomainAnnotation) {
589-
isCursorWithinXBounds = isHorizontalChartRotation
590-
? cursorPosition.x >= startX - offset && cursorPosition.x <= endX + offset
591-
: cursorPosition.x >= chartHeight - startX - offset && cursorPosition.x <= chartHeight - endX + offset;
592-
isCursorWithinYBounds = isHorizontalChartRotation
593-
? cursorPosition.y >= startY && cursorPosition.y <= endY
594-
: cursorPosition.y >= startY - offset && cursorPosition.y <= endY + offset;
595-
} else {
596-
isCursorWithinXBounds = isHorizontalChartRotation
597-
? cursorPosition.x >= startX && cursorPosition.x <= endX
598-
: cursorPosition.x >= startX - offset && cursorPosition.x <= endX + offset;
599-
isCursorWithinYBounds = isHorizontalChartRotation
600-
? cursorPosition.y >= startY - offset && cursorPosition.y <= endY + offset
601-
: cursorPosition.y >= chartWidth - startY - offset && cursorPosition.y <= chartWidth - endY + offset;
602-
}
603-
604-
// If it's within cursor bounds, return true (no need to check marker bounds)
605-
if (isCursorWithinXBounds && isCursorWithinYBounds) {
606-
return true;
588+
if (!hideLinesTooltips) {
589+
if (isXDomainAnnotation) {
590+
isCursorWithinXBounds = isHorizontalChartRotation
591+
? cursorPosition.x >= startX - offset && cursorPosition.x <= endX + offset
592+
: cursorPosition.x >= chartHeight - startX - offset && cursorPosition.x <= chartHeight - endX + offset;
593+
isCursorWithinYBounds = isHorizontalChartRotation
594+
? cursorPosition.y >= startY && cursorPosition.y <= endY
595+
: cursorPosition.y >= startY - offset && cursorPosition.y <= endY + offset;
596+
} else {
597+
isCursorWithinXBounds = isHorizontalChartRotation
598+
? cursorPosition.x >= startX && cursorPosition.x <= endX
599+
: cursorPosition.x >= startX - offset && cursorPosition.x <= endX + offset;
600+
isCursorWithinYBounds = isHorizontalChartRotation
601+
? cursorPosition.y >= startY - offset && cursorPosition.y <= endY + offset
602+
: cursorPosition.y >= chartWidth - startY - offset && cursorPosition.y <= chartWidth - endY + offset;
603+
}
604+
// If it's within cursor bounds, return true (no need to check marker bounds)
605+
if (isCursorWithinXBounds && isCursorWithinYBounds) {
606+
return true;
607+
}
607608
}
608609

609610
if (!marker) {
@@ -748,6 +749,7 @@ export function computeLineAnnotationTooltipState(
748749
chartRotation: Rotation,
749750
chartDimensions: Dimensions,
750751
axesSpecs: Map<AxisId, AxisSpec>,
752+
hideLinesTooltips?: boolean,
751753
): AnnotationTooltipState {
752754
const annotationTooltipState: AnnotationTooltipState = {
753755
isVisible: false,
@@ -778,6 +780,7 @@ export function computeLineAnnotationTooltipState(
778780
chartDimensions,
779781
domainType,
780782
line.marker,
783+
hideLinesTooltips,
781784
);
782785

783786
if (isWithinBounds) {
@@ -991,14 +994,14 @@ export function computeAnnotationTooltipState(
991994
for (const [annotationId, annotationDimension] of annotationDimensions) {
992995
const spec = annotationSpecs.get(annotationId);
993996

994-
if (!spec) {
997+
if (!spec || spec.hideTooltips) {
995998
continue;
996999
}
9971000

9981001
const groupId = spec.groupId;
9991002

10001003
if (isLineAnnotation(spec)) {
1001-
if (spec.hideTooltips || spec.hideLines) {
1004+
if (spec.hideLines) {
10021005
continue;
10031006
}
10041007

@@ -1011,6 +1014,7 @@ export function computeAnnotationTooltipState(
10111014
chartRotation,
10121015
chartDimensions,
10131016
axesSpecs,
1017+
spec.hideLinesTooltips,
10141018
);
10151019

10161020
if (lineAnnotationTooltipState.isVisible) {

src/chart_types/xy_chart/utils/specs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ export type LineAnnotationSpec = BaseAnnotationSpec & {
301301
};
302302
/** Annotation lines are hidden */
303303
hideLines?: boolean;
304+
/** Hide tooltip when hovering over the line */
305+
hideLinesTooltips?: boolean;
304306
/** z-index of the annotation relative to other elements in the chart
305307
* @default 1
306308
*/

stories/annotations.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ storiesOf('Annotations', module)
331331
{
332332
coordinates: {
333333
x0: 'a',
334-
x1: 'b.5',
334+
x1: 'b',
335335
},
336336
details: 'details about this annotation',
337337
},
@@ -535,6 +535,7 @@ storiesOf('Annotations', module)
535535
style={style}
536536
renderTooltip={renderTooltip}
537537
zIndex={zIndex}
538+
hideTooltips={boolean('hide tooltips', false)}
538539
/>
539540
<Axis id={getAxisId('bottom')} position={xAxisPosition} title={xAxisTitle} />
540541
<Axis id={getAxisId('left')} title={yAxisTitle} position={yAxisPosition} />

0 commit comments

Comments
 (0)