{ "$schema": "https://vega.github.io/schema/vega/v5.json", "background": "white", "padding": 5, "height": 200, "style": "cell", "data": [ {"name": "brush_store"}, { "name": "source_0", "url": "data/seattle-weather.csv", "format": {"type": "csv", "parse": {"date": "date"}, "delimiter": ","}, "transform": [ { "field": "date", "type": "timeunit", "units": ["month"], "as": ["month_date", "month_date_end"] } ] }, { "name": "data_0", "source": "source_0", "transform": [ { "type": "filter", "expr": "!length(data(\"brush_store\")) || vlSelectionTest(\"brush_store\", datum)" }, { "type": "aggregate", "groupby": [], "ops": ["mean"], "fields": ["precipitation"], "as": ["mean_precipitation"] }, { "type": "filter", "expr": "isValid(datum[\"mean_precipitation\"]) && isFinite(+datum[\"mean_precipitation\"])" } ] }, { "name": "data_1", "source": "source_0", "transform": [ { "type": "aggregate", "groupby": ["month_date"], "ops": ["mean"], "fields": ["precipitation"], "as": ["mean_precipitation"] }, { "type": "filter", "expr": "isValid(datum[\"mean_precipitation\"]) && isFinite(+datum[\"mean_precipitation\"])" } ] } ], "signals": [ {"name": "x_step", "value": 20}, { "name": "width", "update": "bandspace(domain('x').length, 0.1, 0.05) * x_step" }, { "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": "[0, 0]"}, { "events": [{"source": "view", "type": "dblclick"}], "update": "[0, 0]" }, { "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)" } ] }, { "name": "brush_month_date", "on": [ { "events": {"signal": "brush_x"}, "update": "brush_x[0] === brush_x[1] ? null : invert(\"x\", brush_x)" } ] }, { "name": "brush_scale_trigger", "value": {}, "on": [ { "events": [{"scale": "x"}], "update": "(!isArray(brush_month_date) || (invert(\"x\", brush_x)[0] === brush_month_date[0] && invert(\"x\", brush_x)[1] === brush_month_date[1])) ? brush_scale_trigger : {}" } ] }, { "name": "brush_tuple", "on": [ { "events": [{"signal": "brush_month_date"}], "update": "brush_month_date ? {unit: \"layer_0\", fields: brush_tuple_fields, values: [brush_month_date]} : null" } ] }, { "name": "brush_tuple_fields", "value": [{"field": "month_date", "channel": "x", "type": "E"}] }, { "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)}" } ] }, { "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 === \"layer_0\"", "signal": "brush_x[0]" }, {"value": 0} ], "y": [ { "test": "data(\"brush_store\").length && data(\"brush_store\")[0].unit === \"layer_0\"", "value": 0 }, {"value": 0} ], "x2": [ { "test": "data(\"brush_store\").length && data(\"brush_store\")[0].unit === \"layer_0\"", "signal": "brush_x[1]" }, {"value": 0} ], "y2": [ { "test": "data(\"brush_store\").length && data(\"brush_store\")[0].unit === \"layer_0\"", "field": {"group": "height"} }, {"value": 0} ] } } }, { "name": "layer_0_marks", "type": "rect", "style": ["bar"], "interactive": true, "from": {"data": "data_1"}, "encode": { "update": { "fill": {"value": "#4c78a8"}, "opacity": [ { "test": "!length(data(\"brush_store\")) || vlSelectionTest(\"brush_store\", datum)", "value": 1 }, {"value": 0.7} ], "ariaRoleDescription": {"value": "bar"}, "description": { "signal": "\"date (month): \" + (timeFormat(datum[\"month_date\"], timeUnitSpecifier([\"month\"], {\"year-month\":\"%b %Y \",\"year-month-date\":\"%b %d, %Y \"}))) + \"; Mean of precipitation: \" + (format(datum[\"mean_precipitation\"], \"\"))" }, "x": {"scale": "x", "field": "month_date"}, "width": {"signal": "max(0.25, bandwidth('x'))"}, "y": {"scale": "y", "field": "mean_precipitation"}, "y2": {"scale": "y", "value": 0} } } }, { "name": "layer_1_marks", "type": "rule", "style": ["rule"], "interactive": false, "from": {"data": "data_0"}, "encode": { "update": { "stroke": {"value": "firebrick"}, "description": { "signal": "\"Mean of precipitation: \" + (format(datum[\"mean_precipitation\"], \"\"))" }, "x": {"field": {"group": "width"}}, "x2": {"value": 0}, "y": {"scale": "y", "field": "mean_precipitation"}, "strokeWidth": {"value": 3} } } }, { "name": "brush_brush", "type": "rect", "clip": true, "encode": { "enter": {"fill": {"value": "transparent"}}, "update": { "x": [ { "test": "data(\"brush_store\").length && data(\"brush_store\")[0].unit === \"layer_0\"", "signal": "brush_x[0]" }, {"value": 0} ], "y": [ { "test": "data(\"brush_store\").length && data(\"brush_store\")[0].unit === \"layer_0\"", "value": 0 }, {"value": 0} ], "x2": [ { "test": "data(\"brush_store\").length && data(\"brush_store\")[0].unit === \"layer_0\"", "signal": "brush_x[1]" }, {"value": 0} ], "y2": [ { "test": "data(\"brush_store\").length && data(\"brush_store\")[0].unit === \"layer_0\"", "field": {"group": "height"} }, {"value": 0} ], "stroke": [ {"test": "brush_x[0] !== brush_x[1]", "value": "white"}, {"value": null} ] } } } ], "scales": [ { "name": "x", "type": "band", "domain": {"data": "data_1", "field": "month_date", "sort": true}, "range": {"step": {"signal": "x_step"}}, "paddingInner": 0.1, "paddingOuter": 0.05 }, { "name": "y", "type": "linear", "domain": { "fields": [ {"data": "data_1", "field": "mean_precipitation"}, {"data": "data_0", "field": "mean_precipitation"} ] }, "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": "date (month)", "format": { "signal": "timeUnitSpecifier([\"month\"], {\"year-month\":\"%b %Y \",\"year-month-date\":\"%b %d, %Y \"})" }, "formatType": "time", "labelOverlap": true, "zindex": 0 }, { "scale": "y", "orient": "left", "grid": false, "title": "Mean of precipitation", "labelOverlap": true, "tickCount": {"signal": "ceil(height/40)"}, "zindex": 0 } ] }