我正在尝试在 vega-lite 中制作饼图,并能够选择要包含的类别。
这是我的第一次尝试(vega编辑器)
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"description": "A simple pie chart with embedded data and multi-selection per category.",
"data": {
"values": [
{"category": 1, "value": 4},
{"category": 2, "value": 6},
{"category": 3, "value": 10},
{"category": 4, "value": 3},
{"category": 5, "value": 7},
{"category": 6, "value": 8}
]
},
"selection": {
"category_select": {
"type": "multi",
"fields": ["category"],
"bind": "legend"
}
},
"transform": [{"filter": {"selection": "category_select"}}],
"mark": {"type": "arc"},
"encoding": {
"theta": {"aggregate": "sum", "field": "value", "type": "quantitative"},
"color": {"field": "category", "type": "nominal"}
}
}
但是,当我选择一个
category
时,无法选择多个:
如何支持多类别选择,比如
2 and 4
?
按住 Shift 键时可以进行多项选择。
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"description": "A simple pie chart with embedded data and multi-selection per category.",
"data": {
"values": [
{"category": 1, "value": 4},
{"category": 2, "value": 6},
{"category": 3, "value": 10},
{"category": 4, "value": 3},
{"category": 5, "value": 7},
{"category": 6, "value": 8}
]
},
"params": [{
"name": "test",
"select": {"type": "point", "fields": ["category"]},
"bind": "legend"
}],
"mark": {"type": "arc"},
"encoding": {
"theta": {"aggregate": "sum", "field": "value", "type": "quantitative"},
"color": {"field": "category", "type": "nominal"},
"opacity": {
"condition": {"param": "test", "value": 1},
"value": 0.2
}
}
}