|
| 1 | +{ |
| 2 | + "$schema": "https://vega.github.io/schema/vega/v5.json", |
| 3 | + "background": "white", |
| 4 | + "padding": 5, |
| 5 | + "width": 400, |
| 6 | + "height": 250, |
| 7 | + "style": "cell", |
| 8 | + "data": [ |
| 9 | + { |
| 10 | + "name": "source_0", |
| 11 | + "url": "data/cars.json", |
| 12 | + "format": {"type": "json", "parse": {"Year": "date"}}, |
| 13 | + "transform": [ |
| 14 | + { |
| 15 | + "type": "filter", |
| 16 | + "expr": "inrange(time(datetime(year(datum[\"Year\"]), 0, 1, 0, 0, 0, 0)), [time(datetime(1970, 0, 1, 0, 0, 0, 0)), time(datetime(1971, 0, 1, 0, 0, 0, 0))])" |
| 17 | + }, |
| 18 | + { |
| 19 | + "field": "Year", |
| 20 | + "type": "timeunit", |
| 21 | + "units": ["year", "week"], |
| 22 | + "as": ["yearweek_Year", "yearweek_Year_end"] |
| 23 | + }, |
| 24 | + { |
| 25 | + "type": "aggregate", |
| 26 | + "groupby": ["yearweek_Year", "yearweek_Year_end", "Origin"], |
| 27 | + "ops": ["count"], |
| 28 | + "fields": [null], |
| 29 | + "as": ["__count"] |
| 30 | + }, |
| 31 | + { |
| 32 | + "type": "filter", |
| 33 | + "expr": "(isDate(datum[\"yearweek_Year\"]) || (isValid(datum[\"yearweek_Year\"]) && isFinite(+datum[\"yearweek_Year\"])))" |
| 34 | + } |
| 35 | + ] |
| 36 | + } |
| 37 | + ], |
| 38 | + "marks": [ |
| 39 | + { |
| 40 | + "name": "marks", |
| 41 | + "type": "rect", |
| 42 | + "style": ["bar"], |
| 43 | + "from": {"data": "source_0"}, |
| 44 | + "encode": { |
| 45 | + "update": { |
| 46 | + "fill": {"scale": "color", "field": "Origin"}, |
| 47 | + "ariaRoleDescription": {"value": "bar"}, |
| 48 | + "description": { |
| 49 | + "signal": "\"Year (year-week): \" + (timeFormat(datum[\"yearweek_Year\"], timeUnitSpecifier([\"year\",\"week\"], {\"year-month\":\"%b %Y \",\"year-month-date\":\"%b %d, %Y \"}))) + \"; Number of Cars: \" + (format(datum[\"__count\"], \"\")) + \"; Origin: \" + (isValid(datum[\"Origin\"]) ? datum[\"Origin\"] : \"\"+datum[\"Origin\"])" |
| 50 | + }, |
| 51 | + "xc": { |
| 52 | + "scale": "x", |
| 53 | + "field": "yearweek_Year", |
| 54 | + "offset": {"scale": "xOffset", "field": "Origin", "band": 0.5} |
| 55 | + }, |
| 56 | + "width": {"signal": "max(0.25, bandwidth('xOffset'))"}, |
| 57 | + "y": {"scale": "y", "field": "__count"}, |
| 58 | + "y2": {"scale": "y", "value": 0} |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + ], |
| 63 | + "scales": [ |
| 64 | + { |
| 65 | + "name": "x", |
| 66 | + "type": "time", |
| 67 | + "domain": { |
| 68 | + "data": "source_0", |
| 69 | + "fields": ["yearweek_Year", "yearweek_Year_end"] |
| 70 | + }, |
| 71 | + "range": [0, {"signal": "width"}] |
| 72 | + }, |
| 73 | + { |
| 74 | + "name": "y", |
| 75 | + "type": "linear", |
| 76 | + "domain": {"data": "source_0", "field": "__count"}, |
| 77 | + "range": [{"signal": "height"}, 0], |
| 78 | + "nice": true, |
| 79 | + "zero": true |
| 80 | + }, |
| 81 | + { |
| 82 | + "name": "xOffset", |
| 83 | + "type": "band", |
| 84 | + "domain": {"data": "source_0", "field": "Origin", "sort": true}, |
| 85 | + "range": [ |
| 86 | + { |
| 87 | + "signal": "0.1 * (scale('x', datetime(2001, 0, 8, 0, 0, 0, 0)) - scale('x', datetime(2001, 0, 1, 0, 0, 0, 0)))" |
| 88 | + }, |
| 89 | + { |
| 90 | + "signal": "0.9 * (scale('x', datetime(2001, 0, 8, 0, 0, 0, 0)) - scale('x', datetime(2001, 0, 1, 0, 0, 0, 0)))" |
| 91 | + } |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "name": "color", |
| 96 | + "type": "ordinal", |
| 97 | + "domain": {"data": "source_0", "field": "Origin", "sort": true}, |
| 98 | + "range": "category" |
| 99 | + } |
| 100 | + ], |
| 101 | + "axes": [ |
| 102 | + { |
| 103 | + "scale": "x", |
| 104 | + "orient": "bottom", |
| 105 | + "gridScale": "y", |
| 106 | + "grid": true, |
| 107 | + "tickCount": {"signal": "ceil(width/40)"}, |
| 108 | + "tickMinStep": { |
| 109 | + "signal": "datetime(2001, 0, 8, 0, 0, 0, 0) - datetime(2001, 0, 1, 0, 0, 0, 0)" |
| 110 | + }, |
| 111 | + "domain": false, |
| 112 | + "labels": false, |
| 113 | + "aria": false, |
| 114 | + "maxExtent": 0, |
| 115 | + "minExtent": 0, |
| 116 | + "ticks": false, |
| 117 | + "zindex": 0 |
| 118 | + }, |
| 119 | + { |
| 120 | + "scale": "y", |
| 121 | + "orient": "left", |
| 122 | + "gridScale": "x", |
| 123 | + "grid": true, |
| 124 | + "tickCount": {"signal": "ceil(height/40)"}, |
| 125 | + "domain": false, |
| 126 | + "labels": false, |
| 127 | + "aria": false, |
| 128 | + "maxExtent": 0, |
| 129 | + "minExtent": 0, |
| 130 | + "ticks": false, |
| 131 | + "zindex": 0 |
| 132 | + }, |
| 133 | + { |
| 134 | + "scale": "x", |
| 135 | + "orient": "bottom", |
| 136 | + "grid": false, |
| 137 | + "title": "Year (year-week)", |
| 138 | + "format": { |
| 139 | + "signal": "timeUnitSpecifier([\"year\",\"week\"], {\"year-month\":\"%b %Y \",\"year-month-date\":\"%b %d, %Y \"})" |
| 140 | + }, |
| 141 | + "labelFlush": true, |
| 142 | + "labelOverlap": true, |
| 143 | + "tickCount": {"signal": "ceil(width/40)"}, |
| 144 | + "tickMinStep": { |
| 145 | + "signal": "datetime(2001, 0, 8, 0, 0, 0, 0) - datetime(2001, 0, 1, 0, 0, 0, 0)" |
| 146 | + }, |
| 147 | + "zindex": 0 |
| 148 | + }, |
| 149 | + { |
| 150 | + "scale": "y", |
| 151 | + "orient": "left", |
| 152 | + "grid": false, |
| 153 | + "title": "Number of Cars", |
| 154 | + "labelOverlap": true, |
| 155 | + "tickCount": {"signal": "ceil(height/40)"}, |
| 156 | + "zindex": 0 |
| 157 | + } |
| 158 | + ], |
| 159 | + "legends": [{"fill": "color", "symbolType": "square", "title": "Origin"}] |
| 160 | +} |
0 commit comments