Mattijn
Mattijn

Reputation: 13880

Filter by selection from referenced table

I have a single data source within datasets that I use in two concatenated charts. Now when I click on a bar in the left chart I would like to filter the right chart. See animated gif: enter image description here

But is doesn't. Click here to open in the vega-editor. Or check the spec here::

{
  "config": {"view": {"width": 400, "height": 300}},
  "hconcat": [
    {
      "width": 100,
      "selection": {"SELECT": {"type": "single", "resolve": "global"}},
      "data": {"name": "table_data"},
      "mark": "bar",
      "encoding": {
        "x": {"type": "nominal", "field": "Major_Genre"},
        "y": {"aggregate": "count", "type": "quantitative"},
        "color": {"type": "nominal", "field": "Major_Genre"},
        "fillOpacity": {
          "condition": {"selection": "SELECT", "value": 1},
          "value": 0.3
        }
      }
    },
    {
      "width": 100,
      "data": {"name": "table_data"},
      "transform": [{"filter": {"selection": "SELECT"}}],
      "mark": "bar",
      "encoding": {
        "x": {"type": "nominal", "field": "Major_Genre"},
        "y": {"aggregate": "count", "type": "quantitative"},
        "color": {"type": "nominal", "field": "Major_Genre"}
      }
    }
  ],
  "datasets": {
    "table_data": [
      {
        "Title": "Cidade de Deus",
        "Major_Genre": "Drama",
        "Country_Origin": "Brazil"
      },
      {
        "Title": "Chocolate: Deep Dark Secrets",
        "Major_Genre": "Thriller/Suspense",
        "Country_Origin": "India"
      },
      {"Title": "Fiza", "Major_Genre": "Drama", "Country_Origin": "India"},
      {
        "Title": "First Love, Last Rites",
        "Major_Genre": "Drama",
        "Country_Origin": "United States"
      },
      {
        "Title": "Foolish",
        "Major_Genre": "Comedy",
        "Country_Origin": "United States"
      },
      {
        "Title": "I Married a Strange Person",
        "Major_Genre": "Comedy",
        "Country_Origin": "United States"
      }
    ]
  }
}

Upvotes: 1

Views: 135

Answers (1)

jakevdp
jakevdp

Reputation: 86330

When your selections refer to groups of points rather than single points, you need to provide an explicit fields or encodings argument to specify what points are included in the selection.

In this case, the chart will behave as expected if you specify your selection in one of the following ways:

"selection": {"SELECT": {"type": "single", "fields": ["Major_Genre"], "resolve": "global"}}

"selection": {"SELECT": {"type": "single", "encodings": ["color"], "resolve": "global"}}

"selection": {"SELECT": {"type": "single", "encodings": ["x"], "resolve": "global"}}

Here's the working chart in action: (vega editor link).

Upvotes: 1

Related Questions