vega / vega-lite

A concise grammar of interactive graphics, built on Vega.
https://vega.github.io/vega-lite/
BSD 3-Clause "New" or "Revised" License
4.68k stars 611 forks source link

Character escapes in field names lead to a blank chart #4123

Closed jakevdp closed 4 years ago

jakevdp commented 6 years ago

I have been trying to escape special characters in fields following the field documentation. The following should work (I think), but it produces a blank chart when pasted into the vega editor:

{
  "mark": "line",
  "encoding": {
    "x": {"type": "quantitative", "field": "Month"},
    "y": {"type": "quantitative", "field": "P \\[mm\\]"}
  },
  "data": {
    "values": [
      {"Month": 1, "P [mm]": 90.2},
      {"Month": 2, "P [mm]": 100.4},
      {"Month": 3, "P [mm]": 80.1}
    ]
  }
}
domoritz commented 6 years ago

Here is the working Vega spec

{
  "$schema": "https://vega.github.io/schema/vega/v4.json",
  "autosize": "pad",
  "padding": 5,
  "width": 200,
  "height": 200,
  "style": "cell",
  "data": [
    {
      "name": "source_0",
      "values": [
        {"Month": 1, "P [mm]": 90.2},
        {"Month": 2, "P [mm]": 100.4},
        {"Month": 3, "P [mm]": 80.1}
      ]
    },
    {
      "name": "data_0",
      "source": "source_0",
      "transform": [
        {
          "type": "formula",
          "expr": "toNumber(datum[\"Month\"])",
          "as": "Month"
        },
        {
          "type": "formula",
          "expr": "toNumber(datum[\"P [mm]\"])",
          "as": "P [mm]"
        }
      ]
    }
  ],
  "marks": [
    {
      "name": "marks",
      "type": "line",
      "style": ["line"],
      "sort": {"field": "datum[\"Month\"]", "order": "descending"},
      "from": {"data": "data_0"},
      "encode": {
        "update": {
          "stroke": {"value": "#4c78a8"},
          "tooltip": {
            "signal": "{\"Month\": format(datum[\"Month\"], \"\"), \"P \\[mm\\]\": format(datum[\"P [mm]\"], \"\")}"
          },
          "x": {"scale": "x", "field": "Month"},
          "y": {"scale": "y", "field": "P \\[mm\\]"},
          "defined": {
            "signal": "datum[\"Month\"] !== null && !isNaN(datum[\"Month\"]) && datum[\"P [mm]\"] !== null && !isNaN(datum[\"P [mm]\"])"
          }
        }
      }
    }
  ],
  "scales": [
    {
      "name": "x",
      "type": "linear",
      "domain": {"data": "data_0", "field": "Month"},
      "range": [0, {"signal": "width"}],
      "nice": true,
      "zero": false
    },
    {
      "name": "y",
      "type": "linear",
      "domain": {"data": "data_0", "field": "P \\[mm\\]"},
      "range": [{"signal": "height"}, 0],
      "nice": true,
      "zero": true
    }
  ],
  "axes": [
    {
      "scale": "x",
      "orient": "bottom",
      "grid": false,
      "title": "Month",
      "labelFlush": true,
      "labelOverlap": true,
      "tickCount": {"signal": "ceil(width/40)"},
      "zindex": 1
    },
    {
      "scale": "x",
      "orient": "bottom",
      "gridScale": "y",
      "grid": true,
      "tickCount": {"signal": "ceil(width/40)"},
      "domain": false,
      "labels": false,
      "maxExtent": 0,
      "minExtent": 0,
      "ticks": false,
      "zindex": 0
    },
    {
      "scale": "y",
      "orient": "left",
      "grid": false,
      "title": "P \\[mm\\]",
      "labelOverlap": true,
      "tickCount": {"signal": "ceil(height/40)"},
      "zindex": 1
    },
    {
      "scale": "y",
      "orient": "left",
      "gridScale": "x",
      "grid": true,
      "tickCount": {"signal": "ceil(height/40)"},
      "domain": false,
      "labels": false,
      "maxExtent": 0,
      "minExtent": 0,
      "ticks": false,
      "zindex": 0
    }
  ],
  "config": {"axisY": {"minExtent": 30}}
}