New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[vega] support HTML tooltips #17632
Merged
Merged
[vega] support HTML tooltips #17632
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d9eecee
[WIP] support automatic vega tooltips
nyurik 99a50f1
bumped tooltip dep, css fixes
nyurik 68a637c
support for maps
nyurik 46b1380
handle null view
nyurik ea0d9a3
use new tooltip support
nyurik 4725940
use tooltip for content generation
nyurik 83f83ec
pass padding and position as config vals
nyurik 673a6eb
proper alignment, user configurable
nyurik 6002d8c
test fix
nyurik bf936d1
functional test
nyurik c81fff3
fix tests
nyurik 6deca16
Use _.escape
nyurik File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/core_plugins/vega/public/__tests__/vega_tooltip_test.hjson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# This graph creates a single rectangle for the whole graph, | ||
# backed by a datum with two fields - fld1 & fld2 | ||
# On mouse over, with 0 delay, it should show tooltip | ||
{ | ||
v: 1 | ||
config: { | ||
kibana: { | ||
tooltips: { | ||
// always center on the mark, not mouse x,y | ||
centerOnMark: false | ||
position: top | ||
padding: 20 | ||
} | ||
} | ||
} | ||
data: [ | ||
{ | ||
name: table | ||
values: [ | ||
{ | ||
title: This is a long title | ||
fieldA: value of fld1 | ||
fld2: 42 | ||
} | ||
] | ||
} | ||
] | ||
$schema: https://vega.github.io/schema/vega/v3.json | ||
marks: [ | ||
{ | ||
from: {data: "table"} | ||
type: rect | ||
encode: { | ||
enter: { | ||
fill: {value: "#060"} | ||
x: {signal: "0"} | ||
y: {signal: "0"} | ||
width: {signal: "width"} | ||
height: {signal: "height"} | ||
tooltip: {signal: "datum || null"} | ||
} | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { calculatePopoverPosition } from '@elastic/eui'; | ||
import { formatValue as createTooltipContent } from 'vega-tooltip'; | ||
import _ from 'lodash'; | ||
|
||
// Some of this code was adapted from https://github.com/vega/vega-tooltip | ||
|
||
const tooltipId = 'vega-kibana-tooltip'; | ||
|
||
/** | ||
* Simulate the result of the DOM's getBoundingClientRect() | ||
*/ | ||
function createRect(left, top, width, height) { | ||
return { | ||
left, top, width, height, | ||
x: left, y: top, right: left + width, bottom: top + height, | ||
}; | ||
} | ||
|
||
/** | ||
* The tooltip handler class. | ||
*/ | ||
export class TooltipHandler { | ||
constructor(container, view, opts) { | ||
this.container = container; | ||
this.position = opts.position; | ||
this.padding = opts.padding; | ||
this.centerOnMark = opts.centerOnMark; | ||
|
||
view.tooltip(this.handler.bind(this)); | ||
} | ||
|
||
/** | ||
* The handler function. | ||
*/ | ||
handler(view, event, item, value) { | ||
this.hideTooltip(); | ||
|
||
// hide tooltip for null, undefined, or empty string values | ||
if (value == null || value === '') { | ||
return; | ||
} | ||
|
||
const el = document.createElement('div'); | ||
el.setAttribute('id', tooltipId); | ||
el.classList.add('euiToolTipPopover', 'euiToolTip', `euiToolTip--${this.position}`); | ||
|
||
// Sanitized HTML is created by the tooltip library, | ||
// with a largue nmuber of tests, hence supressing eslint here. | ||
// eslint-disable-next-line no-unsanitized/property | ||
el.innerHTML = createTooltipContent(value, _.escape); | ||
|
||
// add to DOM to calculate tooltip size | ||
document.body.appendChild(el); | ||
|
||
// if centerOnMark numeric value is smaller than the size of the mark, use mouse [x,y] | ||
let anchorBounds; | ||
if (item.bounds.width() > this.centerOnMark || item.bounds.height() > this.centerOnMark) { | ||
// I would expect clientX/Y, but that shows incorrectly | ||
anchorBounds = createRect(event.pageX, event.pageY, 0, 0); | ||
} else { | ||
const containerBox = this.container.getBoundingClientRect(); | ||
anchorBounds = createRect( | ||
containerBox.left + view._origin[0] + item.bounds.x1, | ||
containerBox.top + view._origin[1] + item.bounds.y1, | ||
item.bounds.width(), | ||
item.bounds.height() | ||
); | ||
} | ||
|
||
const pos = calculatePopoverPosition(anchorBounds, el.getBoundingClientRect(), this.position, this.padding); | ||
|
||
el.setAttribute('style', `top: ${pos.top}px; left: ${pos.left}px`); | ||
} | ||
|
||
hideTooltip() { | ||
const el = document.getElementById(tooltipId); | ||
if (el) el.remove(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to change, but in future I'd use
typeof .... === "undefined"
, as it's more canonical and doesn't rely on obscure globals.