{
  "data": [
    {},
    {},
    {
      "transform": [
        {
          "type": "flatten",
          "fields": [
            {
              "signal": "dataPath"
            }
          ]
        }
      ]
    },
    {
      "transform": [
        {
          "type": "project",
          "fields": [
            {
              "signal": "valuePath"
            },
            {
              "signal": "timestampPath"
            },
            {
              "signal": "sensorPath"
            }
          ],
          "as": [
            "value",
            "timestamp",
            "sensor"
          ]
        }
      ]
    },
    {},
    {
      "name": "ranks",
      "source": "table",
      "transform": [
        {
          "type": "window",
          "sort": {
            "field": [
              "sensor",
              "dateTime"
            ],
            "order": [
              "ascending",
              "ascending"
            ]
          },
          "ops": [
            "rank"
          ],
          "fields": [
            "value"
          ],
          "as": [
            "rank"
          ]
        },
        {
          "type": "formula",
          "expr": "datum.rank + 1",
          "as": "nextRank"
        }
      ]
    },
    {
      "name": "lookup",
      "source": "ranks",
      "transform": [
        {
          "type": "lookup",
          "from": "ranks",
          "key": "rank",
          "fields": [
            "nextRank"
          ],
          "as": [
            "tempObj"
          ]
        },
        {
          "type": "formula",
          "expr": "(datum.tempObj == null || datum.tempObj.sensor != datum.sensor || datum.tempObj.dateTime - datum.dateTime > maxTimeDifference) ? null : datum.tempObj",
          "as": "obj"
        }
      ]
    },
    {
      "name": "tooltipTmp1",
      "source": "lookup",
      "transform": [
        {
          "type": "pivot",
          "groupby": [
            "dateTime"
          ],
          "field": "sensor",
          "value": "value",
          "op": "median"
        }
      ]
    },
    {
      "name": "tooltipTmp2",
      "source": "tooltipTmp1",
      "transform": [
        {
          "type": "window",
          "sort": {
            "field": [
              "dateTime"
            ],
            "order": [
              "ascending"
            ]
          },
          "ops": [
            "rank"
          ],
          "fields": [
            "value"
          ],
          "as": [
            "rank"
          ]
        },
        {
          "type": "formula",
          "expr": "datum.rank - 1",
          "as": "prevRank"
        },
        {
          "type": "formula",
          "expr": "datum.rank + 1",
          "as": "nextRank"
        }
      ]
    },
    {
      "name": "tooltip",
      "source": "tooltipTmp2",
      "transform": [
        {
          "type": "lookup",
          "from": "tooltipTmp2",
          "key": "rank",
          "fields": [
            "nextRank"
          ],
          "as": [
            "nextObj"
          ]
        },
        {
          "type": "lookup",
          "from": "tooltipTmp2",
          "key": "rank",
          "fields": [
            "prevRank"
          ],
          "as": [
            "prevObj"
          ]
        },
        {
          "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": "tooltipSignal",
      "value": {},
      "on": [
        {
          "events": "@tooltip-hitarea:mouseover",
          "update": "datum"
        },
        {
          "events": "@tooltip-hitarea:mouseout",
          "update": "{}"
        }
      ]
    }
  ],
  "marks": [
    {
      "scales": [
        {},
        {
          "name": "yDetail",
          "type": "linear",
          "range": [
            390,
            0
          ],
          "domain": {
            "fields": [
              {
                "data": "range",
                "field": "valueMax"
              },
              {
                "data": "table",
                "field": "value"
              }
            ]
          },
          "nice": true,
          "zero": false,
          "padding": 0
        },
        {
          "name": "color",
          "type": "ordinal",
          "range": {
            "scheme": "category20"
          },
          "domain": {
            "data": "table",
            "field": "sensor"
          }
        }
      ],
      "legends": [
        {
          "fill": "color",
          "title": "Sensors:",
          "offset": 0,
          "encode": {
            "symbols": {
              "update": {
                "fillOpacity": {
                  "value": 0.5
                },
                "stroke": {
                  "value": "transparent"
                }
              }
            }
          }
        }
      ],
      "axes": [
        {},
        {
          "orient": "left",
          "scale": "yDetail"
        }
      ],
      "marks": [
        {
          "marks": [
            {
              "type": "group",
              "from": {
                "facet": {
                  "name": "series",
                  "data": "lookup",
                  "groupby": "sensor"
                }
              },
              "marks": [
                {
                  "type": "symbol",
                  "from": {
                    "data": "series"
                  },
                  "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": {
                        "scale": "color",
                        "field": "sensor"
                      }
                    }
                  }
                },
                {
                  "type": "rule",
                  "from": {
                    "data": "series"
                  },
                  "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": {
                        "scale": "color",
                        "field": "sensor"
                      },
                      "strokeWidth": {
                        "value": 1
                      }
                    }
                  }
                },
                {
                  "name": "tooltip-hitarea",
                  "type": "rect",
                  "interactive": true,
                  "from": {
                    "data": "tooltip"
                  },
                  "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": "rule",
                  "interactive": false,
                  "zindex": 20,
                  "encode": {
                    "enter": {
                    },
                    "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)]"
            }
          ]
        }
      ],
      "scales": [
        {},
        {},
        {
          "name": "color",
          "type": "ordinal",
          "range": {
            "scheme": "category20"
          },
          "domain": {
            "data": "table",
            "field": "sensor"
          }
        }
      ],
      "marks": [
        {},
        {},
        {},
        {
          "type": "group",
          "from": {
            "facet": {
              "name": "series",
              "data": "lookup",
              "groupby": "sensor"
            }
          },
          "marks": [
            {
              "type": "symbol",
              "from": {
                "data": "series"
              },
              "encode": {
                "update": {
                  "shape": {
                    "value": "circle"
                  },
                  "size": {
                    "value": 30
                  },
                  "x": {
                    "scale": "xOverview",
                    "field": "dateTime"
                  },
                  "y": {
                    "scale": "yOverview",
                    "field": "value"
                  },
                  "color": {
                    "value": "both"
                  },
                  "strokeWidth": {
                    "value": 4
                  },
                  "fill": {
                    "scale": "color",
                    "field": "sensor"
                  }
                }
              }
            },
            {
              "type": "rule",
              "from": {
                "data": "series"
              },
              "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": {
                    "scale": "color",
                    "field": "sensor"
                  },
                  "strokeWidth": {
                    "value": 1
                  }
                }
              }
            }
          ]
        }
      ]
    }
  ]
}

