具有 2 个不同数据对象的 Vega-lite 堆积条形图
Vega-lite Stacked bar chart with 2 different data objects
我正在尝试在 Vega lite 中模拟 power BI 的行为,如果您根据同一日期添加 2 个数据对象,则可以将数据堆叠在一起。
我在 power BI 中使用 Deneb,但它使用 vega lite,所以任何 vega lite 解决方案都应该没问题。
我有一个今天之前的计数数据集和一个今天之后的计数数据集。我想将它们组合起来,如果今天之前和今天之后有同一个月的数据,那么它们就会堆叠起来而不是相互重叠。
我似乎找不到不涉及将数据分组到一个对象中的解决方案
将两个数据集组合成单个堆积条形图的方法是在制作标准条形图之前使用 lookup transform to join the datasets together. If you want to then stack bars from different columns, you can use a fold transform 将它们组合起来。
例如(open in vega editor):
{
"datasets": {
"table-1": [
{"key": "A", "A": 2},
{"key": "B", "A": 3},
{"key": "C", "A": 1},
{"key": "D", "A": 2}
],
"table-2": [
{"key": "A", "B": 6},
{"key": "B", "B": 4},
{"key": "C", "B": 1},
{"key": "D", "B": 3}
]
},
"data": {"name": "table-1"},
"transform": [
{
"lookup": "key",
"from": {"data": {"name": "table-2"}, "key": "key", "fields": ["B"]}
},
{"fold": ["A", "B"], "as": ["column", "value"]}
],
"mark": "bar",
"encoding": {
"color": {"field": "column", "type": "nominal"},
"x": {"field": "value", "type": "quantitative"},
"y": {"field": "key", "type": "nominal"}
}
}
我正在尝试在 Vega lite 中模拟 power BI 的行为,如果您根据同一日期添加 2 个数据对象,则可以将数据堆叠在一起。
我在 power BI 中使用 Deneb,但它使用 vega lite,所以任何 vega lite 解决方案都应该没问题。
我有一个今天之前的计数数据集和一个今天之后的计数数据集。我想将它们组合起来,如果今天之前和今天之后有同一个月的数据,那么它们就会堆叠起来而不是相互重叠。
我似乎找不到不涉及将数据分组到一个对象中的解决方案
将两个数据集组合成单个堆积条形图的方法是在制作标准条形图之前使用 lookup transform to join the datasets together. If you want to then stack bars from different columns, you can use a fold transform 将它们组合起来。
例如(open in vega editor):
{
"datasets": {
"table-1": [
{"key": "A", "A": 2},
{"key": "B", "A": 3},
{"key": "C", "A": 1},
{"key": "D", "A": 2}
],
"table-2": [
{"key": "A", "B": 6},
{"key": "B", "B": 4},
{"key": "C", "B": 1},
{"key": "D", "B": 3}
]
},
"data": {"name": "table-1"},
"transform": [
{
"lookup": "key",
"from": {"data": {"name": "table-2"}, "key": "key", "fields": ["B"]}
},
{"fold": ["A", "B"], "as": ["column", "value"]}
],
"mark": "bar",
"encoding": {
"color": {"field": "column", "type": "nominal"},
"x": {"field": "value", "type": "quantitative"},
"y": {"field": "key", "type": "nominal"}
}
}