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 Pie Chart #2148

Closed
Delaunay opened this issue May 12, 2020 · 18 comments
Closed

Add Pie Chart #2148

Delaunay opened this issue May 12, 2020 · 18 comments

Comments

@Delaunay
Copy link

I am building a dashboard with flask & altair and I feel the pie chart is missing.
I tried hacking it in following this spec.
But it was harder than expected.

Would be great if that was builtin

@jakevdp
Copy link
Collaborator

jakevdp commented May 12, 2020

Vega-Lite recently added pie chart support in version 4.9, but Altair has not yet been updated to support this release. The next version of Altair should make this possible.

@austinorr
Copy link

Looks like Vega-Lite added significant new features at the end of March, including enabling pie charts and marker rotation (arrows are now possible!). If one were interested in contributing to help update altair incorporate these new capabilities, how would one help @jakevdp and his other core devs with that process? I looked for a development branch, or one tagged with a Vega-Lite version number but most branches appear to be merged PRs from long ago.

I've read the maintainer instructions and I can follow along, but I'm new to altair and I'm wondering if there's an update already in progress that needs more help, or if it would be useful for me to start one. Not wishing to step on any toes (release branches are not usually started by new contributors), just hoping to help, and/or hear that this process is underway and well supported.

@jakevdp
Copy link
Collaborator

jakevdp commented May 14, 2020

The big blocker in this case is updating the code generation tools to handle the new datum encoding. There are no docs because a new class of encoding has not been added since Altair was created.

@austinorr
Copy link

Thanks for the reply, I should have known it wouldn’t be trivial. Is there a path forward or is this one of those road blocks that ends up requiring a time consuming & paradigm shifting refactor? Also, maybe this thread is not the best place for tracking this discussion any more. Does this new encoding warrant its own issue?

@jakevdp
Copy link
Collaborator

jakevdp commented May 14, 2020

Roughly, the idea is this: we will need a DatumChannelMixin similar to the ValueChannelMixin: https://github.com/altair-viz/altair/blob/52732ab3660392c0c41786683f4ba8a276ee00ad/tools/generate_schema_wrapper.py#L145-L159

and a DatumSchemaGenerator similar to the ValueSchemaGenerator: https://github.com/altair-viz/altair/blob/52732ab3660392c0c41786683f4ba8a276ee00ad/tools/generate_schema_wrapper.py#L176-L186

Then these will have to be constructed as part of the loop further down in the file, which may not be straightforward depending on the vega-lite schema's conventions regarding how these definitions are named.

@austinorr
Copy link

I have some catching up to do if I’m going to be of any use in helping with this; I’ll need to better understand the Vega and VegaLite json schemas, and I’ll need more time with the code generation patterns to get my head around the arguments that are passed to the code templates.

Thanks taking time to show me where to begin.

@soundquiet
Copy link

hi, is pie chart now available in altair?

@jakevdp
Copy link
Collaborator

jakevdp commented Dec 20, 2020

No, Altair does not yet support Vega-Lite 4.9 features.

@kstathou
Copy link

Hi @jakevdp, thanks for maintaining/developing Altair, it's my go-to data visualisation library! Is there a roadmap for supporting newer versions of Vega-Lite?

@jakevdp
Copy link
Collaborator

jakevdp commented Feb 11, 2021

Not sure... the update is a bit complicated because Vega-Lite 4.9 added the "datum" encoding specification alongside "field" and "value", so a bunch of the assumptions in tools/generate_schema_wrapper.py are broken by the new version.

@mattijn
Copy link
Contributor

mattijn commented Feb 12, 2021

I played around a bit, just to get a start of an understanding what this generate_schema_wrapper is trying to do. Over there: https://github.com/mattijn/altair/tree/vl_v5 (not worth a PR, but maybe inspiration for others who are brave and clever😉).

I got it to work a bit:

import pandas as pd
import altair as alt

df = pd.DataFrame({'category': [1, 2, 3, 4, 5, 6], 'value': [4, 6, 10, 3, 7, 8]})

alt.Chart(df).mark_arc().encode(
    theta=alt.Theta(field='value', type='quantitative'), 
    color=alt.Color(field='category', type='nominal')
)

image

# this also works
alt.Chart(df).mark_arc().encode(
    theta=alt.PositionFieldDefBase(field='value', type='quantitative'), 
    color=alt.PositionFieldDefBase(field='category', type='nominal')
)

# this creates an empty chart
alt.Chart(df).mark_arc().encode(
    theta=alt.PositionDatumDefBase(datum='value', type='quantitative'), 
    color=alt.PositionDatumDefBase(datum='category', type='nominal')
)

Now I also understand what you mean with:

Then these will have to be constructed as part of the loop further down in the file, which may not be straightforward depending on the vega-lite schema's conventions regarding how these definitions are named.

Since you check for if "Value" in basename: here:https://github.com/mattijn/altair/blob/vl_v5/tools/generate_schema_wrapper.py#L435, but you cannot do this for Datum since Datum is never included in basename. So the DatumSchemaGenerator function is never called

@jeet183
Copy link

jeet183 commented Feb 19, 2021

Is mark_arc supported by altair in the latest version? I am getting 'Chart object has no attribute mark_arc'

@mattijn
Copy link
Contributor

mattijn commented Feb 19, 2021

No. This issue is still open

@ChristopherDavisUCI
Copy link
Contributor

Hi @mattijn I tried adapting your code for pie charts; thanks for sharing it! I think I got a few extra parts to work but also a few other things got broken. I shared it here (not sure if that's the best place or not): #2509

I'm very happy for any feedback! I am not experienced with open-source software development and in many cases am not sure what the etiquette and conventions are.

@mattijn
Copy link
Contributor

mattijn commented Oct 25, 2021

Its great you are trying to push things forward @ChristopherDavisUCI! You have managed to dig deep in Altair internals and tried to give it another spin.
Before you get stuck and give up in despair for not getting everything to work in #2509, it might be also good to get things more clear in the discussion issue in #2425.

@1081
Copy link

1081 commented Nov 26, 2021

Is there a workaround to use pie charts? Would loading a specific version of Vega Light work?

import altair.vegalite.v4_8 as alt

@jakevdp
Copy link
Collaborator

jakevdp commented Nov 26, 2021

Pie charts are part of Altair 4.2 which is not yet released, but a release candidate is available:

pip install altair==4.2.0rc1

You can see some examples here: https://altair-viz.github.io/gallery/index.html#circular-plots

@joelostblom
Copy link
Contributor

Closing since 4.2 is now released.

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

No branches or pull requests

10 participants