Timeline: tooltip fixups (#35145)

This commit is contained in:
Leon Sorokin 2021-06-03 04:46:32 -05:00 committed by GitHub
parent 057ba4a6e7
commit a3ba605aff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 16 deletions

View File

@ -47,8 +47,8 @@ export interface TimelineCoreOptions {
getTimeRange: () => TimeRange;
formatValue?: (seriesIdx: number, value: any) => string;
getFieldConfig: (seriesIdx: number) => TimelineFieldConfig;
onHover?: (seriesIdx: number, valueIdx: number, rect: Rect) => void;
onLeave?: () => void;
onHover: (seriesIdx: number, valueIdx: number, rect: Rect) => void;
onLeave: () => void;
}
/**
@ -416,12 +416,11 @@ export function getConfig(opts: TimelineCoreOptions) {
if (foundAtCursor) {
if (foundAtCursor !== hoveredAtCursor) {
hoveredAtCursor = foundAtCursor;
// @ts-ignore
onHover && onHover(foundAtCursor.sidx, foundAtCursor.didx, foundAtCursor);
onHover(foundAtCursor!.sidx, foundAtCursor!.didx, foundAtCursor);
}
} else if (hoveredAtCursor) {
hoveredAtCursor = null;
onLeave && onLeave();
onLeave();
}
}
@ -439,13 +438,12 @@ export function getConfig(opts: TimelineCoreOptions) {
if (foundAtCursor !== hoveredAtCursor) {
hoveredAtCursor = foundAtCursor;
// @ts-ignore
onHover && onHover(foundAtCursor.sidx, foundAtCursor.didx, foundAtCursor);
onHover(foundAtCursor!.sidx, foundAtCursor!.didx, foundAtCursor);
}
} else if (hoveredAtCursor) {
setHoverMark(0, null);
hoveredAtCursor = null;
onLeave && onLeave();
onLeave();
}
}

View File

@ -99,13 +99,16 @@ export const preparePlotConfigBuilder: UPlotConfigPrepFn<TimelineOptions> = ({
onHover: (seriesIndex, valueIndex) => {
hoveredSeriesIdx = seriesIndex;
hoveredDataIdx = valueIndex;
shouldChangeHover = true;
},
onLeave: () => {
hoveredSeriesIdx = null;
hoveredDataIdx = null;
shouldChangeHover = true;
},
};
let shouldChangeHover = false;
let hoveredSeriesIdx: number | null = null;
let hoveredDataIdx: number | null = null;
@ -123,15 +126,16 @@ export const preparePlotConfigBuilder: UPlotConfigPrepFn<TimelineOptions> = ({
updateActiveDatapointIdx,
updateTooltipPosition
) => (u: uPlot) => {
if (hoveredSeriesIdx != null) {
// @ts-ignore
updateActiveSeriesIdx(hoveredSeriesIdx);
// @ts-ignore
updateActiveDatapointIdx(hoveredDataIdx);
updateTooltipPosition();
} else {
updateTooltipPosition(true);
if (shouldChangeHover) {
if (hoveredSeriesIdx != null) {
updateActiveSeriesIdx(hoveredSeriesIdx);
updateActiveDatapointIdx(hoveredDataIdx);
}
shouldChangeHover = false;
}
updateTooltipPosition(hoveredSeriesIdx == null);
};
builder.setTooltipInterpolator(interpolateTooltip);