I have a table with 2 dates and I would like to create a 2 sliced pie chart with 'delayed' and 'on-time' slices ('delayed' if Date2>Date1, 'on-time' if Date2<=Date1).
What strategy would you recommend to a Vega-Lite beginner?
My ideas:
{"calculate": "datum.Date1 - datum.Date2", "as":"DateDifference"}
Any thoughts are highly appreciated!
You can do this by using a ternary operator in the calculate transform:
{
"calculate": "toDate(datum.Date1) < toDate(datum.Date2) ? 'delayed' : 'on time'",
"as": "diff"
}
Here's an example in a pie chart (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"
}