vega / vl-convert

Utilities for converting Vega-Lite specs from the command line and Python
BSD 3-Clause "New" or "Revised" License
96 stars 12 forks source link

Error if title fontWeight is a number #46

Closed binste closed 1 year ago

binste commented 1 year ago

fontWeight of a view title can be either a string such as "bold" or a number such as 600, see the VL docs. However, when using a number, vl-convert gives an error about "expected a string". It works for e.g. "bold". I haven't tested other titles or texts, maybe they have the same issue.

Example spec which fails:

import vl_convert as vlc

vl_spec = """{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "title": "Some title",
  "config": {"title": {"fontWeight": 600}},
  "data": {
    "values": [
      {"a": "A", "b": 28}, {"a": "B", "b": 55}, {"a": "C", "b": 43},
      {"a": "D", "b": 91}, {"a": "E", "b": 81}, {"a": "F", "b": 53},
      {"a": "G", "b": 19}, {"a": "H", "b": 87}, {"a": "I", "b": 52}
    ]
  },
  "mark": "bar",
  "encoding": {
    "x": {"field": "a", "type": "nominal", "axis": {"labelAngle": 0}},
    "y": {"field": "b", "type": "quantitative"}
  }
}"""
svg_str = vlc.vegalite_to_svg(vl_spec)

Error:

ERROR Error: Failed to deserialize text info: invalid type: integer `600`, expected a string at line 2 column 15
    at Object.sg.textMetrics.width (<anon>:14:30)
    at Be (https://cdn.skypack.dev/-/vega-scenegraph@v4.9.4-Rfd7OGmaS9T7w10Fz4Yx/dist=es2020,mode=imports,min/optimized/vega-scenegraph.js:1:29874)
    at $ (https://cdn.skypack.dev/-/vega-view-transforms@v4.5.8-jtX42Ax7WNjhu4BmY9kH/dist=es2020,mode=imports,min/optimized/vega-view-transforms.js:1:1587)
    at https://cdn.skypack.dev/-/vega-view-transforms@v4.5.8-jtX42Ax7WNjhu4BmY9kH/dist=es2020,mode=imports,min/optimized/vega-view-transforms.js:1:1277
    at Array.forEach (<anonymous>)
    at gt.transform (https://cdn.skypack.dev/-/vega-view-transforms@v4.5.8-jtX42Ax7WNjhu4BmY9kH/dist=es2020,mode=imports,min/optimized/vega-view-transforms.js:1:1258)
    at gt.evaluate (https://cdn.skypack.dev/-/vega-dataflow@v5.7.4-DrCzG6Luqf74SfPN5Hxw/dist=es2020,mode=imports,min/optimized/vega-dataflow.js:1:15456)
    at gt.run (https://cdn.skypack.dev/-/vega-dataflow@v5.7.4-DrCzG6Luqf74SfPN5Hxw/dist=es2020,mode=imports,min/optimized/vega-dataflow.js:1:15313)
    at ne.Jt [as evaluate] (https://cdn.skypack.dev/-/vega-dataflow@v5.7.4-DrCzG6Luqf74SfPN5Hxw/dist=es2020,mode=imports,min/optimized/vega-dataflow.js:1:12100)
    at ne.evaluate (https://cdn.skypack.dev/-/vega-view@v5.11.0-qj2ShFtxO2P3GWqTy2DZ/dist=es2020,mode=imports,min/optimized/vega-view.js:2:1621)
jonmmease commented 1 year ago

Thanks for the helpful report @binste.

Looks like weight here needs to accept a number:

https://github.com/vega/vl-convert/blob/c694065fe4e222049dbc5d56a1045e9026870bc3/vl-convert-rs/src/text.rs#L89

jonmmease commented 1 year ago

Fix in progress in https://github.com/vega/vl-convert/pull/48

jonmmease commented 1 year ago

@binste is this blocking you or are you able to work around it for the time being? It's now fixed on main, but I'm debating whether to publish a fix immediately or just wait until Vega-Lite 5.7 is released (I'll definitely do a release after Vega-Lite 5.7 and before Altair 5).

binste commented 1 year ago

Thank you for asking and working on it so quickly! I switched to fontWeight "bold" which works for the use case I had so it's not blocking me. Releasing the fix together with support for VL 5.7 sounds good.

jonmmease commented 1 year ago

Fix released in 0.8.0