block by ThomasG77 478a6aac8a93904f1894f635b51e10fd

vega for time series chart with shaded blocks

Full Screen

vega stocks spec forked to add shaded blocks

Often time series charts can be enhanced by shaded blocks to highlight events or periods as shown by this example which uses the R package xtsExtra. Vega specs already allow this functionality, and it is fairly easy to implement.

I added an events object to the JSON data array.

      "name": "events",
      "values": [
        {"name":"Test1", "start":"Apr 2000", "end":"Mar 2003"},
        {"name":"Test2", "start":"Oct 2007", "end":"Mar 2009"}

and then a set of marks to do the shading. I was glad to see that vega allows both x2 and width for the rect mark, since I am still unsure how to do calculations with a spec to calculate width.

  "marks": [
      "type" : "rect",
      "from" : { "data": "events" },
      "properties": {
        "enter": {
          "x": {"scale": "x", "field": "data.start"},
          "x2": {"scale": "x", "field": "data.end"},
          "y": {"value": 0},
          "y2": {"scale" : "y", "value" : 0 },
          "fill": {"value": "#888"},
          "opacity": {"value" : 0.5}