简体   繁体   English

具有 2 个不同数据对象的 Vega-lite 堆叠条形图

[英]Vega-lite Stacked bar chart with 2 different data objects

Im trying to emulate in Vega lite the behaviour of power BI where if you add 2 data objects based along the same date, you're able to stack the data on top of each other.我试图在 Vega lite 中模拟 Power BI 的行为,如果您在同一日期添加 2 个数据对象,则可以将数据堆叠在一起。

I'm using Deneb in power BI, but it uses vega lite, so any vega lite solution should be fine.我在 Power BI 中使用 Deneb,但它使用 vega lite,所以任何 vega lite 解决方案都应该没问题。

i have a data set of counts before today and a dataset of counts after today.我有今天之前的计数数据集和今天之后的计数数据集。 I want to combine them so that if before today and after today have data in the same month then they are stacked rather than overlaying each other.我想将它们组合起来,以便如果今天之前和今天之后在同一个月有数据,那么它们是堆叠的,而不是相互重叠的。

I can't seem to find a solution that doesn't involve grouping the data into one object我似乎找不到不涉及将数据分组到一个 object 的解决方案

The way to combine two datasets into a single stacked bar chart is to make use of a lookup transform to join the datasets together.将两个数据集组合成单个堆叠条形图的方法是使用查找转换将数据集连接在一起。 If you want to then stack bars from different columns, you can use a fold transform to combine them before doing a standard bar chart.如果您想堆叠来自不同列的条形图,您可以在制作标准条形图之前使用折叠变换将它们组合起来。

For example ( open in vega editor ):例如( 在 vega 编辑器中打开):

{
  "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"}
  }
}

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM