简体   繁体   中英

Derivative transform on Vega-Lite

I have a dataset that I need to plot the derivative function of it. Is there a way to perform the derivative of a series in Vega-Lite? Maybe a transform function or maybe with the calculate function? Is there a way to do it manually, X(t) - X(t-1) ?

You can construct the operation you have in mind using a window transform to determine the adjacent value, followed by a calculate transform to compute the difference between the values.

Here is an example ( vega editor link ):

{
  "data": {
    "values": [
      {"x": 0, "y": 0},
      {"x": 1, "y": 0.8},
      {"x": 2, "y": 0.9},
      {"x": 3, "y": 0.1},
      {"x": 4, "y": -0.8},
      {"x": 5, "y": -1},
      {"x": 6, "y": -0.3},
      {"x": 7, "y": 0.7},
      {"x": 8, "y": 1},
      {"x": 9, "y": 0.4},
      {"x": 10, "y": -0.5}
    ]
  },
  "transform": [
    {
      "window": [{"op": "last_value", "field": "y", "as": "y1"}],
      "frame": [0, 1],
      "sort": [{"field": "x", "order": "ascending"}]
    },
    {"calculate": "datum.y1 - datum.y", "as": "dy"}
  ],
  "layer": [
    {
      "mark": "line",
      "encoding": {
        "x": {"type": "quantitative", "field": "x"},
        "y": {"type": "quantitative", "field": "y"}
      }
    },
    {
      "mark": "line",
      "encoding": {
        "color": {"value": "red"},
        "x": {"type": "quantitative", "field": "x"},
        "y": {"type": "quantitative", "field": "dy"}
      }
    }
  ],
  "config": {"view": {"width": 400, "height": 300}}
}

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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