Search code examples
vega-lite

Does today() exist in vega-lite?


I'd like to show the difference between a date (Date1) from my data and "today" in my chart. I could add a column in my table that contains today's date, but I was wondering if I can somehow solve this in the transform [] statement to keep my tables lean.

Does something like:

today()

exist in vega-lite?


Solution

  • You can use the now() function within the vega-expression; see https://vega.github.io/vega/docs/expressions/#now

    Here's a simple example of adding a column with today's date (open in editor):

    {
      "data": {
        "values": [
          {"date": "2020-01-01T00:00:00"},
          {"date": "2021-01-01T00:00:00"},
          {"date": "2021-02-01T00:00:00"}
        ]
      },
      "transform": [{"calculate": "now()", "as": "today"}],
      "mark": "point",
      "encoding": {
        "x": {"field": "today", "timeUnit": "yearmonthdate", "type": "ordinal"},
        "y": {"field": "date", "timeUnit": "yearmonthdate", "type": "ordinal"}
      }
    }
    

    enter image description here