Filtering top-K items

Here we use window transform to derive the total number of students along with the rank of the current student to determine the top K students and display their score.

View this example in the online editor

Vega-Lite JSON Specification

{
  "$schema": "https://vega.github.io/schema/vega-lite/v3.json",
  "description": "A bar graph showing the scores of the top 5 students. This shows an example of the window transform, for how the top K (5) can be filtered, and also how a rank can be computed for each student.",
  "data": {
    "values": [
      {"student": "A", "score": 100}, {"student": "B", "score": 56},
      {"student": "C", "score": 88}, {"student": "D", "score": 65},
      {"student": "E", "score": 45}, {"student": "F", "score": 23},
      {"student": "G", "score": 66}, {"student": "H", "score": 67},
      {"student": "I", "score": 13}, {"student": "J", "score": 12},
      {"student": "K", "score": 50}, {"student": "L", "score": 78},
      {"student": "M", "score": 66}, {"student": "N", "score": 30},
      {"student": "O", "score": 97}, {"student": "P", "score": 75},
      {"student": "Q", "score": 24}, {"student": "R", "score": 42},
      {"student": "S", "score": 76}, {"student": "T", "score": 78},
      {"student": "U", "score": 21}, {"student": "V", "score": 46}
    ]
  },
  "transform": [
    {
      "window": [{
        "op": "rank",
        "as": "rank"
      }],
      "sort": [{ "field": "score", "order": "descending" }]
    }, {
      "filter": "datum.rank <= 5"
    }
  ],
  "mark": "bar",
  "encoding": {
    "x": {
        "field": "score",
        "type": "quantitative"
    },
    "y": {
        "field": "student",
        "type": "nominal",
        "sort": {"field": "score", "op": "average", "order":"descending"}
    }
  }
}