{ "$schema": "https://vega.github.io/schema/vega/v5.json", "description": "Bar chart with text labels. Set domain to make the frame cover the labels.", "background": "white", "padding": 5, "width": 200, "style": "cell", "data": [ { "name": "source_0", "values": [{"a": "A", "b": 28}, {"a": "B", "b": 55}, {"a": "C", "b": 43}] }, { "name": "data_1", "source": "source_0", "transform": [ { "type": "stack", "groupby": ["a"], "field": "b", "sort": {"field": [], "order": []}, "as": ["b_start", "b_end"], "offset": "zero" }, { "type": "filter", "expr": "isValid(datum[\"b\"]) && isFinite(+datum[\"b\"])" } ] }, { "name": "data_2", "source": "source_0", "transform": [ { "type": "filter", "expr": "isValid(datum[\"b\"]) && isFinite(+datum[\"b\"])" } ] } ], "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": {"value": "#4c78a8"}, "ariaRoleDescription": {"value": "bar"}, "description": { "signal": "\"b: \" + (format(datum[\"b\"], \"\")) + \"; a: \" + (isValid(datum[\"a\"]) ? datum[\"a\"] : \"\"+datum[\"a\"])" }, "x": {"scale": "x", "field": "b_end"}, "x2": {"scale": "x", "field": "b_start"}, "y": {"scale": "y", "field": "a"}, "height": {"scale": "y", "band": 1} } } }, { "name": "layer_1_marks", "type": "text", "style": ["text"], "from": {"data": "data_2"}, "encode": { "update": { "align": {"value": "left"}, "baseline": {"value": "middle"}, "dx": {"value": 3}, "fill": {"value": "black"}, "description": { "signal": "\"b: \" + (format(datum[\"b\"], \"\")) + \"; a: \" + (isValid(datum[\"a\"]) ? datum[\"a\"] : \"\"+datum[\"a\"])" }, "x": {"scale": "x", "field": "b"}, "y": {"scale": "y", "field": "a", "band": 0.5}, "text": {"signal": "format(datum[\"b\"], \"\")"} } } } ], "scales": [ { "name": "x", "type": "linear", "domain": [0, 60], "range": [0, {"signal": "width"}], "zero": true }, { "name": "y", "type": "band", "domain": { "fields": [ {"data": "data_1", "field": "a"}, {"data": "data_2", "field": "a"} ], "sort": true }, "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": "b", "labelFlush": true, "labelOverlap": true, "tickCount": {"signal": "ceil(width/40)"}, "zindex": 0 }, {"scale": "y", "orient": "left", "grid": false, "title": "a", "zindex": 0} ] }