通过参数更改标记顺序

Change mark order via parameter

我正在玩 vega-lite v5 和新参数,想使用输入元素更改标记的顺序:

Open the Chart in the Vega Editor

这可能吗?

我查看了文档中的示例,但无论是 x 的顺序 属性 还是 order 编码,我都无法使用参数。

是的,您可以通过使用 window 转换对数据进行排序来做到这一点。尽管自 VL 5.1.0 起未记录,window 属性可以使用 signal 值进行参数化(并且应该在下一次更新时采用 expr 值)。下面是一个工作示例,它将您的输入元素分为要排序的字段和排序方向。

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {
    "values": [
      {"a": "A", "b": 28},
      {"a": "B", "b": 55},
      {"a": "C", "b": 43},
      {"a": "D", "b": 91},
      {"a": "E", "b": 81},
      {"a": "F", "b": 53},
      {"a": "G", "b": 19},
      {"a": "H", "b": 87},
      {"a": "I", "b": 52}
    ]
  },
  "params": [
    {"name": "sortChoice", "value":"x","bind": {"input": "select", "options": ["x", "y","-x","-y"]}},
    {"name": "sortBy", "expr":"sortChoice==='x'|| sortChoice==='-x' ? 'a':'b'"},
    {"name": "sortOrder", "expr":"sortChoice==='x'|| sortChoice==='y' ? 'ascending':'descending'"}
  ],
  "transform": [
    {
      "sort": [
        {"field": {"signal": "sortBy"}, "order": {"signal": "sortOrder"}}
      ],
      "window": [{"op": "rank", "as": "sorted"}]
    }
  ],
  "mark": "bar",
  "encoding": {
    "x": {"field": "a", "sort": {"field": "sorted"}},
    "y": {"field": "b", "type": "quantitative"}
  }
}