{ "$schema": "https://vega.github.io/schema/vega/v5.json", "description": "A bar chart with highlighting on hover and selecting on click. (Inspired by Tableau's interaction style.)", "background": "white", "padding": 5, "height": 200, "style": "cell", "data": [ {"name": "highlight_store"}, {"name": "select_store"}, { "name": "source_0", "values": [ {"a": "A", "b": 28}, {"a": "B", "b": 55}, {"a": "C", "b": 43}, {"a": "D", "b": 91}, {"a": "E", "b": 81}, {"a": "F", "b": 53}, {"a": "G", "b": 19}, {"a": "H", "b": 87}, {"a": "I", "b": 52} ] }, { "name": "data_0", "source": "source_0", "transform": [ {"type": "identifier", "as": "_vgsid_"}, { "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\"])" } ] } ], "signals": [ {"name": "x_step", "value": 20}, { "name": "width", "update": "bandspace(domain('x').length, 0.2, 0.1) * x_step" }, { "name": "unit", "value": {}, "on": [ {"events": "mousemove", "update": "isTuple(group()) ? group() : unit"} ] }, { "name": "highlight", "update": "vlSelectionResolve(\"highlight_store\", \"union\", true, true)" }, { "name": "select", "update": "vlSelectionResolve(\"select_store\", \"union\", true, true)" }, { "name": "highlight_tuple", "on": [ { "events": [{"source": "scope", "type": "mouseover"}], "update": "datum && item().mark.marktype !== 'group' ? {unit: \"\", fields: highlight_tuple_fields, values: [(item().isVoronoi ? datum.datum : datum)[\"_vgsid_\"]]} : null", "force": true }, {"events": [{"source": "view", "type": "dblclick"}], "update": "null"} ] }, { "name": "highlight_tuple_fields", "value": [{"type": "E", "field": "_vgsid_"}] }, { "name": "highlight_toggle", "value": false, "on": [ { "events": [{"source": "scope", "type": "mouseover"}], "update": "event.shiftKey" }, {"events": [{"source": "view", "type": "dblclick"}], "update": "false"} ] }, { "name": "highlight_modify", "on": [ { "events": {"signal": "highlight_tuple"}, "update": "modify(\"highlight_store\", highlight_toggle ? null : highlight_tuple, highlight_toggle ? null : true, highlight_toggle ? highlight_tuple : null)" } ] }, { "name": "select_tuple", "on": [ { "events": [{"source": "scope", "type": "click"}], "update": "datum && item().mark.marktype !== 'group' ? {unit: \"\", fields: select_tuple_fields, values: [(item().isVoronoi ? datum.datum : datum)[\"_vgsid_\"]]} : null", "force": true }, {"events": [{"source": "view", "type": "dblclick"}], "update": "null"} ] }, { "name": "select_tuple_fields", "value": [{"type": "E", "field": "_vgsid_"}] }, { "name": "select_toggle", "value": false, "on": [ { "events": [{"source": "scope", "type": "click"}], "update": "event.shiftKey" }, {"events": [{"source": "view", "type": "dblclick"}], "update": "false"} ] }, { "name": "select_modify", "on": [ { "events": {"signal": "select_tuple"}, "update": "modify(\"select_store\", select_toggle ? null : select_tuple, select_toggle ? null : true, select_toggle ? select_tuple : null)" } ] } ], "marks": [ { "name": "marks", "type": "rect", "style": ["bar"], "interactive": true, "from": {"data": "data_0"}, "encode": { "update": { "fill": {"value": "#4C78A8"}, "stroke": {"value": "black"}, "cursor": {"value": "pointer"}, "fillOpacity": [ { "test": "!length(data(\"select_store\")) || vlSelectionTest(\"select_store\", datum)", "value": 1 }, {"value": 0.3} ], "strokeWidth": [ { "test": "length(data(\"select_store\")) && vlSelectionTest(\"select_store\", datum)", "value": 2 }, { "test": "length(data(\"highlight_store\")) && vlSelectionTest(\"highlight_store\", datum)", "value": 1 }, {"value": 0} ], "ariaRoleDescription": {"value": "bar"}, "description": { "signal": "\"a: \" + (isValid(datum[\"a\"]) ? datum[\"a\"] : \"\"+datum[\"a\"]) + \"; b: \" + (format(datum[\"b\"], \"\"))" }, "x": {"scale": "x", "field": "a"}, "width": {"scale": "x", "band": 1}, "y": {"scale": "y", "field": "b_end"}, "y2": {"scale": "y", "field": "b_start"} } } } ], "scales": [ { "name": "x", "type": "band", "domain": {"data": "data_0", "field": "a", "sort": true}, "range": {"step": {"signal": "x_step"}}, "paddingInner": 0.2, "paddingOuter": 0.1 }, { "name": "y", "type": "linear", "domain": {"data": "data_0", "fields": ["b_start", "b_end"]}, "range": [{"signal": "height"}, 0], "nice": true, "zero": true } ], "axes": [ { "scale": "y", "orient": "left", "gridScale": "x", "grid": true, "tickCount": {"signal": "ceil(height/40)"}, "domain": false, "labels": false, "aria": false, "maxExtent": 0, "minExtent": 0, "ticks": false, "zindex": 0 }, { "scale": "x", "orient": "bottom", "grid": false, "title": "a", "labelAlign": "right", "labelAngle": 270, "labelBaseline": "middle", "zindex": 0 }, { "scale": "y", "orient": "left", "grid": false, "title": "b", "labelOverlap": true, "tickCount": {"signal": "ceil(height/40)"}, "zindex": 0 } ] }