CSS
With ninejs, you can add your own CSS for advanced plot customization. Here's how it works.
What is CSS?¶
CSS has two main components:
- Selectors: which elements the style applies to
- Rules: a set of
key: valuepairs defining the style
A basic CSS rule looks like this:
This means: "For every element with the tooltip class, set the background to red and the text color to blue."
Applying CSS to a plot¶
You can directly apply a CSS string to your plot:
Using a Python dictionary¶
For better readability and reusability, you can define CSS as a dictionary:
Method chaining also works if you want to split styles:
(
interactive(gg)
+ css(".tooltip {font-size: 2em;}")
+ css(from_dict={".tooltip": {"color": "blue", "background": "red"}})
)
Loading CSS from a file¶
If your CSS is stored in a .css file like:
You can load it with:
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.polygon: 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!
Default CSS¶
You can find the default CSS applied by ninejs here.
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. ↩