|
| 1 | +{ |
| 2 | + "$schema": "https://vega.github.io/schema/vega/v6.json", |
| 3 | + "description": "Gapminder animated over time, dwelling on 1965 and driven by a play/pause checkbox.", |
| 4 | + "background": "white", |
| 5 | + "padding": 5, |
| 6 | + "width": 300, |
| 7 | + "height": 300, |
| 8 | + "style": "cell", |
| 9 | + "data": [ |
| 10 | + {"name": "animation_frame_store"}, |
| 11 | + { |
| 12 | + "name": "source_0", |
| 13 | + "url": "data/gapminder.json", |
| 14 | + "format": {"type": "json"}, |
| 15 | + "transform": [ |
| 16 | + { |
| 17 | + "type": "filter", |
| 18 | + "expr": "isValid(datum[\"fertility\"]) && isFinite(+datum[\"fertility\"]) && isValid(datum[\"life_expect\"]) && isFinite(+datum[\"life_expect\"])" |
| 19 | + } |
| 20 | + ] |
| 21 | + }, |
| 22 | + { |
| 23 | + "name": "animation_frame_pause_store", |
| 24 | + "values": [{"value": 1965, "duration": 2000}], |
| 25 | + "transform": [{"type": "filter", "expr": "datum.value === anim_value"}] |
| 26 | + }, |
| 27 | + { |
| 28 | + "name": "source_0_curr", |
| 29 | + "source": "source_0", |
| 30 | + "transform": [ |
| 31 | + { |
| 32 | + "type": "filter", |
| 33 | + "expr": "!length(data(\"animation_frame_store\")) || vlSelectionTest(\"animation_frame_store\", datum)" |
| 34 | + } |
| 35 | + ] |
| 36 | + } |
| 37 | + ], |
| 38 | + "signals": [ |
| 39 | + { |
| 40 | + "name": "unit", |
| 41 | + "value": {}, |
| 42 | + "on": [ |
| 43 | + {"events": "pointermove", "update": "isTuple(group()) ? group() : unit"} |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "name": "animation_frame", |
| 48 | + "update": "vlSelectionResolve(\"animation_frame_store\", \"union\", true, true)" |
| 49 | + }, |
| 50 | + { |
| 51 | + "name": "anim_clock", |
| 52 | + "init": "0", |
| 53 | + "on": [ |
| 54 | + { |
| 55 | + "events": {"type": "timer", "throttle": 16.666666666666668}, |
| 56 | + "update": "playing && animation_frame_pause_playing ? (anim_clock + (now() - last_tick_at) > max_range_extent ? 0 : anim_clock + (now() - last_tick_at)) : anim_clock" |
| 57 | + } |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "name": "last_tick_at", |
| 62 | + "init": "now()", |
| 63 | + "on": [ |
| 64 | + { |
| 65 | + "events": [ |
| 66 | + {"signal": "anim_clock"}, |
| 67 | + {"signal": "playing"}, |
| 68 | + {"signal": "animation_frame_pause_playing"} |
| 69 | + ], |
| 70 | + "update": "now()" |
| 71 | + } |
| 72 | + ] |
| 73 | + }, |
| 74 | + { |
| 75 | + "name": "animation_frame_pause_duration", |
| 76 | + "update": "length(data(\"animation_frame_pause_store\")) ? data(\"animation_frame_pause_store\")[0].duration : null" |
| 77 | + }, |
| 78 | + { |
| 79 | + "name": "animation_frame_pause_since", |
| 80 | + "init": "now()", |
| 81 | + "on": [ |
| 82 | + { |
| 83 | + "events": [{"signal": "animation_frame_pause_duration"}], |
| 84 | + "update": "now()" |
| 85 | + } |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "name": "animation_frame_pause_playing", |
| 90 | + "init": "true", |
| 91 | + "on": [ |
| 92 | + { |
| 93 | + "events": {"type": "timer", "throttle": 16.666666666666668}, |
| 94 | + "update": "animation_frame_pause_duration ? (now() - animation_frame_pause_since > animation_frame_pause_duration) : true" |
| 95 | + } |
| 96 | + ] |
| 97 | + }, |
| 98 | + {"name": "playing", "value": true, "bind": {"input": "checkbox"}}, |
| 99 | + {"name": "eased_anim_clock", "update": "anim_clock"}, |
| 100 | + {"name": "animation_frame_domain", "init": "domain('time')"}, |
| 101 | + {"name": "min_extent", "init": "extent(animation_frame_domain)[0]"}, |
| 102 | + {"name": "max_range_extent", "init": "extent(range('time'))[1]"}, |
| 103 | + {"name": "anim_value", "update": "invert('time', eased_anim_clock)"}, |
| 104 | + { |
| 105 | + "name": "animation_frame_tuple", |
| 106 | + "update": "{unit: \"\", fields: animation_frame_tuple_fields, values: [anim_value ? anim_value : min_extent]}" |
| 107 | + }, |
| 108 | + { |
| 109 | + "name": "animation_frame_tuple_fields", |
| 110 | + "value": [{"type": "E", "field": "year"}] |
| 111 | + }, |
| 112 | + { |
| 113 | + "name": "animation_frame_modify", |
| 114 | + "update": "modify(\"animation_frame_store\", animation_frame_tuple, true)" |
| 115 | + } |
| 116 | + ], |
| 117 | + "marks": [ |
| 118 | + { |
| 119 | + "name": "marks", |
| 120 | + "type": "symbol", |
| 121 | + "style": ["point"], |
| 122 | + "interactive": true, |
| 123 | + "from": {"data": "source_0_curr"}, |
| 124 | + "encode": { |
| 125 | + "update": { |
| 126 | + "opacity": {"value": 0.7}, |
| 127 | + "cursor": {"value": "pointer"}, |
| 128 | + "fill": {"value": "transparent"}, |
| 129 | + "stroke": {"scale": "color", "field": "cluster"}, |
| 130 | + "ariaRoleDescription": {"value": "point"}, |
| 131 | + "description": { |
| 132 | + "signal": "\"fertility: \" + (format(datum[\"fertility\"], \"\")) + \"; life_expect: \" + (format(datum[\"life_expect\"], \"\")) + \"; cluster: \" + (isValid(datum[\"cluster\"]) ? isArray(datum[\"cluster\"]) ? join(datum[\"cluster\"], ' ') : datum[\"cluster\"] : \"\"+datum[\"cluster\"]) + \"; year: \" + (isValid(datum[\"year\"]) ? isArray(datum[\"year\"]) ? join(datum[\"year\"], ' ') : datum[\"year\"] : \"\"+datum[\"year\"])" |
| 133 | + }, |
| 134 | + "x": {"scale": "x", "field": "fertility"}, |
| 135 | + "y": {"scale": "y", "field": "life_expect"} |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + ], |
| 140 | + "scales": [ |
| 141 | + { |
| 142 | + "name": "x", |
| 143 | + "type": "linear", |
| 144 | + "domain": {"data": "source_0", "field": "fertility"}, |
| 145 | + "range": [0, {"signal": "width"}], |
| 146 | + "nice": true, |
| 147 | + "zero": true |
| 148 | + }, |
| 149 | + { |
| 150 | + "name": "y", |
| 151 | + "type": "linear", |
| 152 | + "domain": {"data": "source_0", "field": "life_expect"}, |
| 153 | + "range": [{"signal": "height"}, 0], |
| 154 | + "nice": true, |
| 155 | + "zero": true |
| 156 | + }, |
| 157 | + { |
| 158 | + "name": "color", |
| 159 | + "type": "ordinal", |
| 160 | + "domain": {"data": "source_0", "field": "cluster", "sort": true}, |
| 161 | + "range": "category" |
| 162 | + }, |
| 163 | + { |
| 164 | + "name": "time", |
| 165 | + "type": "band", |
| 166 | + "domain": {"data": "source_0", "field": "year", "sort": true}, |
| 167 | + "range": {"step": 500} |
| 168 | + } |
| 169 | + ], |
| 170 | + "axes": [ |
| 171 | + { |
| 172 | + "scale": "x", |
| 173 | + "orient": "bottom", |
| 174 | + "gridScale": "y", |
| 175 | + "grid": true, |
| 176 | + "tickCount": {"signal": "ceil(width/40)"}, |
| 177 | + "domain": false, |
| 178 | + "labels": false, |
| 179 | + "aria": false, |
| 180 | + "maxExtent": 0, |
| 181 | + "minExtent": 0, |
| 182 | + "ticks": false, |
| 183 | + "zindex": 0 |
| 184 | + }, |
| 185 | + { |
| 186 | + "scale": "y", |
| 187 | + "orient": "left", |
| 188 | + "gridScale": "x", |
| 189 | + "grid": true, |
| 190 | + "tickCount": {"signal": "ceil(height/40)"}, |
| 191 | + "domain": false, |
| 192 | + "labels": false, |
| 193 | + "aria": false, |
| 194 | + "maxExtent": 0, |
| 195 | + "minExtent": 0, |
| 196 | + "ticks": false, |
| 197 | + "zindex": 0 |
| 198 | + }, |
| 199 | + { |
| 200 | + "scale": "x", |
| 201 | + "orient": "bottom", |
| 202 | + "grid": false, |
| 203 | + "title": "fertility", |
| 204 | + "labelFlush": true, |
| 205 | + "labelOverlap": true, |
| 206 | + "tickCount": {"signal": "ceil(width/40)"}, |
| 207 | + "zindex": 0 |
| 208 | + }, |
| 209 | + { |
| 210 | + "scale": "y", |
| 211 | + "orient": "left", |
| 212 | + "grid": false, |
| 213 | + "title": "life_expect", |
| 214 | + "labelOverlap": true, |
| 215 | + "tickCount": {"signal": "ceil(height/40)"}, |
| 216 | + "zindex": 0 |
| 217 | + } |
| 218 | + ], |
| 219 | + "legends": [ |
| 220 | + { |
| 221 | + "stroke": "color", |
| 222 | + "symbolType": "circle", |
| 223 | + "title": "cluster", |
| 224 | + "encode": { |
| 225 | + "symbols": { |
| 226 | + "update": { |
| 227 | + "fill": {"value": "transparent"}, |
| 228 | + "opacity": {"value": 0.7} |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | + ] |
| 234 | +} |
0 commit comments