1

I am using a density transform in Vega Lite and would like to set the extent dynamically. It looks like the density transform is set up such that the extent can only be defined explicitly: https://vega.github.io/vega-lite/docs/density.html

{"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "params": [
    {"name": "myScale", "value": [0, 10]},
    {"name": "myExtent", "value": [0, 6]}
  ],
  "data": {
    "values": [
      {"game": 1, "score": 2, "flag": 0},
      {"game": 2, "score": 4, "flag": 0},
      {"game": 3, "score": 5, "flag": 0},
      {"game": 4, "score": 6, "flag": 1},
      {"game": 5, "score": 9, "flag": 0}
      ]
  },
  "mark": {"type": "area"},
  "transform": [
    {
      "density": "score",
      "extent": [0, 6]
      //"extent": {"param": "myExtent"}
    }
  ],
  "encoding": {
    "x": {
      "field": "value", "type": "quantitative",
      "scale": {
        "domain": {"param": "myScale"}
      }
    },
    "y": {"field": "density", "type": "quantitative"}
  }
}

I’ve shown above what does work (explicitly defining the extent to [0, 6]) and have commented out what I would like to do, but which doesn’t work (defining the extent using the myExtent parameter).

Am I missing something or is there an alternative approach that I can use? I also tried using a filter transform, but wasn’t successful there either.

sc20d
  • 47
  • 4

1 Answers1

0

Please try this.

enter image description here

{"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "params": [
    {"name": "myScale", "value": [0, 10]},
    {"name": "myExtent", "value": [0, 6]}
  ],
  "data": {
    "values": [
      {"game": 1, "score": 2, "flag": 0},
      {"game": 2, "score": 4, "flag": 0},
      {"game": 3, "score": 5, "flag": 0},
      {"game": 4, "score": 6, "flag": 1},
      {"game": 5, "score": 9, "flag": 0}
      ]
  },
  "mark": {"type": "area"},
  "transform": [
    {
      "density": "score",
      
      "extent": {"signal": "myExtent"}
    }
  ],
  "encoding": {
    "x": {
      "field": "value", "type": "quantitative",
      "scale": {
        "domain": {"param": "myScale"}
      }
    },
    "y": {"field": "density", "type": "quantitative"}
  }
}
Davide Bacci
  • 16,647
  • 3
  • 10
  • 36
  • David, many thanks for this. It worked perfectly! Totally understand if you don't have time for this, but any brief explanation of why adding the signal term makes this work would be most appreciated. I've run through the documentation a few times and didn't really understand why it is needed here. Also, the Vega Lite online editor complains about the addition of the term by underlining it. Either way, many thanks again for the speedy solution! – sc20d Nov 22 '22 at 12:59
  • One additional ask here: what I'm really trying to do is to set the extent from 0 to 6 based on the fact that the 4th data point has a score of 6 and it's the data point where flag is set to 1. Would you be able to help me out with setting the end point of an array based on a value in the data or suggest an alternative approach? – sc20d Nov 22 '22 at 14:19
  • See the issue here along with a message from the devs: https://github.com/vega/vega-lite/issues/8531 – Davide Bacci Nov 22 '22 at 14:25
  • Your second point should probably be asked as another question. – Davide Bacci Nov 22 '22 at 14:25