|
| 1 | +{ |
| 2 | + "$schema": "https://vega.github.io/schema/vega/v5.json", |
| 3 | + "description": "A simple bar chart with embedded data.", |
| 4 | + "background": "white", |
| 5 | + "padding": 5, |
| 6 | + "height": 200, |
| 7 | + "style": "cell", |
| 8 | + "data": [ |
| 9 | + { |
| 10 | + "name": "source_0", |
| 11 | + "values": [ |
| 12 | + {"a": "A", "b": 28}, |
| 13 | + {"a": "B", "b": 55}, |
| 14 | + {"a": "C", "b": null} |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "name": "data_0", |
| 19 | + "source": "source_0", |
| 20 | + "transform": [ |
| 21 | + { |
| 22 | + "type": "stack", |
| 23 | + "groupby": ["a"], |
| 24 | + "field": "b", |
| 25 | + "sort": {"field": [], "order": []}, |
| 26 | + "as": ["b_start", "b_end"], |
| 27 | + "offset": "zero" |
| 28 | + } |
| 29 | + ] |
| 30 | + } |
| 31 | + ], |
| 32 | + "signals": [ |
| 33 | + {"name": "x_step", "value": 20}, |
| 34 | + { |
| 35 | + "name": "width", |
| 36 | + "update": "bandspace(domain('x').length, 0.1, 0.05) * x_step" |
| 37 | + } |
| 38 | + ], |
| 39 | + "marks": [ |
| 40 | + { |
| 41 | + "name": "layer_0_marks", |
| 42 | + "type": "rect", |
| 43 | + "style": ["bar"], |
| 44 | + "from": {"data": "data_0"}, |
| 45 | + "encode": { |
| 46 | + "update": { |
| 47 | + "fill": {"value": "#4c78a8"}, |
| 48 | + "ariaRoleDescription": {"value": "bar"}, |
| 49 | + "description": { |
| 50 | + "signal": "\"a: \" + (isValid(datum[\"a\"]) ? datum[\"a\"] : \"\"+datum[\"a\"]) + \"; b: \" + (format(datum[\"b\"], \"\"))" |
| 51 | + }, |
| 52 | + "x": {"scale": "x", "field": "a"}, |
| 53 | + "width": {"signal": "max(0.25, bandwidth('x'))"}, |
| 54 | + "y": {"scale": "y", "field": "b_end"}, |
| 55 | + "y2": {"scale": "y", "field": "b_start"} |
| 56 | + } |
| 57 | + } |
| 58 | + }, |
| 59 | + { |
| 60 | + "name": "layer_1_marks", |
| 61 | + "type": "text", |
| 62 | + "style": ["text"], |
| 63 | + "from": {"data": "source_0"}, |
| 64 | + "encode": { |
| 65 | + "update": { |
| 66 | + "baseline": {"value": "bottom"}, |
| 67 | + "fill": {"value": "black"}, |
| 68 | + "description": { |
| 69 | + "signal": "\"a: \" + (isValid(datum[\"a\"]) ? datum[\"a\"] : \"\"+datum[\"a\"]) + \"; b: \" + (format(datum[\"b\"], \"\"))" |
| 70 | + }, |
| 71 | + "x": {"scale": "x", "field": "a", "band": 0.5}, |
| 72 | + "y": [ |
| 73 | + { |
| 74 | + "test": "!isValid(datum[\"b\"]) || !isFinite(+datum[\"b\"])", |
| 75 | + "scale": "y", |
| 76 | + "value": 0 |
| 77 | + }, |
| 78 | + {"scale": "y", "field": "b"} |
| 79 | + ], |
| 80 | + "text": {"signal": "format(datum[\"b\"], \"\")"}, |
| 81 | + "align": {"value": "center"} |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + ], |
| 86 | + "scales": [ |
| 87 | + { |
| 88 | + "name": "x", |
| 89 | + "type": "band", |
| 90 | + "domain": { |
| 91 | + "fields": [ |
| 92 | + {"data": "data_0", "field": "a"}, |
| 93 | + {"data": "source_0", "field": "a"} |
| 94 | + ], |
| 95 | + "sort": true |
| 96 | + }, |
| 97 | + "range": {"step": {"signal": "x_step"}}, |
| 98 | + "paddingInner": 0.1, |
| 99 | + "paddingOuter": 0.05 |
| 100 | + }, |
| 101 | + { |
| 102 | + "name": "y", |
| 103 | + "type": "linear", |
| 104 | + "domain": { |
| 105 | + "fields": [ |
| 106 | + {"data": "data_0", "field": "b_start"}, |
| 107 | + {"data": "data_0", "field": "b_end"}, |
| 108 | + {"data": "source_0", "field": "b"} |
| 109 | + ] |
| 110 | + }, |
| 111 | + "range": [{"signal": "height"}, 0], |
| 112 | + "nice": true, |
| 113 | + "zero": true |
| 114 | + } |
| 115 | + ], |
| 116 | + "axes": [ |
| 117 | + { |
| 118 | + "scale": "y", |
| 119 | + "orient": "left", |
| 120 | + "gridScale": "x", |
| 121 | + "grid": true, |
| 122 | + "tickCount": {"signal": "ceil(height/40)"}, |
| 123 | + "domain": false, |
| 124 | + "labels": false, |
| 125 | + "aria": false, |
| 126 | + "maxExtent": 0, |
| 127 | + "minExtent": 0, |
| 128 | + "ticks": false, |
| 129 | + "zindex": 0 |
| 130 | + }, |
| 131 | + { |
| 132 | + "scale": "x", |
| 133 | + "orient": "bottom", |
| 134 | + "grid": false, |
| 135 | + "title": "a", |
| 136 | + "labelAngle": 0, |
| 137 | + "labelBaseline": "top", |
| 138 | + "zindex": 0 |
| 139 | + }, |
| 140 | + { |
| 141 | + "scale": "y", |
| 142 | + "orient": "left", |
| 143 | + "grid": false, |
| 144 | + "title": "b", |
| 145 | + "labelOverlap": true, |
| 146 | + "tickCount": {"signal": "ceil(height/40)"}, |
| 147 | + "zindex": 0 |
| 148 | + } |
| 149 | + ] |
| 150 | +} |
0 commit comments