Forum Discussion
Deneb: Line Chart Tooltips that don't require you to hover directly over line
- 3 years ago
Hey Anonymous, you can layer a series of points with an opacity of zero spanning the entire x-axis and encode your tooltips off of those points. You'll want to set nearest to true.
Alternatively if you want a vertical line to show, you could do something similar with a series of rule marks and only have the one that aligns with your cursor be visible.
check out this example: https://vega.github.io/vega-lite/examples/interactive_multi_line_pivot_tooltip.html
if you still need additional help, I can try to generate an example spec later this evening when I have time.
- 3 years ago
Here you go Anonymous
If this is enough to get you going please consider liking this reply and choosing it as the solution.
gist
spec:{ "$schema": "https://vega.github.io/schema/vega-lite/v5.json", "params": [{"name": "dt", "expr": "hover['date']"}], "width": 800, "height": 400, "description": "Google's stock price over time.", "data": {"url": "data/stocks.csv"}, "transform": [{"filter": "datum.symbol==='GOOG'"}], "layer": [ { "encoding": {"x": {"field": "date", "type": "temporal"}}, "layer": [ { "mark": "line", "encoding": {"y": {"field": "price", "type": "quantitative"}} }, { "params": [ { "name": "hover", "select": { "type": "point", "fields": ["date"], "nearest": true, "on": "mouseover", "clear": "mouseout" } } ], "mark": {"type": "rule", "y": 0, "y2": {"expr": "height"}}, "transform": [{"calculate": "format(datum['price'], '$')", "as": "price"}], "encoding": { "opacity": { "condition": { "test": "+hover['date'] === datum['date']", "value": 0.5 }, "value": 0 }, "tooltip": [ {"title": "Symbol", "field": "GOOG"}, {"title": "Price", "field": "price"} ] } } ] } ], "config": {"axis": {"grid": false}} }
Here you go Anonymous
If this is enough to get you going please consider liking this reply and choosing it as the solution.
gist
spec:
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"params": [{"name": "dt", "expr": "hover['date']"}],
"width": 800,
"height": 400,
"description": "Google's stock price over time.",
"data": {"url": "data/stocks.csv"},
"transform": [{"filter": "datum.symbol==='GOOG'"}],
"layer": [
{
"encoding": {"x": {"field": "date", "type": "temporal"}},
"layer": [
{
"mark": "line",
"encoding": {"y": {"field": "price", "type": "quantitative"}}
},
{
"params": [
{
"name": "hover",
"select": {
"type": "point",
"fields": ["date"],
"nearest": true,
"on": "mouseover",
"clear": "mouseout"
}
}
],
"mark": {"type": "rule", "y": 0, "y2": {"expr": "height"}},
"transform": [{"calculate": "format(datum['price'], '$')", "as": "price"}],
"encoding": {
"opacity": {
"condition": {
"test": "+hover['date'] === datum['date']",
"value": 0.5
},
"value": 0
},
"tooltip": [
{"title": "Symbol", "field": "GOOG"},
{"title": "Price", "field": "price"}
]
}
}
]
}
],
"config": {"axis": {"grid": false}}
}