Vega 转换为 select 前 n 行

Vega transform to select the first n rows

是否有 Vega/Vega-Lite 转换可用于 select 数据集中的前 n 行?

假设我从 URL 中得到一个数据集,例如:

Person Height
Jeremy 6.2
Alice 6.0
Walter 5.8
Amy 5.6
Joe 5.5

我想创建一个条形图,仅显示最高的三个人的身高。假设我们确定 URL 中的数据集已经排序。假设我们无法更改 URL.

返回的数据

我想做这样的事情:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {
    "url": "heights.csv"
  },
  "transform": [
      {"head": 3}
  ],
  "mark": "bar",
  "encoding": {
    "x": {"field": "Person", "type": "nominal"},
    "y": {"field": "Height", "type": "quantitative"}
  }
}

只有 head 转换实际上并不存在 - 我可以做些什么来获得相同的效果吗?

Vega-Lite 文档在 filtering top-k items.

中有一个类似的示例

你的情况有点特殊:你不想根据排名排序,而是根据数据的原始排序。您可以使用基于计数的 window 转换后跟适当的 filter 来执行此操作。例如 (view in editor):

{
  "data": {
    "values": [
      {"Person": "Jeremy", "Height": 6.2},
      {"Person": "Alice", "Height": 6.0},
      {"Person": "Walter", "Height": 5.8},
      {"Person": "Amy", "Height": 5.6},
      {"Person": "Joe", "Height": 5.5}
    ]
  },
  "transform": [
    {"window": [{"op": "count", "as": "count"}]},
    {"filter": "datum.count <= 3"}
  ],
  "mark": "bar",
  "encoding": {
    "x": {"field": "Height", "type": "quantitative"},
    "y": {"field": "Person", "type": "nominal", "sort": null}
  }
}