Vega lite/Altair 以季度指定的解析日期

Vega lite/Altair parsing date specified in quarters

我有一个数据集,数据格式为 2000Q1、2000Q2 等。我想将其用作时间变量。

我怎样才能在 Vega 中实现这个 Lite/Altair?

您可以使用 timeParse in combination with string functions as part of a calculate transform.

timeParse 使用 d3 time formats,不幸的是它本身不支持 quarters,所以我们需要做一些字符串操作。

不是很优雅,但这是 Vega Lite

"transform": [
    {
      "as": "date",
      "calculate": "timeParse(replace(replace(replace(replace(datum.yearquarter, 'Q1', '02'), 'Q2', '05'),  'Q3', '08'), 'Q4', '11'), '%Y%m')"
    }
  ]

在 Altair 中,它看起来像这样:

chart = alt.Chart(url
).transform_calculate(date = "timeParse(replace(replace(replace(replace(datum.yearquarter, 'Q1', '02'), 'Q2', '05'),  'Q3', '08'), 'Q4', '11'), '%Y%m')"
).mark_bar().encode(
y = 'sum(value):Q',
x = 'date:T'
)