Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type when serialize x2 and y2 encodings as json #3793

Open
mauro-dribia opened this issue Feb 6, 2025 · 0 comments
Open

Add type when serialize x2 and y2 encodings as json #3793

mauro-dribia opened this issue Feb 6, 2025 · 0 comments
Labels

Comments

@mauro-dribia
Copy link

What is your suggestion?

I am using altair==5.5.0, vega==5.31.0, vega-embed==6.29.0, and vega-lite==5.23.0 to embed an altair plot into a website. However, I get the following error in the browser console: Error: Invalid field type "undefined". This happens whenever I have x2 or y2 encodings in the plot.

As an example, the needed json that I get with the plot:

import altair as alt
import pandas as pd
rect_data = pd.DataFrame({"x1": [-2], "x2": [-1]})
rect = alt.Chart(rect_data).mark_rect().encode(x="x1:Q", x2="x2:Q")
print(rect.to_json())

is

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.20.1.json",
  "config": {
    "view": {
      "continuousHeight": 300, "continuousWidth": 300
    }
  },
  "data": {
    "name": "data-c59eb5edb94e6fb23b2cddfce3d03317"
  },
  "datasets": {
    "data-c59eb5edb94e6fb23b2cddfce3d03317": [
      {
        "x1": -2, "x2": -1
      }
    ]
  },
  "encoding": {
    "x": {
      "field": "x1", "type": "quantitative"
    },
    "x2": { 
      "field": "x2"
    }
  },
  "mark": {
    "type": "rect"
  }
}

I think that x2 would need to include the type of x1 in the json. I understand that the definition of X2 does not need type since it should be inferred from the definition of X, but the serialization might export it from there.

Have you considered any alternative solutions?

I have manually defined the type: quantitative for x2 in the json, and the plot now shows correctly. But I think this patch is far from ideal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants