{
  "data": [
    {},
    {},
    {},
    {},
    {},
    {
      "name": "ranks",
      "source": "table",
      "transform": [
        {
          "type": "window",
          "sort": {
            "field": "dateTime",
            "order": "ascending"
          },
          "ops": [
            "rank"
          ],
          "fields": [
            "value"
          ],
          "as": [
            "rank"
          ]
        },
        {
          "type": "formula",
          "expr": "datum.rank + 1",
          "as": "nextRank"
        },
        {
          "type": "formula",
          "expr": "datum.rank - 1",
          "as": "prevRank"
        }
      ]
    },
    {
      "name": "lookup",
      "source": "ranks",
      "transform": [
        {
          "type": "lookup",
          "from": "ranks",
          "key": "rank",
          "fields": [
            "nextRank"
          ],
          "as": [
            "nextObj"
          ]
        },
        {
          "type": "lookup",
          "from": "ranks",
          "key": "rank",
          "fields": [
            "prevRank"
          ],
          "as": [
            "prevObj"
          ]
        },
        {
          "type": "formula",
          "expr": "(datum.nextObj == null || datum.nextObj.dateTime - datum.dateTime > maxTimeDifference) ? null : datum.nextObj",
          "as": "obj"
        },
        {
          "type": "formula",
          "expr": "datum.prevObj == null ?  data('range')[0].dateMin : (time(datum.prevObj.dateTime) + time(datum.dateTime)) / 2",
          "as": "dateStart"
        },
        {
          "type": "formula",
          "expr": "datum.nextObj == null ?  data('range')[0].dateMax : (time(datum.nextObj.dateTime) + time(datum.dateTime)) / 2",
          "as": "dateEnd"
        }
      ]
    }
  ],

  "signals": [
    {},
    {},
    {},
    { "name": "extMin", "value": -1 },
    { "name": "extMax", "value": -1 },
    {
      "name": "yDomainSignal",
      "update": "(extMin === -1 || extMax === -1) ? null : [extMin, extMax]"
    },
    {
      "name": "tooltipSignal",
      "value": {},
      "on": [
        {
          "events": "@tooltip-hitarea:mouseover",
          "update": "datum"
        },
        {
          "events": "@tooltip-hitarea:mouseout",
          "update": "{}"
        }
      ]
    }
  ],

  "marks": [
    {
      "scales": [
        {},
        {
          "name": "yDetail",
          "type": "linear",
          "range": [390, 0],
          "nice": true,
          "zero": false,
          "padding": 0,

          "domain": {
            "fields": [
              {
                "data": "range", "field": "valueMax"
              },
              {
                "data": "table", "field": "value"
              }
            ]
          },
          "domainRaw":
          { "signal": "yDomainSignal" }
        }
      ],
      "axes": [
        {},
        {"orient": "left", "scale": "yDetail"}
      ],
      "marks": [
        {
          "marks": [
            {
              "name": "tooltip-hitarea",
              "type": "rect",
              "interactive": true,
              "from": {
                "data": "lookup"
              },
              "encode": {
                "enter": {
                  "tooltip": {
                    "signal": null
                  }
                },
                "update": {
                  "x": {
                    "scale": "xDetail",
                    "field": "dateStart"
                  },
                  "x2": {
                    "scale": "xDetail",
                    "field": "dateEnd"
                  },
                  "y": {
                    "scale": "yDetail",
                    "band": 1
                  },
                  "y2": {
                    "scale": "yDetail",
                    "signal": "-1e+36"
                  },
                  "fill": {
                    "value": "#00ff00"
                  },
                  "fillOpacity": {
                    "value": 0.0
                  }
                }
              }
            },
            {
              "type": "symbol",
              "from": {"data":"lookup"},
              "encode": {
                "update": {
                  "shape": {"value": "circle"},
                  "size": {"value": 30},
                  "x": {"scale": "xDetail", "field": "dateTime"},
                  "y": {"scale": "yDetail", "field": "value"},
                  "color": {"value": "both"},
                  "strokeWidth": {"value": 4},
                  "fill": {"value": "steelblue"}
                }
              }
            },
            {
              "type": "rule",
              "from": {
                "data": "lookup"
              },
              "encode": {
                "update": {
                  "x": {
                    "scale": "xDetail",
                    "field": "dateTime"
                  },
                  "x2": [
                    {
                      "test": "isValid(datum.obj)",
                      "scale": "xDetail",
                      "field": "obj.dateTime"
                    },
                    {
                      "scale": "xDetail",
                      "field": "dateTime"
                    }
                  ],
                  "y": {
                    "scale": "yDetail",
                    "field": "value"
                  },
                  "y2": [
                    {
                      "test": "isValid(datum.obj)",
                      "scale": "yDetail",
                      "field": "obj.value"
                    },
                    {
                      "scale": "yDetail",
                      "field": "value"
                    }
                  ],
                  "stroke": {
                    "value": "steelblue"
                  },
                  "strokeWidth": {
                    "value": 1
                  }
                }
              }
            },

            {
              "type": "rule",
              "interactive": false,
              "zindex": 20,
              "encode": {
                "update": {
                  "x": {
                    "scale": "xDetail",
                    "signal": "tooltipSignal.dateTime"
                  },
                  "y": {
                    "scale": "yDetail",
                    "band": 1
                  },
                  "y2": {
                    "scale": "yDetail",
                    "signal": "-1e+36"
                  },
                  "strokeOpacity": [
                    {
                      "test": "tooltipSignal.dateTime > 1",
                      "value": 1
                    },
                    {
                      "value": 0
                    }
                  ]
                }
              }
            }
          ]
        }
      ]
    },
    {
      "signals": [
        {
          "value": 0,
          "on": [
            {},
            {
              "events": "@overview:mousedown",
              "update": "[x(), x()]"
            },
            {
              "events": "[@overview:mousedown, window:mouseup] > window:mousemove!",
              "update": "[brush[0], clamp(x(), 0, width)]"
            }
          ]
        }
      ],
      "marks": [
        {},
        {},
        {},
        {
          "type": "symbol",
          "from": {"data":"table"},
          "encode": {
            "update": {
              "shape": {"value": "circle"},
              "size": {"value": 10},
              "x": {"scale": "xOverview", "field": "dateTime"},
              "y": {"scale": "yOverview", "field": "value"},
              "color": {"value": "both"},
              "strokeWidth": {"value": 4}
            }
          }
        },
        {
          "type": "rule",
          "from": {
            "data": "lookup"
          },
          "encode": {
            "update": {
              "x": {
                "scale": "xOverview",
                "field": "dateTime"
              },
              "x2": [
                {
                  "test": "isValid(datum.obj)",
                  "scale": "xOverview",
                  "field": "obj.dateTime"
                },
                {
                  "scale": "xOverview",
                  "field": "dateTime"
                }
              ],
              "y": {
                "scale": "yOverview",
                "field": "value"
              },
              "y2": [
                {
                  "test": "isValid(datum.obj)",
                  "scale": "yOverview",
                  "field": "obj.value"
                },
                {
                  "scale": "yOverview",
                  "field": "value"
                }
              ],
              "stroke": {
                "value": "steelblue"
              },
              "strokeWidth": {
                "value": 1
              }
            }
          }
        }
      ]
    }
  ]
}
