Chart FX for Java supports an abundance of general chart properties exposed by the Chart class that allow the assignment of attributes directly to elements present in a given chart. These general properties are considered to be global for the chart because when assigned, they take affect for all points and series contained within the chart.
However, there are times when setting these properties globally does not allow you to generate the chart output you desire to display to end users. Chart FX for Java exposes the Point and Series objects, which provide the mechanism for setting per-point and per-series attributes for a selected chart.
By exposing these objects in the Chart FX for Java API, developers may create combination charts (Gallery type) as well as setting per-point and per-series attributes that can help convey information to end users. For example, you can color markers individually or separate a pie slice from a pie chart based on a condition.
General Properties
General chart properties are exposed by the Chart class and when configured affect all points and series included in the selected chart. An example of setting a general chart property is setting the chart’s Gallery or chart type. Setting the gallery for the chart using the Chart class Gallery property will set a chart type globally, in other words, only one chart type will be displayed at a time (Bar, Lines, etc.).
chart1.setGallery(Gallery.AREA);
|
Some other supported members of the Chart class include the Border, BorderColor, BorderEffect, CylSides, MarkerShape, MarkerSize, PointLabels, Scheme, TypeMask, Volume, MarkerStep, etc. These are just a few of the general properties exposed by the Chart class, for more supported members please reference the Chart FX for Java API Reference.
Per-Series Attributes
The SeriesAttributes class exposes many of the same properties described as General properties, however, when configured only affect the selected series in the chart. By allowing customization on a per-series basis, Chart FX provides the developer with more control over the supported chart elements. Setting different chart galleries for selected series in a chart permits you to create combination charts to convey more information to the end users at one time. The Series object also allows you to set visual attributes to a series that can bring attention to a selected series. Below is an example of creating a combination chart with two series:
chart1.getSeries(0).setGallery(Gallery.AREA); chart1.getSeries(1).setGallery(Gallery.LINES);
|
Per-Point Attributes
The PointAttributes class also exposes many of the same properties available in the Chart class. Once again, giving the developer access to the point object increases the control over charts generated using Chart FX for Java. The supported PointAttributes class members allow you to set border, color, marker, point label and scheme attributes, as well as many others, directly to a selected point included in the chart.
Setting per-point attributes is most useful for highlighting special points or groups of points within charts. By highlighting selected points in a chart, you can focus the attention of end users to specific information conveyed in the chart. Another useful tool when setting per-point attributes is the user legend box. The user legend box is configured exclusively by the developer and can act as a legend for these special points.
The following code sets the color for a particular point to red; this could be used to show a high or low level for that particular point:
chart1.getPoint(1,3).setColor(java.awt.Color.red);
|
When setting per-point attributes you should also consider efficiency of code, specifically the reuse of per-point attribute objects. In the following code sample, every time the color is assigned for a particular point, a new point attribute is created in memory:
double myLimit = 50; int totSeries = chart1.getNSeries(); int totValues = chart1.getNValues(); for (int i=0;i<totSeries;i++) for (int j=0;j<totValues;j++) if (chart1.getValue(i,j) >= myLimit) {
chart1.getPoint(i, j). setColor(java.awt.Color.red); } else {
chart1.getPoint(i, j). setColor(java.awt.Color.blue); }
|
Notice even though there are more lines of code in this example, only two point attribute objects are created; this code is more efficient, takes less memory and will run much faster. A user legend has also been configured:
double myLimit = 50; int totSeries = chart1.getNSeries(); int totValues = chart1.getNValues();
PointAttributes redPoints = new PointAttributes(chart1); redPoints.setColor(java.awt.Color.red); PointAttributes bluePoints = new PointAttributes(chart1); bluePoints.setColor(java.awt.Color.blue); for (int i=0;i<totSeries;i++) for (int j=0;j<totValues;j++) if (chart1.getValue(i,j) >= myLimit) {
chart1.setPoint(i, j, redPoints); } else {
chart1.setPoint(i, j, bluePoints); }
UserLegendBoxItem userLegItem; userLegItem = chart1.getUserLegendBoxObj().getItem(0); userLegItem.setLabel("Below Limit"); userLegItem.setColor(java.awt.Color.blue); userLegItem.setMarkerShape(MarkerShape.RECT); userLegItem.setBorderEffect(BorderEffect.NONE); userLegItem = chart1.getUserLegendBoxObj().getItem(1); userLegItem.setLabel("Above Limit"); userLegItem.setColor(java.awt.Color.red); userLegItem.setMarkerShape(MarkerShape.RECT); userLegItem.setBorderEffect(BorderEffect.NONE); chart1.setUserLegendBox(true);
|
Also, when you reuse a point attribute object, you can change all the assigned points by modifying the existing object once, rather than looping back through all the points:
redPoints.setColor(java.awt.Color.yellow);
|
|