Skip to content
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

docs: add example of rendering images in tooltips #8048

Merged
merged 8 commits into from Apr 10, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added examples/compiled/text_tooltip_image.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/compiled/text_tooltip_image.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 60 additions & 0 deletions examples/compiled/text_tooltip_image.vg.json
@@ -0,0 +1,60 @@
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "A simple chart with an image tooltip.",
"background": "white",
"padding": 5,
"width": 20,
"style": "cell",
"data": [
{
"name": "source_0",
"values": [
{"image": "data/ffox.png"},
{"image": "data/gimp.png"},
{"image": "data/7zip.png"}
]
}
],
"signals": [
{"name": "y_step", "value": 20},
{
"name": "height",
"update": "bandspace(domain('y').length, 1, 0.5) * y_step"
}
],
"marks": [
{
"name": "marks",
"type": "text",
"style": ["text"],
"from": {"data": "source_0"},
"encode": {
"update": {
"fill": {"value": "black"},
"tooltip": {
"signal": "{\"image\": isValid(datum[\"image\"]) ? datum[\"image\"] : \"\"+datum[\"image\"]}"
},
"description": {
"signal": "\"image: \" + (isValid(datum[\"image\"]) ? datum[\"image\"] : \"\"+datum[\"image\"])"
},
"x": {"signal": "width", "mult": 0.5},
"y": {"scale": "y", "field": "image"},
"text": {
"signal": "isValid(datum[\"image\"]) ? datum[\"image\"] : \"\"+datum[\"image\"]"
},
"align": {"value": "center"},
"baseline": {"value": "middle"}
}
}
}
],
"scales": [
{
"name": "y",
"type": "point",
"domain": {"data": "source_0", "field": "image", "sort": true},
"range": {"step": {"signal": "y_step"}},
"padding": 0.5
}
]
}
17 changes: 17 additions & 0 deletions examples/specs/text_tooltip_image.vl.json
@@ -0,0 +1,17 @@
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"description": "A simple chart with an image tooltip.",
"data": {
"values": [
{"image": "data/ffox.png"},
{"image": "data/gimp.png"},
{"image": "data/7zip.png"}
]
},
"mark": "text",
"encoding": {
"text": {"field": "image"},
"y": {"field": "image", "axis": null},
"tooltip": [{"field": "image"}]
}
}
1 change: 1 addition & 0 deletions site/_includes/docs_toc.md
Expand Up @@ -374,5 +374,6 @@
- [Tooltip Based on Encoding]({{site.baseurl}}/docs/tooltip.html#encoding)
- [Tooltip Based on Underlying Data Point]({{site.baseurl}}/docs/tooltip.html#data)
- [Tooltip channel]({{site.baseurl}}/docs/tooltip.html#channel)
- [Tooltip image]({{site.baseurl}}/docs/tooltip.html#tooltip-image)
- [Disable tooltips]({{site.baseurl}}/docs/tooltip.html#disable-tooltips)
- [Vega Tooltip plugin]({{site.baseurl}}/docs/tooltip.html#plugin)
8 changes: 8 additions & 0 deletions site/docs/tooltip.md
Expand Up @@ -64,6 +64,14 @@ To avoid that the tooltips groups the data, add an aggregate to the tooltip enco

<div class="vl-example" data-name="bar_tooltip_aggregate"></div>

## Tooltip image

To display an image in a tooltip you need to be using the [Vega Tooltip plugin](#plugin). Vega Tooltip requires the special field name `image` to be used to indicate that the field values should be rendered as images instead of displayed as text in the tooltip. The image tooltip can be specified either by setting the `tooltip` property of the mark definition (as detailed above) or by passing the field _as an array_ to the tooltip encoding channel, as in the example below:
joelostblom marked this conversation as resolved.
Show resolved Hide resolved

<div class="vl-example" data-name="text_tooltip_image"></div>

In addition to providing the path to an image, the Vega Tooltip plugin can also render base64 encoded images prefixed with `data:image/png;base64,` [similarly to how these are rendered inside HTML image tags](https://www.w3docs.com/snippets/html/how-to-display-base64-images-in-html.html). To change the maximum size of the rendered tooltip images, you can adjust the `max-width` and `max-height` properties of the CSS selector `#vg-tooltip-element img`.

## Disable tooltips

To disable tooltips for a particular single view specification, you can set the `"tooltip"` property of a mark definition block to `null`.
Expand Down