{
  "data": [
    {
      "name": "legend",
      "values": null
    },
    {
      "name": "source",
      "values": null
    },
    {
      "name": "flatten",
      "source": "source"
    },
    {
      "name": "table",
      "source": "flatten",
      "transform": [
        {
          "type": "project",
          "fields": [
            {"signal" :  "valuePath"},
            {"signal":  "timestampPath"}

          ],
          "as": [
            "value",
            "timestamp"
          ]
        },
        {
          "type": "formula",
          "expr": "utcParse(datum.timestamp, '%Y-%m-%d %H:%M:%S%Z')",
          "as": "dateTime"
        }
      ]
    },
    {
      "name": "range",
      "source": "table",
      "transform": [
        {
          "type": "aggregate",
          "fields": ["dateTime", "dateTime", "value"],
          "ops": ["min", "max", "max"],
          "as": ["dateMinTemp", "dateMaxTemp", "maxValueTemp"]
        },
        {
          "type": "formula",
          "expr":"time(datum[\"dateMinTemp\"]) - timeWindow",
          "as": "dateMin"
        },
        {
          "type": "formula",
          "expr":"time(datum[\"dateMaxTemp\"]) + timeWindow",
          "as": "dateMax"
        },
        {
          "type": "formula",
          "expr": "datum[\"maxValueTemp\"] * 1.0",
          "as": "valueMax"
        }
      ]
    }
  ],

  "signals": [

    {
      "name": "timeWindow",
      "value": null
    },
    {
      "name":  "maxTimeDifference",
      "update": "timeWindow * 5"
    },
    {
      "name": "detailDomain"
    }

  ],

  "marks": [
    {
      "type": "group",
      "name": "detail",
      "encode": {
        "update": {
          "height": {"signal": "detailHeight"},
          "width": {"signal": "width"}
        }
      },

      "scales": [
        {
          "name": "xDetail",
          "type": "time",
          "range": "width",
          "domain": {
          "data": "range",
          "fields": [
            "dateMin",
            "dateMax"
          ]
        },
          "domainRaw": {"signal": "detailDomain"}
        }
      ],
      "axes": [
        {"orient": "bottom", "scale": "xDetail"}
      ],
      "marks": [
        {
          "type": "group",
          "encode": {
            "update": {
              "height": {
                "field": {
                  "group": "height"
                }
              },
              "width": {
                "field": {
                  "group": "width"
                }
              },
              "clip": {
                "value": true
              }
            }
          }
        }
      ]
    },

    {
      "type": "group",
      "name": "overview",
      "encode": {
        "update": {
          "x": {"value": 0},
          "y": {"signal":  "overviewHeightStart"},
          "height": {"signal": "overviewHeight"},
          "width": {"signal":  "width"},
          "fill": {"value": "transparent"}
        }
      },
      "signals": [
        {
          "name": "brush",
          "on": [
            {
              "events": {"signal": "delta"},
              "update": "clampRange([anchor[0] + delta, anchor[1] + delta], 0, width)"
            }
          ]
        },
        {
          "name": "anchor", "value": null,
          "on": [{"events": "@brush:mousedown", "update": "slice(brush)"}]
        },
        {
          "name": "xdown", "value": 0,
          "on": [{"events": "@brush:mousedown", "update": "x()"}]
        },
        {
          "name": "delta", "value": 0,
          "on": [
            {
              "events": "[@brush:mousedown, window:mouseup] > window:mousemove!",
              "update": "x() - xdown"
            }
          ]
        },
        {
          "name": "detailDomain",
          "push": "outer",
          "on": [
            {
              "events": {"signal": "brush"},
              "update": "span(brush) ? invert('xOverview', brush) : null"
            }
          ]
        }
      ],
      "scales": [
        {
          "name": "xOverview",
          "type": "time",
          "range": "width",
          "domain": {"data": "range", "fields": ["dateMin", "dateMax"]}
        },
        {
          "name": "yOverview",
          "type": "linear",
          "range": [70, 0],
          "domain": {"data": "table", "field": "value"},
          "nice": true,
          "zero": false,
          "padding": 0
        }
      ],
      "axes": [
        {"orient": "bottom", "scale": "xOverview"}
      ],
      "marks": [
        {
          "type": "rect",
          "name": "brush",
          "encode": {
            "enter": {
              "y": {"value": 0},
              "height": {"value": 70},
              "fill": {"value": "#333"},
              "fillOpacity": {"value": 0.2}
            },
            "update": {
              "x": {"signal": "brush[0]"},
              "x2": {"signal": "brush[1]"}
            }
          }
        },
        {
          "type": "rect",
          "interactive": false,
          "encode": {
            "enter": {
              "y": {"value": 0},
              "height": {"value": 70},
              "width": {"value": 1},
              "fill": {"value": "firebrick"}
            },
            "update": {
              "x": {"signal": "brush[0]"}
            }
          }
        },
        {
          "type": "rect",
          "interactive": false,
          "encode": {
            "enter": {
              "y": {"value": 0},
              "height": {"value": 70},
              "width": {"value": 1},
              "fill": {"value": "firebrick"}
            },
            "update": {
              "x": {"signal": "brush[1]"}
            }
          }
        }
      ]
    }
  ]
}



