vega-lite:如何在JSON中将日期指定为包含年,月和日数字的单独字段?

时间:2019-02-22 22:19:56

标签: d3.js vega vega-lite

我的JSON数据是一个对象列表,每个对象都包含以下格式的日期:

  "date" : {
    "year" : 2019,
    "month" : 2,
    "day" : 17
  },

我如何告诉Vega-lite这是约会? 为此,我创建了另一个日字段,该字符串是连接这三个字段的字符串,并使用:

    "format" : {
      "parse" : {
        "day" : "date: '%Y %m %d'"
      }

但是我希望能够使用现有的3个字段。...

1 个答案:

答案 0 :(得分:4)

您可以通过计算转换以及datetime表达式来执行此操作。例如(vega-editor link):

{
  "data": {
    "values": [
      {"date": {"year": 2019, "month": 2, "day": 15}, "val": 1},
      {"date": {"year": 2019, "month": 2, "day": 16}, "val": 2},
      {"date": {"year": 2019, "month": 2, "day": 17}, "val": 4},
      {"date": {"year": 2019, "month": 2, "day": 18}, "val": 3},
      {"date": {"year": 2019, "month": 2, "day": 19}, "val": 5},
      {"date": {"year": 2019, "month": 2, "day": 20}, "val": 6}
    ]
  },
  "transform": [
    {
      "calculate": "datetime(datum.date.year, datum.date.month, datum.date.day)",
      "as": "combined"
    }
  ],
  "mark": "area",
  "encoding": {
    "x": {"field": "combined", "type": "temporal"},
    "y": {"field": "val", "type": "quantitative"}
  }
}

enter image description here