Multi Series Line Chart with Interactive Highlgiht
The plot below uses argmax to position text labels at the end of line. It also applies single selection to highlight a hovered line. Note that we can hidden thick lines to make it easier to hover.
View this example in the online editor
Vega-Lite JSON Specification
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"description": "Multi-series line chart with labels and interactive highlight on hover. We also set the selection's initial value to provide a better screenshot",
"data": {"url": "data/stocks.csv"},
"transform": [{"filter": "datum.symbol!=='IBM'"}],
"encoding": {
"x": {"field": "date", "type": "temporal", "title": "date"},
"y": {"field": "price", "type": "quantitative", "title": "price"},
"color": {
"condition": {
"selection": "hover",
"field":"symbol",
"type":"nominal",
"legend": null
},
"value": "grey"
},
"opacity": {
"condition": {
"selection": "hover",
"value": 1
},
"value": 0.2
}
},
"layer": [{
"description": "transparent layer to make it easier to trigger selection",
"selection": {
"hover": {
"type": "single",
"on": "mouseover",
"empty": "all",
"fields": ["symbol"],
"init": {"symbol": "AAPL"}
}
},
"mark": {"type": "line", "strokeWidth": 8, "stroke": "transparent"}
}, {
"mark": "line"
}, {
"encoding": {
"x": {"aggregate": "max", "field": "date"},
"y": {"aggregate": {"argmax": "date"}, "field": "price"}
},
"layer": [{
"mark": {"type": "circle"}
}, {
"mark": {"type": "text", "align": "left", "dx": 4},
"encoding": {"text": {"field":"symbol", "type": "nominal"}}
}]
}],
"config": {"view": {"stroke": null}}
}