Project a Selection
Edit this pageA selection’s type determines which data values fall within it by default:
- For
single
andmulti
selections, only values that have been directly interacted with (i.e., those that have been clicked on) are considered to be “selected.” - For
interval
selections, values that fall within both the horizontal (x
) and vertical (y
) extents are considered to be “selected.”
These default inclusion criteria can be modified with the project selection transformation, which offers the following two properties:
Property | Type | Description |
---|---|---|
encodings | String[] |
An array of encoding channels. The corresponding data field values must match for a data tuple to fall within the selection. See also: |
fields | String[] |
An array of field names whose values must match for a data tuple to fall within the selection. See also: |
Examples
In the scatterplot example below, use the project transformation to highlight : .
{
"data": {"url": "data/cars.json"},
"selection": {
"pts": {
"type": "single"
}
},
"mark": "point",
"encoding": {
"x": {"field": "Horsepower", "type": "quantitative"},
"y": {"field": "Miles_per_Gallon", "type": "quantitative"},
"color": {
"condition": {
"selection": "pts",
"field": "Cylinders", "type": "ordinal",
"scale": {"scheme": "yelloworangebrown"}
},
"value": "grey"
},
"size": {
"condition": {"selection": "pts", "value": 200},
"value": 50
}
}
}
With interval selections, the project transformation can be used to restrict the region to just the and/or dimensions.
{
"data": {"url": "data/cars.json"},
"selection": {
"pts": {"type": "interval"}
},
"mark": "rect",
"encoding": {
"y": {"field": "Origin", "type": "ordinal"},
"x": {"field": "Cylinders", "type": "ordinal"},
"color": {
"condition": {
"selection": "pts",
"aggregate": "count"
},
"value": "grey"
}
}
}
Current Limitations
- Selections projected over aggregated
fields
/encodings
can only be used within the same view they are defined in. - Interval selections can only be projected using
encodings
. - Interval selections projected over binned or
timeUnit
fields remain continuous selections. Thus, if the visual encoding discretizes them, conditional encodings will no longer work. Instead, use a layered view as shown in the example below. The bar mark discretizes the binnedAcceleration
field. As a result, to highlight selected bars, we use a second layered view rather than a conditional color encoding within the same view.
{
"data": {"url": "data/cars.json"},
"layer": [
{
"selection": {
"brush": {"type": "interval", "encodings": ["x"]}
},
"mark": "bar",
"encoding": {
"x": {"field": "Acceleration", "bin": true},
"y": {"aggregate": "count"}
}
},
{
"transform": [{"filter": {"selection": "brush"}}],
"mark": "bar",
"encoding": {
"x": {"field": "Acceleration", "bin": true},
"y": {"aggregate": "count"},
"color": {"value": "goldenrod"}
}
}
]
}