1

扁平化和泛化转换后是否可以使用 timeUnit ?在下面的示例中它不起作用!

如果我从它绘制的 x 轴上删除 timeUnit,但没有 timeUnit 带来的好处。

谢谢

这是一个示例代码,可以在下面的链接中执行 https://vega.github.io/editor/#/edited

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "description": "Sales in a Year.",
  "width": 500,
  "height": 200,
  
  "data": {
    "values": [
      {"timestamp": ["2019-01-01","2019-02-01","2019-03-01","2019-04-01","2019-05-01","2019-06-01",
                     "2019-07-01","2019-08-01","2019-09-01","2019-10-01","2019-11-01","2019-12-01"],
       "cars"  : [55, 43, 91, 81, 53, 19, 87, 52, 52, 44, 52, 52],
       "bikes"     : [12,  6,  2,  0,  0,  0,  0,  0,  0,  3,  9, 15]}
    ]
  },
  
  "transform": [
    {"flatten": ["timestamp", "cars", "bikes"]},
    {"fold": ["cars", "bikes"]}
  ],
  "mark": {"type":"bar", "tooltip": true, "cornerRadiusEnd": 4},

  "encoding": {
    "x": {"field": "timestamp", 
          "timeUnit": "month",
          "type": "ordinal", 
          "title": "", 
          "axis": {"labelAngle": 0}},

    "y": {"field": "value", 
          "type": "quantitative",  
          "title": "Soiling Loss"},

    "color":{"field": "key", 
             "type": "nominal"}
  }
}
4

1 回答 1

0

为方便起见,输入数据中具有简单时间编码的字符串会自动解析为日期,但这种解析不适用于转换结果的数据。

在这种情况下,您可以使用计算转换(在编辑器中查看)手动进行解析:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "description": "Sales in a Year.",
  "width": 500,
  "height": 200,
  "data": {
    "values": [
      {
        "timestamp": [
          "2019-01-01",
          "2019-02-01",
          "2019-03-01",
          "2019-04-01",
          "2019-05-01",
          "2019-06-01",
          "2019-07-01",
          "2019-08-01",
          "2019-09-01",
          "2019-10-01",
          "2019-11-01",
          "2019-12-01"
        ],
        "cars": [55, 43, 91, 81, 53, 19, 87, 52, 52, 44, 52, 52],
        "bikes": [12, 6, 2, 0, 0, 0, 0, 0, 0, 3, 9, 15]
      }
    ]
  },
  "transform": [
    {"flatten": ["timestamp", "cars", "bikes"]},
    {"fold": ["cars", "bikes"]},
    {"calculate": "toDate(datum.timestamp)", "as": "timestamp"}
  ],
  "mark": {"type": "bar", "tooltip": true, "cornerRadiusEnd": 4},
  "encoding": {
    "x": {
      "field": "timestamp",
      "timeUnit": "month",
      "type": "ordinal",
      "title": "",
      "axis": {"labelAngle": 0}
    },
    "y": {"field": "value", "type": "quantitative", "title": "Soiling Loss"},
    "color": {"field": "key", "type": "nominal"}
  }
}

在此处输入图像描述

于 2020-07-02T15:58:39.903 回答