英文:
In Vega-Lite, 'config.legend.columns' doesn't appear to work when using 'resolve'. Is there another way of specifying the number of entries per line?
问题
这里是一个示例。
正如您所看到的,我已将 'config.legend.columns' 设置为 3。因此,我期望在渲染的图表中,图例每行只有 3 个条目。
在使用 'resolve' 之前,设置 'config.legend.columns' 对我来说是有效的。换句话说,图例会在显示指定列数时换行。
但在开始使用 'resolve' 后,实际上解决了一系列问题,设置顶级 'config.legend.columns' 似乎没有任何效果。
我尝试将顶级 'columns' 更改为不同的值,但没有变化。
我还尝试在每个层次的 'encoding.color.legend' 对象中添加一个 'columns' 条目,但似乎也没有变化。
英文:
Here is an example.
As you can see I have set 'config.legend.columns' to be 3. So I am expecting the legend in the rendered graph to only have 3 entries per row.
Before I was using 'resolve', setting 'config.legend.columns' was working for me. IOW the legend would line break when it displayed the specified number of columns.
After I started using 'resolve', which actually addressed a litany of issues we were fighting, setting the top-level 'config.legend.columns' appears to have no effect whatsoever.
I changed the toplevel 'columns' to different values, no change.
I tried adding a 'columns' entry to each of the layers 'encoding.color.legend' objects, also no apparent change.
答案1
得分: 1
在底层,由于您的设置方式,Vega实际上为您提供了4个单独的图例。您还可以将此视为您已经四次设置图例标题为""。
例如:
您需要重新考虑如何提供数据给Vega,可以通过重新整理数据和/或直接使用Vega来获得所需的灵活性。
以下内容可能有助于您理解正在发生的情况,并可能成为您的解决方案(一次指定每个图例类型的完整域和范围)
英文:
Under the covers, Vega is actually giving you 4 separate legends due to the way you have set this up. You can also see this as you have set the legend title to "" four times.
e.g.
You need to rethink how you provide your data to Vega either reshaping it and/or using Vega directly to give you the flexibility you require.
The following might help you understand what is happening and might be a solution for you (to specify the full domain and range once per legend type)
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.0.json",
"title": "",
"width": "container",
"config": {
"customFormatTypes": true,
"axis": {
"labelFont": "Roboto",
"labelFontSize": 12,
"labelFontWeight": 400,
"labelColor": "0x5f6368"
},
"legend": {
"symbolOpacity": 1,
"orient": "bottom",
"direction": "horizontal",
"columns": 3,
"symbolSize": 400
}
},
"datasets": {
"dataset_id_1": [
{"value": 12, "date": "2022-01-10T14:00:00.000Z"},
{"value": 22, "date": "2022-02-10T14:00:00.000Z"},
{"value": 66, "date": "2022-03-10T14:00:00.000Z"},
{"value": 33, "date": "2022-04-10T14:00:00.000Z"}
],
"dataset_id_2": [
{"value": 22, "date": "2022-01-10T14:00:00.000Z"},
{"value": 12, "date": "2022-02-10T14:00:00.000Z"},
{"value": 96, "date": "2022-03-10T14:00:00.000Z"},
{"value": 33, "date": "2022-04-10T14:00:00.000Z"}
]
},
"layer": [
{
"data": {"name": "dataset_id_1"},
"transform": [{"calculate": "'Holy Handgrenades'", "as": "c"}],
"mark": {"type": "line", "stroke": "red", "strokeWidth": 2},
"encoding": {
"y": {
"field": "value",
"type": "quantitative",
"axis": {"title": "Incidents"}
},
"x": {
"timeUnit": "yearmonthdate",
"field": "date",
"title": "",
"axis": {"labelFlush": true, "tickCount": 5}
}
}
},
{
"data": {"name": "dataset_id_2"},
"transform": [{"calculate": "'Vorpal Rabbits'", "as": "c"}],
"mark": {"type": "line", "stroke": "blue", "strokeWidth": 2},
"encoding": {
"y": {
"field": "value",
"type": "quantitative",
"axis": {"title": "Incidents"}
},
"x": {
"timeUnit": "yearmonthdate",
"field": "date",
"title": "",
"axis": {"labelFlush": true, "tickCount": 5}
},
"color": {
"field": "c",
"title": "a",
"scale": {"domain": ["Holy Handgrenades","Vorpal Rabbits"], "range": ["red","blue"]}, "legend":{"columns":1}
}
}
},
{
"data": {"values": [{"max": 100, "min": 20, "group": "top"}]},
"mark": {"type": "rect", "color": "#1e8e3e", "opacity": 0.08},
"encoding": {
"y": {"aggregate": "max", "field": "max"},
"y2": {"aggregate": "min", "field": "min"},
"color": {
"field": "group",
"title": "b",
"scale": {"domain": ["top", "bottom"], "range": ["#1e8e3e","#d93025"]},
"legend": {"symbolType": "square", "symbolOpacity": 0.08, "columns":1}
}
}
},
{
"data": {"values": [{"max": 20, "min": 0, "group": "bottom"}]},
"mark": {"type": "rect", "color": "#d93025", "opacity": 0.08},
"encoding": {
"y": {"aggregate": "max", "field": "max"},
"y2": {"aggregate": "min", "field": "min"},
"color": {
"field": "group",
"title": "d",
"scale": {"domain": ["bottom"], "range": ["#d93025"]},
"legend": null
}
}
}
],
"resolve": {"scale": {"stroke": "independent", "color": "independent"}}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论