1

Vega Lite中,是否可以将数据值的一个字段用作轴值,将另一个字段用作标签?

如果这是我的 vega lite 规格,那么图表可以正常工作,但在 x 轴上显示日期。如何改为在 x 轴上显示日期名称?

{
  "$schema": "https://vega.github.io/schema/vega-lite/v2.json",
  "description": "basic line graph",
  "data": {
    "values": [
        {"date":"2017-08-15", "dayName":"Tue","item":"foo","count":"0"},
        {"date":"2017-08-16", "dayName":"Wed","item":"foo","count":"11"},
        {"date":"2017-08-17", "dayName":"Thu","item":"foo","count":"7"},
        {"date":"2017-08-18", "dayName":"Fri","item":"foo","count":"28"},
        {"date":"2017-08-19", "dayName":"Sat","item":"foo","count":"0"},
        {"date":"2017-08-20", "dayName":"Sun","item":"foo","count":"0"},
        {"date":"2017-08-21", "dayName":"Mon","item":"foo","count":"0"}
    ]},
  "mark": {
    "type": "line",
    "interpolate": "monotone"
  },
  "encoding": {
    "x": {"field": "date", "type": "temporal"},
    "y": {"field": "count", "type": "quantitative"}
  }
}

在此处输入图像描述

August 16它在August 17x 轴上显示日期字段。我怎样才能让它显示 dayName 字段呢?它应该显示Tue, Wed, 等等。

4

1 回答 1

1

您可以使用timeUnit.

{
  "$schema": "https://vega.github.io/schema/vega-lite/v2.json",
  "description": "basic line graph",
  "data": {
    "values": [
        {"date":"2017-08-15", "dayName":"Tue","item":"foo","count":"0"},
        {"date":"2017-08-16", "dayName":"Wed","item":"foo","count":"11"},
        {"date":"2017-08-17", "dayName":"Thu","item":"foo","count":"7"},
        {"date":"2017-08-18", "dayName":"Fri","item":"foo","count":"28"},
        {"date":"2017-08-19", "dayName":"Sat","item":"foo","count":"0"},
        {"date":"2017-08-20", "dayName":"Sun","item":"foo","count":"0"},
        {"date":"2017-08-21", "dayName":"Mon","item":"foo","count":"0"}
    ]},
  "mark": {
    "type": "line",
    "interpolate": "monotone"
  },
  "encoding": {
    "x": {
      "timeUnit": "day",
      "field": "date", 
      "type": "temporal"
    },
    "y": {"field": "count", "type": "quantitative"}
  }
}

如果要自定义标签格式,可以添加轴格式,也可以

于 2017-08-25T21:11:21.287 回答