parametersvega-litedensity-plotextent

Vega Lite - cannot set extent of density transform with parameter


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.


Solution

  • 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"}
      }
    }