vega / vl-convert

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

Emoji support in PNG export #137

Closed corradio closed 3 months ago

corradio commented 10 months ago

Hello, What would need to happen in order to have emoji support in PNG exports? Thanks, Olivier

jonmmease commented 10 months ago

Hi, as you noted over in https://github.com/RazrFalcon/resvg/issues/485#issuecomment-1826847676, this is dependent on emoji support in resvg.

jonmmease commented 10 months ago

As a workaround for some use cases, you can use a site like https://emoji.aranja.com/ to download png images of individual emoji, then host them somewhere (like a github repo), and then display them using the vega/altair image mark like https://altair-viz.github.io/user_guide/marks/image.html.

corradio commented 10 months ago

As a workaround for some use cases, you can use a site like https://emoji.aranja.com/ to download png images of individual emoji, then host them somewhere (like a github repo), and then display them using the vega/altair image mark like https://altair-viz.github.io/user_guide/marks/image.html.

Sweet thank you!

jonmmease commented 8 months ago

It's early days, but I'm playing with writing an alternative PNG renderer that uses a different text engine (cosmic-text) which does support emoji.

See https://pypi.org/project/avenger/

For the example at https://altair-viz.github.io/gallery/isotype_emoji.html.

# Create chart as usual
import altair as alt
import pandas as pd

source = pd.DataFrame([
      {'country': 'Great Britain', 'animal': 'cattle'},
      {'country': 'Great Britain', 'animal': 'cattle'},
      {'country': 'Great Britain', 'animal': 'cattle'},
      {'country': 'Great Britain', 'animal': 'pigs'},
      {'country': 'Great Britain', 'animal': 'pigs'},
      {'country': 'Great Britain', 'animal': 'sheep'},
      {'country': 'Great Britain', 'animal': 'sheep'},
      {'country': 'Great Britain', 'animal': 'sheep'},
      {'country': 'Great Britain', 'animal': 'sheep'},
      {'country': 'Great Britain', 'animal': 'sheep'},
      {'country': 'Great Britain', 'animal': 'sheep'},
      {'country': 'Great Britain', 'animal': 'sheep'},
      {'country': 'Great Britain', 'animal': 'sheep'},
      {'country': 'Great Britain', 'animal': 'sheep'},
      {'country': 'Great Britain', 'animal': 'sheep'},
      {'country': 'United States', 'animal': 'cattle'},
      {'country': 'United States', 'animal': 'cattle'},
      {'country': 'United States', 'animal': 'cattle'},
      {'country': 'United States', 'animal': 'cattle'},
      {'country': 'United States', 'animal': 'cattle'},
      {'country': 'United States', 'animal': 'cattle'},
      {'country': 'United States', 'animal': 'cattle'},
      {'country': 'United States', 'animal': 'cattle'},
      {'country': 'United States', 'animal': 'cattle'},
      {'country': 'United States', 'animal': 'pigs'},
      {'country': 'United States', 'animal': 'pigs'},
      {'country': 'United States', 'animal': 'pigs'},
      {'country': 'United States', 'animal': 'pigs'},
      {'country': 'United States', 'animal': 'pigs'},
      {'country': 'United States', 'animal': 'pigs'},
      {'country': 'United States', 'animal': 'sheep'},
      {'country': 'United States', 'animal': 'sheep'},
      {'country': 'United States', 'animal': 'sheep'},
      {'country': 'United States', 'animal': 'sheep'},
      {'country': 'United States', 'animal': 'sheep'},
      {'country': 'United States', 'animal': 'sheep'},
      {'country': 'United States', 'animal': 'sheep'}
    ])

chart = alt.Chart(source).mark_text(size=45, baseline='middle').encode(
    alt.X('x:O').axis(None),
    alt.Y('animal:O').axis(None),
    alt.Row('country:N').title(''),
    alt.Text('emoji:N')
).transform_calculate(
    emoji="{'cattle': '🐄', 'pigs': '🐖', 'sheep': '🐏'}[datum.animal]"
).transform_window(
    x='rank()',
    groupby=['country', 'animal']
).properties(
    width=550,
    height=140
)

# Export to PNG with avenger
import avenger
png = avenger.altair_utils.chart_to_png(chart, scale=1)
with open("emoji.png", "wb") as f:
    f.write(png)

emoji.png: emoji

jonmmease commented 3 months ago

Done in 1.5.0!