vega / vega

A visualization grammar.

Home Page:https://vega.github.io/vega

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

vlSelectionTest doesn't handle null values

jonmmease opened this issue · comments

I'm working with Vega-Lite's selections framework, but since vlSelectionTest lives in this repo I'm opening the issue here.

Vega-Lite spec for simple pie chart with a null-valued wedge and click highlighting enabled.

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "A simple pie chart with embedded data.",
  "data": {
    "values": [
      {"category": 1, "value": 4},
      {"category": 2, "value": 6},
      {"category": 3, "value": 10},
      {"category": 4, "value": 3},
      {"category": 5, "value": 7},
      {"category": 6, "value": 8},
      {"category": null, "value": 10}
    ]
  },
  "mark": "arc",
  "encoding": {
    "theta": {"field": "value", "type": "quantitative"},
    "color": {"field": "category", "type": "nominal"},
    "opacity": {"condition": {"test": {"param": "click"}, "value": 1.0}, "value": 0.5}
  },
  "params": [{
    "name": "click",
    "select": {
      "type": "point",
      "encodings": ["color"]
    }
  }]
}

visualization (1)

This compiles into the following Vega spec

{
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "description": "A simple pie chart with embedded data.",
  "background": "white",
  "padding": 5,
  "width": 200,
  "height": 200,
  "style": "view",
  "data": [
    {"name": "click_store"},
    {
      "name": "source_0",
      "values": [
        {"category": 1, "value": 4},
        {"category": 2, "value": 6},
        {"category": 3, "value": 10},
        {"category": 4, "value": 3},
        {"category": 5, "value": 7},
        {"category": 6, "value": 8},
        {"category": null, "value": 10}
      ]
    },
    {
      "name": "data_0",
      "source": "source_0",
      "transform": [
        {
          "type": "stack",
          "groupby": [],
          "field": "value",
          "sort": {"field": ["category"], "order": ["ascending"]},
          "as": ["value_start", "value_end"],
          "offset": "zero"
        },
        {
          "type": "filter",
          "expr": "isValid(datum[\"value\"]) && isFinite(+datum[\"value\"])"
        }
      ]
    }
  ],
  "signals": [
    {
      "name": "unit",
      "value": {},
      "on": [
        {"events": "mousemove", "update": "isTuple(group()) ? group() : unit"}
      ]
    },
    {
      "name": "click",
      "update": "vlSelectionResolve(\"click_store\", \"union\", true, true)"
    },
    {
      "name": "click_tuple",
      "on": [
        {
          "events": [{"source": "scope", "type": "click"}],
          "update": "datum && item().mark.marktype !== 'group' && indexof(item().mark.role, 'legend') < 0 ? {unit: \"\", fields: click_tuple_fields, values: [(item().isVoronoi ? datum.datum : datum)[\"category\"]]} : null",
          "force": true
        },
        {"events": [{"source": "view", "type": "dblclick"}], "update": "null"}
      ]
    },
    {
      "name": "click_tuple_fields",
      "value": [{"field": "category", "channel": "color", "type": "E"}]
    },
    {
      "name": "click_toggle",
      "value": false,
      "on": [
        {
          "events": [{"source": "scope", "type": "click"}],
          "update": "event.shiftKey"
        },
        {"events": [{"source": "view", "type": "dblclick"}], "update": "false"}
      ]
    },
    {
      "name": "click_modify",
      "on": [
        {
          "events": {"signal": "click_tuple"},
          "update": "modify(\"click_store\", click_toggle ? null : click_tuple, click_toggle ? null : true, click_toggle ? click_tuple : null)"
        }
      ]
    }
  ],
  "marks": [
    {
      "name": "marks",
      "type": "arc",
      "style": ["arc"],
      "interactive": true,
      "from": {"data": "data_0"},
      "encode": {
        "update": {
          "fill": {"scale": "color", "field": "category"},
          "opacity": [
            {
              "test": "!length(data(\"click_store\")) || vlSelectionTest(\"click_store\", datum)",
              "value": 1
            },
            {"value": 0.5}
          ],
          "description": {
            "signal": "\"value: \" + (format(datum[\"value\"], \"\")) + \"; category: \" + (isValid(datum[\"category\"]) ? datum[\"category\"] : \"\"+datum[\"category\"])"
          },
          "x": {"signal": "width", "mult": 0.5},
          "y": {"signal": "height", "mult": 0.5},
          "outerRadius": {"signal": "min(width,height)/2"},
          "innerRadius": {"value": 0},
          "startAngle": {"scale": "theta", "field": "value_end"},
          "endAngle": {"scale": "theta", "field": "value_start"}
        }
      }
    }
  ],
  "scales": [
    {
      "name": "theta",
      "type": "linear",
      "domain": {"data": "data_0", "fields": ["value_start", "value_end"]},
      "range": [0, 6.283185307179586],
      "zero": true
    },
    {
      "name": "color",
      "type": "ordinal",
      "domain": {"data": "data_0", "field": "category", "sort": true},
      "range": "category"
    }
  ],
  "legends": [
    {
      "fill": "color",
      "symbolType": "circle",
      "title": "category",
      "encode": {"symbols": {"update": {"opacity": {"value": 1}}}}
    }
  ]
}

Open the Chart in the Vega Editor

When clicking on the 1-valued wedge, the click_store dataset is set to this

Screenshot 2023-07-21 at 11 11 16 AM
and all of the other wedges turn transparent:

visualization (2)

When clicking on the null-valued wdget, the click_store dataset is set to this

Screenshot 2023-07-21 at 11 12 44 AM

but the wedge is not highlighted and this error is raised in the console:

index-1cb9cf04.js:685 TypeError: Cannot read properties of null (reading '0')
    at Zte (index-1cb9cf04.js:84:10747)
    at Object.KUe [as vlSelectionTest] (index-1cb9cf04.js:84:11344)
    at Object.eval (eval at j_ (index-1cb9cf04.js:84:24669), <anonymous>:3:143)
    at index-1cb9cf04.js:82:23367
    at Array.forEach (<anonymous>)
    at eg (index-1cb9cf04.js:76:33193)
    at Rg.visit (index-1cb9cf04.js:81:11952)
    at zge.transform (index-1cb9cf04.js:82:23354)
    at zge.evaluate (index-1cb9cf04.js:81:16773)
    at zge.run (index-1cb9cf04.js:81:16630)

I'm doing this in the Vega editor, and it looks like the Vega bundle is minified. But the error is happening somewhere in vlSelectionTest.