{ "$schema": "https://vega.github.io/schema/vega/v5.json", "description": "Vega-Lite version of bar chart from https://observablehq.com/@d3/learn-d3-scales.", "background": "white", "padding": 5, "width": 400, "style": "cell", "data": [ { "name": "source_0", "values": [ {"name": "🍊", "count": 21}, {"name": "🍇", "count": 13}, {"name": "🍏", "count": 8}, {"name": "🍌", "count": 5}, {"name": "🍐", "count": 3}, {"name": "🍋", "count": 2}, {"name": "🍎", "count": 1}, {"name": "🍉", "count": 1} ] }, { "name": "data_1", "source": "source_0", "transform": [ { "type": "stack", "groupby": ["name"], "field": "count", "sort": {"field": ["count"], "order": ["ascending"]}, "as": ["count_start", "count_end"], "offset": "zero" }, { "type": "filter", "expr": "isValid(datum[\"count\"]) && isFinite(+datum[\"count\"])" } ] }, { "name": "data_2", "source": "source_0", "transform": [ { "type": "filter", "expr": "isValid(datum[\"count\"]) && isFinite(+datum[\"count\"])" } ] } ], "signals": [ {"name": "y_step", "value": 20}, { "name": "height", "update": "bandspace(domain('y').length, 0.1, 0.05) * y_step" } ], "marks": [ { "name": "layer_0_marks", "type": "rect", "style": ["bar"], "from": {"data": "data_1"}, "encode": { "update": { "fill": {"scale": "color", "field": "count"}, "ariaRoleDescription": {"value": "bar"}, "description": { "signal": "\"count: \" + (format(datum[\"count\"], \"\")) + \"; name: \" + (isValid(datum[\"name\"]) ? datum[\"name\"] : \"\"+datum[\"name\"]) + \"; Number of fruit: \" + (format(datum[\"count\"], \"\"))" }, "x": {"scale": "x", "field": "count_end"}, "x2": {"scale": "x", "field": "count_start"}, "y": {"scale": "y", "field": "name"}, "height": {"scale": "y", "band": 1} } } }, { "name": "layer_1_marks", "type": "text", "style": ["text"], "aria": false, "from": {"data": "data_2"}, "encode": { "update": { "align": {"value": "right"}, "fill": [ {"test": "datum[\"count\"]>10", "value": "white"}, {"value": "black"} ], "x": {"scale": "x", "field": "count", "offset": -4}, "y": {"scale": "y", "field": "name", "band": 0.5}, "text": {"signal": "format(datum[\"count\"], \"\")"}, "baseline": {"value": "middle"} } } } ], "scales": [ { "name": "x", "type": "linear", "domain": { "fields": [ {"data": "data_1", "field": "count_start"}, {"data": "data_1", "field": "count_end"}, {"data": "data_2", "field": "count"} ] }, "range": [0, {"signal": "width"}], "nice": true, "zero": true }, { "name": "y", "type": "band", "domain": { "data": "source_0", "field": "name", "sort": {"op": "min", "field": "count", "order": "descending"} }, "range": {"step": {"signal": "y_step"}}, "paddingInner": 0.1, "paddingOuter": 0.05 }, { "name": "color", "type": "linear", "domain": {"data": "data_1", "field": "count"}, "range": "ramp", "interpolate": "hcl", "zero": false } ], "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, "labelFlush": true, "labelOverlap": true, "tickCount": {"signal": "ceil(width/40)"}, "zindex": 0 }, {"scale": "y", "orient": "left", "grid": false, "zindex": 0} ], "legends": [ { "title": "Number of fruit", "fill": "color", "gradientLength": {"signal": "clamp(height, 64, 200)"} } ] }