英文:
How to add different mark types in the legend
问题
I'd like to provide a name for a mark rule via some mark's property (which one), and then add it to the legend? Is it possible?
我想通过某个标记的属性(哪一个)为标记规则提供一个名称,然后将其添加到图例中。是否可能?
英文:
I'd like to provide a name for a mark rule via some mark's property (which one), and then add it to the legend? Is it possible?
I need to provide a few such rules on a chart, so it would be useful to see them all on the legend.
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"values": [
{"X": 1, "Y": 2, "Series": "A"},
{"X": 1, "Y": 3, "Series": "B"},
{"X": 2, "Y": 3, "Series": "A"},
{"X": 2, "Y": 4, "Series": "B"},
{"X": 3, "Y": 4, "Series": "A"},
{"X": 3, "Y": 5, "Series": "B"},
{"X": 4, "Y": 3, "Series": "A"},
{"X": 4, "Y": 4, "Series": "B"},
{"X": 5, "Y": 2, "Series": "A"},
{"X": 5, "Y": 3, "Series": "B"}
]
},
"encoding": {
"x": {"field": "X", "type": "nominal"},
"y": {"field": "Y", "aggregate": "sum", "type": "quantitative"},
"color": {"field": "Series"}
},
"layer":[
{
"mark":"bar"
},
{
"mark": {
"type": "rule",
"stroke": "red",
"strokeWidth": 1,
"strokeDash": [8,4]
},
"encoding":{
"x": {},
"y": {"datum": 6}
}
}
]
}
答案1
得分: 0
以下是翻译好的内容,代码部分未翻译:
"Here you go. You might be better off using Vega instead of VL once your viz gets more complicated.
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"encoding": {
"x": {"field": "X", "type": "nominal"},
"y": {"field": "Y", "aggregate": "sum", "type": "quantitative"}
},
"layer": [
{
"data": {
"name": "a",
"values": [
{"X": 1, "Y": 2, "Series": "A"},
{"X": 1, "Y": 3, "Series": "B"},
{"X": 2, "Y": 3, "Series": "A"},
{"X": 2, "Y": 4, "Series": "B"},
{"X": 3, "Y": 4, "Series": "A"},
{"X": 3, "Y": 5, "Series": "B"},
{"X": 4, "Y": 3, "Series": "A"},
{"X": 4, "Y": 4, "Series": "B"},
{"X": 5, "Y": 2, "Series": "A"},
{"X": 5, "Y": 3, "Series": "B"}
]
},
"mark": "bar",
"encoding": {
"fill": {"field": "Series", "legend": {"symbolType": "square"}}
}
},
{
"data": {"name": "b", "values": [{"Y": 6, "Series": "Goal", "C": "red"}]},
"mark": {"type": "rule", "strokeWidth": 1, "strokeDash": [8, 4]},
"encoding": {
"x": {},
"y": {"field": "Y"},
"color": {
"field": "Series",
"legend": {"symbolType": "stroke"},
"scale": {"range": {"field": "C"}}
}
}
}
]
}"
英文:
Here you go. You might be better off using Vega instead of VL once your viz gets more complicated.
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"encoding": {
"x": {"field": "X", "type": "nominal"},
"y": {"field": "Y", "aggregate": "sum", "type": "quantitative"}
},
"layer": [
{
"data": {
"name": "a",
"values": [
{"X": 1, "Y": 2, "Series": "A"},
{"X": 1, "Y": 3, "Series": "B"},
{"X": 2, "Y": 3, "Series": "A"},
{"X": 2, "Y": 4, "Series": "B"},
{"X": 3, "Y": 4, "Series": "A"},
{"X": 3, "Y": 5, "Series": "B"},
{"X": 4, "Y": 3, "Series": "A"},
{"X": 4, "Y": 4, "Series": "B"},
{"X": 5, "Y": 2, "Series": "A"},
{"X": 5, "Y": 3, "Series": "B"}
]
},
"mark": "bar",
"encoding": {
"fill": {"field": "Series", "legend": {"symbolType": "square"}}
}
},
{
"data": {"name": "b", "values": [{"Y": 6, "Series": "Goal", "C": "red"}]},
"mark": {"type": "rule", "strokeWidth": 1, "strokeDash": [8, 4]},
"encoding": {
"x": {},
"y": {"field": "Y"},
"color": {
"field": "Series",
"legend": {"symbolType": "stroke"},
"scale": {"range": {"field": "C"}}
}
}
}
]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论