Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pygal charts not displaying tooltips in Jupyter / IPython notebook

After much research, I finally managed to get tooltips working in pygal thus:

Config = pygal.Config()
Config.js = ['http://kozea.github.io/pygal.js/2.0.x/pygal-tooltips.js']
bar_chart = pygal.Bar(Config)                                      # Then create a bar graph object
bar_chart.add('Fibonacci', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55])  # Add some values
bar_chart.render_to_file('bar_chart.svg', force_uri_protocol='https') 

In the produced .svg, tooltips are now working nicely, but only when the file is opened in a browser.

When the chart is displayed directly in Jupyter (either with IPython.core.display.SVG(filename="bar_chart.svg") or simply bar_chart), the tooltips and styling are not present.

Is this a known limitation? Or can it be achieved?

like image 895
Pyderman Avatar asked Oct 18 '25 19:10

Pyderman


2 Answers

My workaround involved setting up the HTML with the necessary javascript and chart svg like so:

import pygal
from IPython.display import display, HTML

base_html = """
<!DOCTYPE html>
<html>
  <head>
  <script type="text/javascript" src="http://kozea.github.com/pygal.js/javascripts/svg.jquery.js"></script>
  <script type="text/javascript" src="https://kozea.github.io/pygal.js/2.0.x/pygal-tooltips.min.js""></script>
  </head>
  <body>
    <figure>
      {rendered_chart}
    </figure>
  </body>
</html>
"""

def galplot(chart):
    rendered_chart = chart.render(is_unicode=True)
    plot_html = base_html.format(rendered_chart=rendered_chart)
    display(HTML(plot_html))

bar_chart = pygal.StackedBar()
bar_chart.add('Bars', [1,2,3,4,5])

galplot(bar_chart)

Not the prettiest solution, but it works

like image 68
Niels Avatar answered Oct 20 '25 13:10

Niels


Found this on the Google search that led me to this question. If you read the pygal documentation or look at an example SVG it generates, you'll see it includes pygal.js, which is missing in your notebook.

like image 40
darkphoenix Avatar answered Oct 20 '25 15:10

darkphoenix