{ "$schema": "https://vega.github.io/schema/vega/v5.json", "background": "white", "padding": 5, "height": 200, "style": "cell", "data": [ { "name": "source_0", "url": "data/cars.json", "format": {"type": "json", "parse": {"Year": "date"}}, "transform": [ { "field": "Year", "type": "timeunit", "units": ["year"], "as": ["year_Year", "year_Year_end"] }, { "type": "aggregate", "groupby": ["Cylinders", "Origin", "year_Year"], "ops": ["sum"], "fields": ["Weight_in_lbs"], "as": ["sum_Weight_in_lbs"] }, { "type": "stack", "groupby": ["Cylinders", "Origin"], "field": "sum_Weight_in_lbs", "sort": {"field": ["year_Year"], "order": ["descending"]}, "as": ["sum_Weight_in_lbs_start", "sum_Weight_in_lbs_end"], "offset": "zero" }, { "type": "filter", "expr": "isValid(datum[\"sum_Weight_in_lbs\"]) && isFinite(+datum[\"sum_Weight_in_lbs\"])" } ] } ], "signals": [ { "name": "x_step", "update": "20 * bandspace(domain('xOffset').length, 0, 0) / (1-0.2)" }, { "name": "width", "update": "bandspace(domain('x').length, 0.2, 0.2) * x_step" } ], "marks": [ { "name": "marks", "type": "rect", "style": ["bar"], "from": {"data": "source_0"}, "encode": { "update": { "fill": {"scale": "color", "field": "year_Year"}, "ariaRoleDescription": {"value": "bar"}, "description": { "signal": "\"Cylinders: \" + (isValid(datum[\"Cylinders\"]) ? datum[\"Cylinders\"] : \"\"+datum[\"Cylinders\"]) + \"; Sum of Weight_in_lbs: \" + (format(datum[\"sum_Weight_in_lbs\"], \"\")) + \"; Origin: \" + (isValid(datum[\"Origin\"]) ? datum[\"Origin\"] : \"\"+datum[\"Origin\"]) + \"; Year (year): \" + (timeFormat(datum[\"year_Year\"], timeUnitSpecifier([\"year\"], {\"year-month\":\"%b %Y \",\"year-month-date\":\"%b %d, %Y \"})))" }, "x": { "scale": "x", "field": "Cylinders", "offset": {"scale": "xOffset", "field": "Origin"} }, "width": {"scale": "xOffset", "band": 1}, "y": {"scale": "y", "field": "sum_Weight_in_lbs_end"}, "y2": {"scale": "y", "field": "sum_Weight_in_lbs_start"} } } } ], "scales": [ { "name": "x", "type": "band", "domain": {"data": "source_0", "field": "Cylinders", "sort": true}, "range": {"step": {"signal": "x_step"}}, "paddingInner": 0.2, "paddingOuter": 0.2 }, { "name": "y", "type": "linear", "domain": { "data": "source_0", "fields": ["sum_Weight_in_lbs_start", "sum_Weight_in_lbs_end"] }, "range": [{"signal": "height"}, 0], "nice": true, "zero": true }, { "name": "xOffset", "type": "band", "domain": {"data": "source_0", "field": "Origin", "sort": true}, "range": {"step": 20} }, { "name": "color", "type": "ordinal", "domain": {"data": "source_0", "field": "year_Year", "sort": true}, "range": {"scheme": "category20"} } ], "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": "Cylinders", "labelAlign": "right", "labelAngle": 270, "labelBaseline": "middle", "zindex": 0 }, { "scale": "y", "orient": "left", "grid": false, "title": "Sum of Weight_in_lbs", "labelOverlap": true, "tickCount": {"signal": "ceil(height/40)"}, "zindex": 0 } ], "legends": [ { "fill": "color", "format": { "signal": "timeUnitSpecifier([\"year\"], {\"year-month\":\"%b %Y \",\"year-month-date\":\"%b %d, %Y \"})" }, "formatType": "time", "symbolType": "square", "title": "Year (year)" } ] }