vega / altair

Declarative statistical visualization library for Python
https://altair-viz.github.io/
BSD 3-Clause "New" or "Revised" License
9.26k stars 793 forks source link

interactive geoshape chart doesn't show #679

Closed iliatimofeev closed 1 year ago

iliatimofeev commented 6 years ago

Chart(...).mark_geoshape(...)...interactive() generates invalid json that couldn't be shown.

import altair as alt
from vega_datasets import data
#alt.renderers.enable('notebook')

states = alt.topo_feature(data.us_10m.url, feature='states')
# US states background
background = alt.Chart(states).mark_geoshape(
    fill='lightgray',
    stroke='white'
).properties(
    projection={'type': 'albersUsa'},
    width=500,
    height=300)

background.interactive()

Vega Editor shows error

[Error] Error: Expression parse error: " ? {unit: \"\", intervals: []} : null"
[Warning] Cannot project a selection on encoding channel "x", which has no field.
[Warning] Cannot project a selection on encoding channel "y", which has no field.

on this generated json

{"$schema":"https://vega.github.io/schema/vega-lite/v2.3.0.json",
    "width": 500,
    "height": 300,
    "config": {
        "view": {
            "width": 400,
            "height": 300
        }
    },
    "data": {
        "url": "https://vega.github.io/vega-datasets/data/us-10m.json",
        "format": {
            "feature": "states",
            "type": "topojson"
        }
    },
    "mark": {
        "type": "geoshape",
        "fill": "lightgray",
        "stroke": "white"
    },
    "projection": {
        "type": "albersUsa"
    },

 -->"selection": {
        "selector010": {
            "type": "interval",
            "bind": "scales",
            "encodings": [
                "x",
                "y"
            ]
        }
    }

}
jakevdp commented 6 years ago

Geo markers do not yet support interaction in Vega-Lite. The issue is being tracked here: https://github.com/vega/vega-lite/issues/3306

jakevdp commented 6 years ago

Looking closer, the particular error you're getting is not related to geographic markers; you're asking for interactive x/y encodings, but you've not mapped those encodings to any data. You get the same error with a simpler chart; e.g.

alt.Chart('data.csv').mark_point().interactive()
iliatimofeev commented 6 years ago

I expected that interactive() give me a zoomable version of the chart which i can get without it :)

Something like https://vega.github.io/vega/examples/zoomable-world-map/ Or better like https://bl.ocks.org/mbostock/2374239 it works more natural

So i think that for simplicity details like encoding of x or longitude should be hidden from me in that case.

By the way according to documentation { "type": "interval", "bind": "scales" } will be enough to zooming, so adding encoding block in interactive() looks unnecessary.

Another problem that it shows empty cell without any explanation or hints on the causes. I know that it's another issue and is somewhere tracked :) but I lost the link.

jakevdp commented 6 years ago

The zoomable plot you linked to is a vega plot; vega supports interactive geographic scales, but vega-lite does not, so there's no way to make the Altair API create zoomable geographic visualizations. If that is ever supported by vega-lite, then we can think about details of what the Altair API might look like.

Regarding the empty plot: we're working on a solution that will display javascript errors in the notebook in cases like this. See #512 and #666.

joelostblom commented 1 year ago

This will available in Altair after https://github.com/vega/vega-lite/issues/3306 is merged, so closing this as there is nothing to do on the Altair side of things.