{ "$schema": "https://vega.github.io/schema/vega/v5.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.", "background": "white", "padding": 5, "width": 200, "style": "cell", "data": [ { "name": "source_0", "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": 79}, {"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} ] }, { "name": "data_0", "source": "source_0", "transform": [ { "type": "window", "params": [null], "as": ["rank"], "ops": ["rank"], "fields": [null], "sort": {"field": ["score"], "order": ["descending"]} }, {"type": "filter", "expr": "datum.rank <= 5"} ] }, { "name": "data_1", "source": "data_0", "transform": [ { "type": "stack", "groupby": ["student"], "field": "score", "sort": {"field": [], "order": []}, "as": ["score_start", "score_end"], "offset": "zero" }, { "type": "filter", "expr": "isValid(datum[\"score\"]) && isFinite(+datum[\"score\"])" } ] } ], "signals": [ {"name": "y_step", "value": 20}, { "name": "height", "update": "bandspace(domain('y').length, 0.1, 0.05) * y_step" } ], "marks": [ { "name": "marks", "type": "rect", "style": ["bar"], "from": {"data": "data_1"}, "encode": { "update": { "fill": {"value": "#4c78a8"}, "ariaRoleDescription": {"value": "bar"}, "description": { "signal": "\"score: \" + (format(datum[\"score\"], \"\")) + \"; student: \" + (isValid(datum[\"student\"]) ? datum[\"student\"] : \"\"+datum[\"student\"])" }, "x": {"scale": "x", "field": "score_end"}, "x2": {"scale": "x", "field": "score_start"}, "y": {"scale": "y", "field": "student"}, "height": {"scale": "y", "band": 1} } } } ], "scales": [ { "name": "x", "type": "linear", "domain": {"data": "data_1", "fields": ["score_start", "score_end"]}, "range": [0, {"signal": "width"}], "nice": true, "zero": true }, { "name": "y", "type": "band", "domain": { "data": "data_0", "field": "student", "sort": {"op": "average", "field": "score", "order": "descending"} }, "range": {"step": {"signal": "y_step"}}, "paddingInner": 0.1, "paddingOuter": 0.05 } ], "axes": [ { "scale": "x", "orient": "bottom", "gridScale": "y", "grid": true, "tickCount": {"signal": "ceil(width/40)"}, "domain": false, "labels": false, "aria": false, "maxExtent": 0, "minExtent": 0, "ticks": false, "zindex": 0 }, { "scale": "x", "orient": "bottom", "grid": false, "title": "score", "labelFlush": true, "labelOverlap": true, "tickCount": {"signal": "ceil(width/40)"}, "zindex": 0 }, { "scale": "y", "orient": "left", "grid": false, "title": "student", "zindex": 0 } ] }