JavaScript
With ninejs, you can add your own JavaScript for advanced plot customization. Here's how it works.
What is JavaScript?¶
JavaScript is the language that makes web pages interactive: it reacts to clicks, animates elements, and changes the page on the fly. It runs directly in the browser, on the page where your plot is displayed.
A tiny JavaScript snippet looks like this:
document.querySelectorAll(".bar").forEach((bar) => {
bar.addEventListener("click", () => alert("You clicked a bar!"));
});
This means: "For every element with the bar class, when it is clicked, show an alert."
Applying JavaScript to a plot¶
You can directly apply a JavaScript string to your plot:
import pandas as pd
from plotnine import aes, geom_col, ggplot, labs, theme_minimal
from ninejs import interactive, save, javascript
df = pd.DataFrame({"category": ["A", "B", "C", "D", "E"], "value": [3, 7, 5, 9, 4]})
gg = (
ggplot(df, aes(x="category", y="value", tooltip="category"))
+ geom_col(fill="#4C78A8")
+ labs(x="Category", y="Value")
+ theme_minimal()
)
hello_js = """
document.querySelectorAll(".bar").forEach((bar) => {
bar.addEventListener("click", () => alert("You clicked a bar!"));
});
"""
(
interactive(gg)
+ javascript(hello_js)
+ save("docs/iframes/javascript-hello.html")
)
Warning
JavaScript you add this way runs as-is in the generated page. Only use code you trust.
Examples¶
Mutate the bar's fill attribute when clicked.
Fade the bars in one after the other when the chart loads (refresh the page to show the animation).
Selectable elements¶
To style or add interactivity, you need to select elements using the DOM1. These are the most common selectors:
Plot elements¶
.point: scatter plot points.line: line chart lines.area: area chart fills.bar: bar chart bars.polygons: polygon chart maps.plot-element: all of the above
You can combine these with .hovered or .not-hovered, e.g., .point.hovered.
Misc¶
.tooltip: tooltip shown on hoversvg: the entire SVG element (e.g., the whole chart)
Question
Something missing? Please open an issue!
Appendix¶
-
The DOM (Document Object Model) is like a tree structure representing your web page. JavaScript and CSS use it to select, modify, and interact with elements dynamically. ↩