使用 Vega-Lite 计算同一字段的负值和正值

Counting negative and positive values of the same field with Vega-Lite

我有一个包含 2 个日期的 table,我想创建一个包含 'delayed' 和 'on-time' 切片的 2 切片饼图('delayed' 如果 Date2>Date1 , 'on-time' 如果 Date2<=Date1).

您会向 Vega-Lite 初学者推荐什么策略?

我的想法:

  1. 计算新字段中的差异。然后我不清楚我应该如何在编码块中表达我的意图。

{"calculate": "datum.Date1 - datum.Date2", "as":"DateDifference"}

  1. bin 数据 -> 如何?

非常感谢任何想法!

您可以通过在计算转换中使用三元运算符来执行此操作:

{
  "calculate": "toDate(datum.Date1) < toDate(datum.Date2) ? 'delayed' : 'on time'",
  "as": "diff"
}

这是一个饼图示例 (view in editor):

{
  "data": {
    "values": [
      {"Date1": "2021-02-01T06:00:00", "Date2": "2021-02-01T05:50:00"},
      {"Date1": "2021-02-01T07:00:00", "Date2": "2021-02-01T06:49:00"},
      {"Date1": "2021-02-01T08:00:00", "Date2": "2021-02-01T08:15:00"},
      {"Date1": "2021-02-01T09:00:00", "Date2": "2021-02-01T09:05:00"},
      {"Date1": "2021-02-01T19:00:00", "Date2": "2021-02-01T09:59:00"}
    ]
  },
  "transform": [
    {
      "calculate": "toDate(datum.Date1) < toDate(datum.Date2) ? 'delayed' : 'on time'",
      "as": "diff"
    }
  ],
  "mark": "arc",
  "encoding": {
    "theta": {"field": "diff", "aggregate": "count", "type": "quantitative"},
    "color": {"field": "diff", "type": "nominal"}
  },
  "$schema": "https://vega.github.io/schema/vega-lite/v4.8.1.json"
}