Vega lite - Create a legend for a layered line plot

Viewed 17

I am trying to create a legend for a layered line plot, I tried both datum and fold solutions described in the following topic but did not succeed to make it work:

How do I create a legend for a layered line plot

Here is my current vega-lite code using datum but not legend shows off:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v2.json",
  
  
  title: {
    text: Main title
    anchor: "start"},
  data: {
    url: {
      %context%: true,
      %timefield%: "MESSAGEDATE",
      index: "mms-maws-ott",
      body: {
        size: 10000,
        _source: ["MESSAGEDATE", "wl_pressure", "station_id", "wl_radar"]
      }
    },
    format: {"property": "hits.hits"}
  },
  transform: [
    {"calculate": "toDate(datum._source['MESSAGEDATE'])", "as": "time"}
    {"calculate": "datum._source['wl_radar'] - datum._source['wl_pressure']", "as": "Offset"}
  ],
  
  
  encoding: {
    x: {
      field: time
      type: temporal
      title: "Date"
    }}

  
  layer: [{
  "mark": {"type": "line"},
  encoding: {
    y: {
      field: _source.wl_pressure
      type: quantitative
      title: "Bubble"
    },
    color: {datum: "wl_pressure"}
    }
  },
  {
  "mark": {"type": "line"},
  encoding: {
    y: {
      field: _source.wl_radar
      type: quantitative
      title: "Radar"
    },
  color: {datum: "_source.wl_radar"}
    }
  },
  {
  "mark": {"type": "line"},
  encoding: {
    y: {
      field: Offset
      type: quantitative
      title: "Offset"
    },
    color: {datum: "Offset"}
    }
  }
  ]
}

Current visualization

0 Answers
Related