Q:

Calculations in vega-lite

is there a way to dynamically calculate growth rates in Vega-Lite.
For example:
    [
      {"date": "1/1/2020", "b": 27},
      {"date": "1/2/2020", "b": 30},
      {"date": "1/3/2020", "b": 33}
    ]

How could I create data (and a chart) that shows the daily +3 (or the ~+10%)?

Edit: Thanks for the answer, @jakevdp.
Should have outlined the added complexity earlier; apologies: I need to aggregate prior to calculating changes. See below for the data and my attempt (dates seem offset and last date's drop doesn't make sense.
[Vega Editor][1]
{
  "data": {
    "values": [
      {"date": "2020-01-01", "country": "CHN", "count": 0},
      {"date": "2020-01-02", "country": "CHN", "count": 2},
      {"date": "2020-01-03", "country": "CHN", "count": 4},

      {"date": "2020-01-01", "country": "GER", "count": 0},
      {"date": "2020-01-02", "country": "GER", "count": 2},
      {"date": "2020-01-03", "country": "GER", "count": 4},
      {"date": "2020-01-04", "country": "GER", "count": 6}
    ]
  },
"transform": [
    {
      "aggregate": [{"op": "sum", "field":"count", "as":"daily_count"}],
      "groupby": ["date"]
    },
    {
      "window": [
        {"op": "lead", "field": "daily_count", "as": "daily_count_tomorrow"}
      ]
    },
    {"filter": "isValid(datum.daily_count_tomorrow)"},
    {"calculate": "datum.daily_count_tomorrow - datum.daily_count", "as": "change"}
  ],

  "mark": "bar",
  "encoding": {
    "x": {"type": "ordinal", "field": "date", "timeUnit": "yearmonthdate"},
    "y": {"type": "quantitative", "field": "change"}
  }
}

  [1]: https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAmCGAutIC4yghSA3WAbArgKYDOKYA2uBhMDAoWZAEwAMrAtCwIydeQA0UAMYB7fADt4AJwCejAMIAJAHIDhYyWRYBfflWq048BqmZsWvTkzWRRE6XNNLVg2xvhkmu-RkP1GrBzcnADMNnaSsgoq4e5kACzePjR0xgHmltyx9lGmAOIAogBK2ZqoOnrUKUYmUIEWwWylDoyFJa4RHqhelVV+aab1mWEd7rlQbc0J3lVoqbVmQTws8c3jkJOj9mQAbNo+ALpU3pDSsOLEAGYiUgC2ZJQGyVCwAOavUoSv-qjktCIAB0YxHw91clwAloRcNAUG5tq5YKRkHQIbgZAB9TqQbQHXrUSAfMQAgBGjgo80gR2oM18z0gAHcIeJoCIGQ9nilAYxcIRYLDwVCYYw4GjMdjEcioKL0Vj3Bj4CJbjcpGycc9qRhaSlIbhjFJGBDiAA1PAQ6AACiMoIAdDLxfLFcqpKqGQBKHH4uZCPBCfC4H7ShC2+1y+wKpUqtlgdhga23O2wMVhzSSxhCAAW51eDH2EDxICokFusCkAGtGCTSwIi4RxKJoMzXmR0BhIAAPFunGQAhY3RviPA2SHQ2GmGo2eAQ26EACq4ghXSgMj5dxEkgzE+1y678B7CwAjvhzlPEFOsAxBaP01nxDn1RB9togA

A:

Yes, you can do this using the window transform with the lead or lag operation. For example (vega editor):
{
  "data": {
    "values": [
      {"date": "2020-01-01", "b": 29},
      {"date": "2020-01-02", "b": 30},
      {"date": "2020-01-03", "b": 32},
      {"date": "2020-01-04", "b": 31},
      {"date": "2020-01-05", "b": 34}
    ]
  },
  "transform": [
    {"window": [{"op": "lead", "field": "b", "as": "b1"}]},
    {"filter": "isValid(datum.b1)"},
    {"calculate": "datum.b1 - datum.b", "as": "change"}
  ],
  "mark": "bar",
  "encoding": {
    "x": {"type": "ordinal", "field": "date", "timeUnit": "yearmonthdate"},
    "y": {"type": "quantitative", "field": "change"},
    "color": {
      "condition": {"test": "datum.change > 0", "value": "green"},
      "value": "red"
    }
  }
}

