Chart Styling
Charts can be styled using CSS, and in Flow applications also through the Java API. Vaadin Charts also has many built-in theme variants.
|
Caution
|
Both CSS and Java styling API can’t be used in the same chart
While no error is thrown if different styling methods are used in the same chart, only one method should be used in any chart, since having both could lead to unexpected results.
|
Theme Variants
| Variant | Description | Supported by |
|---|---|---|
| Colors varying in hue | Lumo |
| Colors varying in brightness | Lumo |
| Colors matching those in older versions | Lumo |
Java Styling API in Flow
The default styling mode in Flow applications uses the Java API. See the Java API reference for details.
Styling Using Java API Example
In this example Java API is used to change the color of point markers to yellow, their outline to purple, x-axis labels to blue, and the line color to black.
Source code
FlowStyleExample.java
FlowStyleExample.javapublic class FlowStyleExample extends Div {
public FlowStyleExample() {
var chart = new Chart();
var configuration = chart.getConfiguration();
var ds = new DataSeries();
ds.setData(7.0, 6.9, 9.5, 14.5);
ds.setColorAxis(3);
var plotOptions = new PlotOptionsLine();
plotOptions.setColor(SolidColor.BLACK); // Line color
var marker = new Marker();
marker.setFillColor(SolidColor.YELLOW); // Point inside color
marker.setLineWidth(2);
marker.setLineColor(SolidColor.PURPLE); // Point outline color
plotOptions.setMarker(marker);
ds.setPlotOptions(plotOptions);
configuration.addSeries(ds);
configuration.getxAxis().setCategories("Jan", "Feb", "Mar", "Apr");
configuration.getxAxis().getLabels().getStyle().setColor(SolidColor.BLUE); // X axis labels color
add(chart);
}
}
CSS Styling
Styling Charts with CSS works similarly to other Vaadin components: create a vaadin-chart.css file in your theme’s components folder, and place the styles there.
It’s also possible to use the @CssImport annotation to load the style sheet in Flow applications.
See Highcharts styling documentation for details on CSS styling of Charts.
CSS Styling Mode for Flow
Flow applications can use CSS styling by enabling "styled mode" in the Chart configuration:
Source code
Java
var chart = new Chart();
var conf = chart.getConfiguration();
conf.getChart().setStyledMode(true);CSS Styling Example
In this example CSS is used to change the color of point markers to yellow, their outline to purple, data labels to red, and line colors to green.
Source code
styles.css
styles.cssvaadin-chart.first-chart {
--vaadin-charts-color-0: green;
--vaadin-charts-color-1: lightgreen;
--vaadin-charts-color-2: darkgreen;
--vaadin-charts-data-label: red;
}Source code
CssStyleExample.java
CssStyleExample.javapublic class CssStyleExample extends Div {
public CssStyleExample() {
var chart = new Chart();
var configuration = chart.getConfiguration();
var chartModel = configuration.getChart();
chartModel.setType(ChartType.LINE);
chartModel.setStyledMode(true);
configuration.getxAxis().setCategories("Jan", "Feb", "Mar", "Apr");
var ds = new DataSeries();
ds.setData(7.0, 6.9, 9.5, 14.5);
var callout = new DataLabels(true);
callout.setShape(Shape.CALLOUT);
callout.setY(-12);
ds.get(1).setDataLabels(callout);
ds.get(2).setDataLabels(callout);
configuration.addSeries(ds);
chart.addClassName("first-chart");
add(chart);
}
}Source code
themes/{mytheme}/components/vaadin-chart.css - Requires "Theme component style injection" feature flag enabled - com.vaadin.experimental.themeComponentStyles=true
themes/{mytheme}/components/vaadin-chart.css - Requires "Theme component style injection" feature flag enabled - com.vaadin.experimental.themeComponentStyles=true:host(.first-chart) .highcharts-color-0 .highcharts-point {
fill: yellow;
stroke: purple;
stroke-width: 2px;
}
Adding and Styling a Class Name
CSS class names can be applied to both chart instances and individual chart elements.
In the example below, the bold-green-axis class name is applied to the X-axis of a chart to give it a distinct style.
Source code
CssStyleExample.java
CssStyleExample.javaconfiguration.getxAxis().setClassName("bold-green-axis");Source code
themes/{mytheme}/components/vaadin-chart.css - Requires "Theme component style injection" feature flag enabled - com.vaadin.experimental.themeComponentStyles=true
themes/{mytheme}/components/vaadin-chart.css - Requires "Theme component style injection" feature flag enabled - com.vaadin.experimental.themeComponentStyles=true.bold-green-axis {
font-weight: bold;
fill: green;
font-size: 14px;
}
3E5B31FB-DF25-4D1E-80EB-7AB485C7B566