问题
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?
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/36322683/pygal-charts-not-displaying-tooltips-in-jupyter-ipython-notebook