Java provides a Color Class that can be used to assign colors to all of the color type properties supported by Chart FX for Java. For example, if you would like to set the background color for a chart to blue, then you would utilize the BackColor property in conjunction with the Color class static fields:
chart1.setBackColor(java.awt.Color.blue);
|
The Color class can also be used to create colors with RGB values. This overloaded method allows you to create a Colors from the four 8-bit RGB component values. Below is an example:
chart1.setInsideColor(new java.awt.Color(255, 255, 0));
|
Controlling the Color of Various Chart Elements
Chart FX for Java supports many properties that allow you to customize the color scheme of your charts. Some of the elements that allow customization are the chart’s background, inside background, series, points, gridlines, interlaced grids and many more. This next section will cover some of the common chart elements that can be altered and the properties used to do so.
BackColor Property
The Chart class supports the BackColor property that is used to set the color for the chart’s background color. Below is a sample of setting the background color for a chart to ‘magenta’:
chart1.setBackColor(java.awt.Color.magenta);
|
InsideColor Property
Also supported by the Chart class is the InsideColor property. This color allows you to set the color for the inside charting area of the chart, including the back wall in 3D charts. Below is the code used to set the inside color of a chart to ‘orange’:
chart1.InsideColor(java.awt.Color.orange);
|
Note: When using the BackgroundImage property, it is a good idea to set the InsideColor to ‘Transparent’ so the background image may be easily viewed.
BorderColor Property
Utilizing the Chart class BorderColor property allows you to set the color for the borders of markers in the chart. By default the borders of markers are set to a darker shade of the same color for the selected marker. For the BorderColor property to be applied to a selected marker, the BorderEffect property for the marker must be set to ‘NONE’ as well as the Border property set to ‘true’. The following code will set all markers in the chart to have a black border:
chart1.setBorder(true); chart1.setBorderEffect(BorderEffect.NONE); chart1.setBorderColor(java.awt.Color.black);
|
Axis Gridline Colors
Gridlines may be set for any axes. Axis gridlines may also be set to specific colors by utilizing the Color property of the Line class, however, the gridlines must be made visible using the Axis class GridLines property. Below is an example of setting the gridline color for the Y-axis to ‘cyan’:
chart1.getAxisY().setGridlines(true); chart1.getAxisY().getGrid().setColor(java.awt.Color.cyan);
|
Interlaced Grids
Interlaced grids allow you to alternate between two different colors of gridlines for a selected axis. This can greatly improve chart readability for the end user by visually separating the scale values of an axis. The members used to control these colors are the Chart class InsideColor property and the Axis class AlternateColor property (Axis class Interlaced property must also be ‘true’). Below is an example of creating an interlaced grid for the Y-axis:
chart1.getAxisY().setAlternateColor(java.awt.Color.red); chart1.setInsideColor(java.awt.Color.yellow); chart1.getAxisY().setInterlaced(true);
|
Assigning Individual Colors to Markers
The main point of using a chart is to visually convey data to a user in a graphical format. In some cases, you do not want colors associated to series; you want to be able to assign colors individually by marker. For example, you may need a bar chart that has red bars for negative values and blue bars for positive values. The PointAttributes class supports a Color property that enables you to color selected markers in your charts. To color a specific marker in a chart to red, the following code may be used:
chart1.getPoint(0, 0).setColor(java.awt.Color.red);
|
Coloring Markers per Series
The SeriesAttributes class also provides a Color member (derived from the PointAttributes class) that can be used to change colors associated with each series in the chart. For example, the following code will set the markers of the first series in a chart to red:
chart1.getSeries(0).setColor(java.awt.Color.red);
|
|