Module

x/vega_lite/examples/compiled/selection_translate_brush_drag.vg.json

A concise grammar of interactive graphics, built on Vega.
Latest
File
{ "$schema": "https://vega.github.io/schema/vega/v5.json", "background": "white", "padding": 5, "width": 200, "height": 200, "style": "cell", "data": [ {"name": "brush_store"}, { "name": "source_0", "url": "data/cars.json", "format": {"type": "json"}, "transform": [ { "type": "filter", "expr": "isValid(datum[\"Horsepower\"]) && isFinite(+datum[\"Horsepower\"]) && isValid(datum[\"Miles_per_Gallon\"]) && isFinite(+datum[\"Miles_per_Gallon\"]) && isValid(datum[\"Cylinders\"]) && isFinite(+datum[\"Cylinders\"])" } ] } ], "signals": [ { "name": "unit", "value": {}, "on": [ {"events": "mousemove", "update": "isTuple(group()) ? group() : unit"} ] }, { "name": "brush", "update": "vlSelectionResolve(\"brush_store\", \"union\")" }, { "name": "brush_x", "value": [], "on": [ { "events": { "source": "scope", "type": "mousedown", "filter": [ "!event.item || event.item.mark.name !== \"brush_brush\"" ] }, "update": "[x(unit), x(unit)]" }, { "events": { "source": "window", "type": "mousemove", "consume": true, "between": [ { "source": "scope", "type": "mousedown", "filter": [ "!event.item || event.item.mark.name !== \"brush_brush\"" ] }, {"source": "window", "type": "mouseup"} ] }, "update": "[brush_x[0], clamp(x(unit), 0, width)]" }, { "events": {"signal": "brush_scale_trigger"}, "update": "[scale(\"x\", brush_Horsepower[0]), scale(\"x\", brush_Horsepower[1])]" }, { "events": {"signal": "brush_translate_delta"}, "update": "clampRange(panLinear(brush_translate_anchor.extent_x, brush_translate_delta.x / span(brush_translate_anchor.extent_x)), 0, width)" }, { "events": {"signal": "brush_zoom_delta"}, "update": "clampRange(zoomLinear(brush_x, brush_zoom_anchor.x, brush_zoom_delta), 0, width)" }, { "events": [{"source": "scope", "type": "dblclick"}], "update": "[0, 0]" } ] }, { "name": "brush_Horsepower", "on": [ { "events": {"signal": "brush_x"}, "update": "brush_x[0] === brush_x[1] ? null : invert(\"x\", brush_x)" } ] }, { "name": "brush_y", "value": [], "on": [ { "events": { "source": "scope", "type": "mousedown", "filter": [ "!event.item || event.item.mark.name !== \"brush_brush\"" ] }, "update": "[y(unit), y(unit)]" }, { "events": { "source": "window", "type": "mousemove", "consume": true, "between": [ { "source": "scope", "type": "mousedown", "filter": [ "!event.item || event.item.mark.name !== \"brush_brush\"" ] }, {"source": "window", "type": "mouseup"} ] }, "update": "[brush_y[0], clamp(y(unit), 0, height)]" }, { "events": {"signal": "brush_scale_trigger"}, "update": "[scale(\"y\", brush_Miles_per_Gallon[0]), scale(\"y\", brush_Miles_per_Gallon[1])]" }, { "events": {"signal": "brush_translate_delta"}, "update": "clampRange(panLinear(brush_translate_anchor.extent_y, brush_translate_delta.y / span(brush_translate_anchor.extent_y)), 0, height)" }, { "events": {"signal": "brush_zoom_delta"}, "update": "clampRange(zoomLinear(brush_y, brush_zoom_anchor.y, brush_zoom_delta), 0, height)" }, { "events": [{"source": "scope", "type": "dblclick"}], "update": "[0, 0]" } ] }, { "name": "brush_Miles_per_Gallon", "on": [ { "events": {"signal": "brush_y"}, "update": "brush_y[0] === brush_y[1] ? null : invert(\"y\", brush_y)" } ] }, { "name": "brush_scale_trigger", "value": {}, "on": [ { "events": [{"scale": "x"}, {"scale": "y"}], "update": "(!isArray(brush_Horsepower) || (+invert(\"x\", brush_x)[0] === +brush_Horsepower[0] && +invert(\"x\", brush_x)[1] === +brush_Horsepower[1])) && (!isArray(brush_Miles_per_Gallon) || (+invert(\"y\", brush_y)[0] === +brush_Miles_per_Gallon[0] && +invert(\"y\", brush_y)[1] === +brush_Miles_per_Gallon[1])) ? brush_scale_trigger : {}" } ] }, { "name": "brush_tuple", "on": [ { "events": [{"signal": "brush_Horsepower || brush_Miles_per_Gallon"}], "update": "brush_Horsepower && brush_Miles_per_Gallon ? {unit: \"\", fields: brush_tuple_fields, values: [brush_Horsepower,brush_Miles_per_Gallon]} : null" } ] }, { "name": "brush_tuple_fields", "value": [ {"field": "Horsepower", "channel": "x", "type": "R"}, {"field": "Miles_per_Gallon", "channel": "y", "type": "R"} ] }, { "name": "brush_translate_anchor", "value": {}, "on": [ { "events": [ {"source": "scope", "type": "mousedown", "markname": "brush_brush"} ], "update": "{x: x(unit), y: y(unit), extent_x: slice(brush_x), extent_y: slice(brush_y)}" } ] }, { "name": "brush_translate_delta", "value": {}, "on": [ { "events": [ { "source": "window", "type": "mousemove", "consume": true, "between": [ { "source": "scope", "type": "mousedown", "markname": "brush_brush" }, {"source": "window", "type": "mouseup"} ] } ], "update": "{x: brush_translate_anchor.x - x(unit), y: brush_translate_anchor.y - y(unit)}" } ] }, { "name": "brush_zoom_anchor", "on": [ { "events": [ { "source": "scope", "type": "wheel", "consume": true, "markname": "brush_brush" } ], "update": "{x: x(unit), y: y(unit)}" } ] }, { "name": "brush_zoom_delta", "on": [ { "events": [ { "source": "scope", "type": "wheel", "consume": true, "markname": "brush_brush" } ], "force": true, "update": "pow(1.001, event.deltaY * pow(16, event.deltaMode))" } ] }, { "name": "brush_modify", "on": [ { "events": {"signal": "brush_tuple"}, "update": "modify(\"brush_store\", brush_tuple, true)" } ] } ], "marks": [ { "name": "brush_brush_bg", "type": "rect", "clip": true, "encode": { "enter": {"fill": {"value": "#333"}, "fillOpacity": {"value": 0.125}}, "update": { "x": [ { "test": "data(\"brush_store\").length && data(\"brush_store\")[0].unit === \"\"", "signal": "brush_x[0]" }, {"value": 0} ], "y": [ { "test": "data(\"brush_store\").length && data(\"brush_store\")[0].unit === \"\"", "signal": "brush_y[0]" }, {"value": 0} ], "x2": [ { "test": "data(\"brush_store\").length && data(\"brush_store\")[0].unit === \"\"", "signal": "brush_x[1]" }, {"value": 0} ], "y2": [ { "test": "data(\"brush_store\").length && data(\"brush_store\")[0].unit === \"\"", "signal": "brush_y[1]" }, {"value": 0} ] } } }, { "name": "marks", "type": "symbol", "clip": true, "style": ["circle"], "interactive": true, "from": {"data": "source_0"}, "encode": { "update": { "opacity": {"value": 0.7}, "fill": [ { "test": "!(length(data(\"brush_store\"))) || (vlSelectionTest(\"brush_store\", datum))", "scale": "color", "field": "Origin" }, {"value": "grey"} ], "ariaRoleDescription": {"value": "circle"}, "description": { "signal": "\"Horsepower: \" + (format(datum[\"Horsepower\"], \"\")) + \"; Miles_per_Gallon: \" + (format(datum[\"Miles_per_Gallon\"], \"\")) + \"; Cylinders: \" + (format(datum[\"Cylinders\"], \"\")) + \"; Origin: \" + (isValid(datum[\"Origin\"]) ? datum[\"Origin\"] : \"\"+datum[\"Origin\"])" }, "x": [ { "test": "!isValid(datum[\"Horsepower\"]) || !isFinite(+datum[\"Horsepower\"])", "value": 0 }, {"scale": "x", "field": "Horsepower"} ], "y": [ { "test": "!isValid(datum[\"Miles_per_Gallon\"]) || !isFinite(+datum[\"Miles_per_Gallon\"])", "field": {"group": "height"} }, {"scale": "y", "field": "Miles_per_Gallon"} ], "size": {"scale": "size", "field": "Cylinders"}, "shape": {"value": "circle"} } } }, { "name": "brush_brush", "type": "rect", "clip": true, "encode": { "enter": {"fill": {"value": "transparent"}}, "update": { "x": [ { "test": "data(\"brush_store\").length && data(\"brush_store\")[0].unit === \"\"", "signal": "brush_x[0]" }, {"value": 0} ], "y": [ { "test": "data(\"brush_store\").length && data(\"brush_store\")[0].unit === \"\"", "signal": "brush_y[0]" }, {"value": 0} ], "x2": [ { "test": "data(\"brush_store\").length && data(\"brush_store\")[0].unit === \"\"", "signal": "brush_x[1]" }, {"value": 0} ], "y2": [ { "test": "data(\"brush_store\").length && data(\"brush_store\")[0].unit === \"\"", "signal": "brush_y[1]" }, {"value": 0} ], "stroke": [ { "test": "brush_x[0] !== brush_x[1] && brush_y[0] !== brush_y[1]", "value": "white" }, {"value": null} ] } } } ], "scales": [ { "name": "x", "type": "linear", "domain": [75, 150], "range": [0, {"signal": "width"}], "nice": true, "zero": false }, { "name": "y", "type": "linear", "domain": [20, 40], "range": [{"signal": "height"}, 0], "nice": true, "zero": false }, { "name": "color", "type": "ordinal", "domain": {"data": "source_0", "field": "Origin", "sort": true}, "range": "category" }, { "name": "size", "type": "linear", "domain": {"data": "source_0", "field": "Cylinders"}, "range": [0, 361], "zero": true } ], "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": "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": "Horsepower", "labelFlush": true, "labelOverlap": true, "tickCount": {"signal": "ceil(width/40)"}, "zindex": 0 }, { "scale": "y", "orient": "left", "grid": false, "title": "Miles_per_Gallon", "labelOverlap": true, "tickCount": {"signal": "ceil(height/40)"}, "zindex": 0 } ], "legends": [ { "fill": "color", "symbolType": "circle", "title": "Origin", "encode": {"symbols": {"update": {"opacity": {"value": 0.7}}}} }, { "size": "size", "symbolType": "circle", "title": "Cylinders", "encode": { "symbols": { "update": { "fill": {"value": "grey"}, "opacity": {"value": 0.7}, "stroke": {"value": "transparent"} } } } } ]}