Another interesting way to change colors in Chart FX is to use the Palette property, where all series and different objects (like backgrounds, gridlines, etc.) will follow a pre-defined color palette. By default, Chart FX provides a myriad of pre-defined color-palettes. For example, if you want the chart to be displayed using the "Pastels" palette, your code should look like:
chart1.setPalette("DarkPastels.Pastels");
|
Other Chart FX for Java pre-defined color palettes include:
- Default.Alternate
- Default.ChartFX6
- Default.EarthTones
- Default.ModernBusiness
- Default.Windows
- DarkPastels.AltPastels
- HighContrast.HighContrast
- ChartFX5
Color Schemes
Another interesting feature of Chart FX for Java related to colors is the chart’s Color Scheme. This feature allows you to change the fill of a marker from Solid, Gradients, Colored Patterns and Black & White patterns. This feature is very useful if your application needs to display markers with patterns to improve readability of the chart.
This feature is accessible from Chart FX for Java User Interface dialogs as well as through the API. Using the Chart class Scheme property, you can set the color scheme for the entire chart. The PointAttributes and SeriesAttributes classes also support Scheme members that allow you to change the scheme for individual markers and complete series. To set the color scheme for a chart to colored patterns, you could use the following code:
chart1.setScheme(Scheme.PATTERN);
|
When using color patterns in your charts, you may control the colors used for the patterns of each marker by utilizing the SeriesAttributes and PointAttributes classes Color and AlternateColor members.
chart1.getSeries(1).setColor(java.awt.Color.red); chart1.getSeries(1). setAlternateColor(java.awt.Color.yellow); chart1.getPoint(0,0).setColor(java.awt.Color.red); chart1.getPoint(0,0). setAlternateColor(java.awt.Color.yellow);
|
The Pattern property, supported by the SeriesAttributes class and the PointAttributes class, allows you to select the pattern style to be used for the selected markers.
chart1.getSeries(0).setPattern(HatchStyle.CROSS); chart1.getPoint(0,0).setPattern(HatchStyle.DIVOT);
|
2D Colored Lines (Line Charts)
By default, Chart FX for Java will paint connecting lines using the same color as the configured series color. You can instruct Chart FX to paint the lines of a 2D line chart to a specified color using the Border, BorderColor, BorderEffect and TypeMask (ColorLines flag) properties. Below is an example of how to set the color of lines in a 2D line chart to black:
chart1.setBorder(true); chart1.setTypeMask(chart1.getTypeMask() ^ ChartType.COLORLINE); chart1.setBorderEffect(BorderEffect.NONE); chart1.setBorderColor(java.awt.Color.black);
|
Note: When the MarkerShape property is set to ‘NONE’, the lines in a line chart will be set to the series color.
Legend Background Colors
Users may set a background color for a chart legend using the selected legend’s BackColor property. The series, values and user legend objects all support this property. To set the background color for the series legend box to green, you would use the following code:
chart1.getSerLegBoxObj().setBackColor(java.awt.Color.green);
|
|