With stylesheets, Chartbreaker can be highly customized. It is possible to change the layout, colors, fonts, and various other properties.
Stylesheets are basically JavaScript Arrays containing individual rules consisting of a selector, and a set of values to be applied to all objects matching the selector. In this regard, they are very similar to CSS. Different objects support different properties of different types. E.g. a property called "text-color" will accept any valid css color, while "line-width" will expect an integer, and "font" a CSS font descriptor.
A stylesheet could look like this:
const style = [
{
"selector": "chartset",
"values": {
"background-color": "#F7FBFF",
// additional properties could follow here ...
}
},
// additional rules could follow here ...
];
Every Controller can use its own stylesheet. It can be set at any time with the Controller.setStyle method. The method accepts three different kinds of parameters:
The style identifiers are resolved via the global setting "styleURL" (read or write via utils.get / utils.set).
import { utils } from '@stock3/charting-bundle-chartbreaker';
utils.set('styleURL', 'https://www.example.com/styles/');
controller.setStyle("foobar"); // => will try to load 'http://example.com/styles/foobar'
The advantage of using style identifiers is that they are serialized and deserialized automatically along with the contents of the chart. After deserializing a chart with an associated style identifier, the style is automatically loaded and applied. This makes it very easy, for example, to allow your users to switch between different themes.
In contrast, stylesheets that are applied directly or loaded via a URL are NOT serialized/deserialized and must be managed manually.
Like in CSS, selectors are matched right to left, so it is not neccessary to always specify the complete object hierarchy. Also, multiple selectors may be concatenated with commas. E.g. to change the property of all markers, you could use "chart tool marker, chart marker" or just the selector "marker" instead.
For example, to change the line color of all tools, you could use:
{
"selector": "tool",
"values": {
"line-color": "#f00"
}
}
To change the line color of just TrendLines, you could use:
{
"selector": "tool.TrendLine",
"values": {
"line-color": "#f00"
}
}
You could provide a custom color for only the CCI indicator like this:
{
"selector":"indicator.CCI layer.line",
"values": {
"line-color": "#00f"
}
}
Rules are always applied in the order of appearance in the stylesheet. E.g. the following stylesheet will yield red lines for all TrendLines, but blue lines for all other tools:
{
"selector": "tool",
"values": {
"line-color": "blue"
}
},
{
"selector": "tool.TrendLine",
"values": {
"line-color": "red" // this property will overwrite the previously encountered value for matching objects
}
}
If the two blocks of the previous example were swapped, then ALL tools would be blue.
The following object hierarchy can be used for stylesheet selectors:
Stylesheets also support classes, just like in CSS. Various classes are automatically applied to objects and can be used to make more specific rules:
Indicators are automatically assigned their type as a class. For example, the indicator "SMA" will have the class "SMA", so the following will match only layers used with this indicator type:
{
"selector": "indicator.SMA layer",
"values": {
"line-color": "#f00"
}
}
Just like Indicators, Tools are automatically assigned their type as a class. For example, the tool "TrendLine" will have the class "TrendLine", so following will only affect the color of TrendLines:
{
"selector": "tool.TrendLine",
"values": {
"line-color": "#000"
}
}
In addition to the common rules, there is one reserved special selector: "palettes". Palettes are sets of color definitions that can be used for example to populate color pickers with some predefined choices in the user interface. They are also used to automatically select line colors.
Chartbreaker contains defaults for all available palettes:
To define custom palettes using the reserved "palettes" selector:
{
"selector" : "palettes",
"values": {
"lines" : [
"#ffcf00", "#807b79", "#299dc3", "#696185",
"#904583", "#658955", "#895555", "#9d9991"
]
}
}
If the stylesheet used does not specify fixed values for the line-color property of line layers (selector "layer.line"), this property is assigned automatically so that no color is used twice in a single chart. The colors are selected from the 'lines' palette.
To access palettes programmatically via the API the following methods can be used:
controller.getStyle().getPalette('lines');
The value returned will be an array of strings, as defined in the stylesheet.