{
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "width": 1200,
  "height": 200,
  "padding": 5,

  "signals": [
    {
      "name": "length",
      "value":  ["year","month", "date"],
      "bind": {
        "input": "select",
        "options": [
          ["year"],
          ["year","month"],
          ["year","week"],
          ["year","month", "date"]
        ],
        "labels": [
          "year",
          "month",
          "week",
          "day"
        ]
      }
    },
    {
      "name": "operation",
      "value": "average",
      "bind": {
        "input": "select",
        "options": [
          "average",
          "count",
          "sum",
          "median",
          "min",
          "max"
        ]
      }
    }
  ],

  "data": [
    {
      "name": "table",
      "values" : null,
      "transform": [
        {
          "type": "formula",
          "expr": "toDate(datum[\"timestamp\"])",
          "as": "dateTime"
        },
        {
          "type": "timeunit",
          "field": "dateTime",
          "units": {"signal": "length"}
        },
        {
          "type": "aggregate",
          "fields": ["max"],
          "ops": [{"signal" :  "operation"}],
          "groupby": ["unit0"],
          "as": ["avg"]
        },
        {
          "type": "timeunit",
          "field": "unit0",
          "units": {"signal": "length"}
        }
      ]
    },
    {
      "name": "table2",
      "source": "table",
      "transform":[
        {
          "type": "lookup",
          "from": "table",
          "key": "unit0",
          "fields": ["unit1"],
          "as": ["obj"]
        }
      ]
    }
  ],

  "scales": [
    {
      "name": "xscale",
      "type": "time",
      "domain": {"data": "table", "fields": ["unit0", "unit1"]},
      "range": "width"
    },
    {
      "name": "yscale",
      "type": "linear",
      "domain": {"data": "table", "field": "avg"},
      "nice": true,
      "range": "height"
    }
  ],

  "axes": [
    { "orient": "bottom", "scale": "xscale", "tickCount": 6  },
    { "orient": "left", "scale": "yscale" }
  ],

  "marks": [
    {
      "type": "symbol",
      "from": {"data":"table"},
      "encode": {
        "update": {
          "shape": {"value": "circle"},
          "size": {"value": 20},
          "x": {"scale": "xscale", "field": "unit0"},
          "y": {"scale": "yscale", "field": "avg"},
          "color": {"value": "both"},
          "strokeWidth": {"value": 1}
        }
      }
    },
    {
      "type": "rule",
      "from": {"data":"table2"},
      "encode": {
        "update": {
          "x": {"scale": "xscale", "field": "unit0"},
          "x2":[
            {
              "test": "isValid(datum.obj)",
              "scale" : "xscale",
              "field" : "obj.unit0"
            },
            {
              "scale" : "xscale",
              "field" : "unit0"
            }
          ],
          "y": {"scale": "yscale", "field": "avg"},
          "y2":[
            {
              "test": "isValid(datum.obj)",
              "scale" : "yscale",
              "field" : "obj.avg"
            },
            {
              "scale" : "yscale",
              "field" : "avg"
            }
          ]



        },
        "update": {
          "stroke": {"value": "steelblue"},
          "strokeWidth": {"value": 1}
        },
        "hover": {
          "stroke": {"value": "red"},
          "strokeWidth": {"value": 3}
        }
      }
    }
  ]



}
