简体   繁体   English

如何从 Vega 中的偏移量创建新日期?

[英]How to create new date from offset in Vega?

I want to create a new date field using a field containing seconds and a field using time.我想使用包含秒的字段和使用时间的字段创建一个新的日期字段。

I've tried adding an additional field {calculate: "toDate(datum._source['TimeStamp']-datum._source['Metric'])", as: "time2"}我尝试添加一个附加字段{calculate: "toDate(datum._source['TimeStamp']-datum._source['Metric'])", as: "time2"}

I'd like to plot Y against time as blue points and Y against time2 as red points.我想将 Y 与时间绘制成蓝点,将 Y 与时间 2 绘制成红点。 When I include the time2 expression and plot against it the graph comes back empty without error.当我包含 time2 表达式并针对它绘制图形时,图形返回为空且没有错误。 I know I need to do something involving converting either the Date or Metric that can be subtracted.我知道我需要做一些涉及转换可以减去的日期或指标的事情。

Ex: "TimeStamp": July 1st 2019, 09:16:44.000 "Metric": 0.3 <-seconds例如:“时间戳”:2019 年 7 月 1 日,09:16:44.000 “公制”:0.3 <-秒

  $schema: https://vega.github.io/schema/vega-lite/v2.json
  data: {
    url: {
      %context%: true
      %timefield%: TimeStamp
      index: a.index*
      body: {
        size: 10000
        _source: ["@timestamp", "TimeStamp", "Metric", "TxnType","Y"]
      }
    }
    format: {property: "hits.hits"}
  }
  transform: [
    {calculate: "toDate(datum._source['TimeStamp'])", as: "time"},
    {"filter": "datum._source['Y'] > 0"},
    {"filter": "datum._source['TxnType'] == 'Type'"}

  ]

  mark: circle
  encoding: {
    x: {field: "time", type: "temporal",
    }
    y: {field: "_source.Y", type: "quantitative", "scale": {"type": "log"}}
  }
}```

Starting with Vega 5.8 you can use the timeOffset() function in expressions.从 Vega 5.8 开始,您可以在表达式中使用timeOffset()函数。

From https://vega.github.io/vega/docs/expressions/#timeOffset来自https://vega.github.io/vega/docs/expressions/#timeOffset

timeOffset(unit, date[, step]) ≥ 5.8
Returns a new Date instance that offsets the given date by the specified time unit in the local timezone.返回一个新的 Date 实例,该实例将给定日期偏移本地时区中指定的时间单位。 The optional step argument indicates the number of time unit steps to offset by (default 1).可选的 step 参数指示要偏移的时间单位步数(默认为 1)。

Assuming your field TimeStamp is a timepoint from which the offset Metric (in seconds) should be subtracted, then you would use假设您的字段TimeStamp是一个时间点,应该从中减去偏移Metric (以秒为单位),那么您将使用

"transform": [
  {
    "calculate": "timeOffset('seconds', datum.TimeStamp, -datum.Metric)",
    "as": "time"
  }
]

If that does not work, make sure that datum.TimeStamp is a date object.如果这不起作用,请确保datum.TimeStamp是一个日期对象。 Eg by specifying例如通过指定

"data": {
  ...,
  "format": {"parse": {"TimeStamp": "date"}}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM