Labels

Labels may be configured in a number of ways. By default, Chart FX will attempt to label a selected axis based on its configured format (numerical by default). When data is passed to Chart FX via one of the supported Data Providers, Chart FX will use string fields for labels and numerical fields for chart values.

Chart FX will determine these assignments using built-in algorithms, however, developers may also configure how fields will be used within a chart by utilizing the DataType property. By specifying a field as a Label, you can control exactly what values will be painted as labels in the chart.

Note: Legends may also be configured using the DataType property when using a supported Chart FX Data Provider. Please see the Passing Data section in the programmer's guide for more information.

Setting Labels using the API

Setting the Labels for an axis may be done using the Label member of the Axis class. This property is used to read/write labels for a categorical axis. By default, the X-axis is a categorical axis containing labels associated with each major tick mark, while the Y-axis label is a numerical axis where labels are controlled by the scale (Min, Max, etc.) and numerical data contained in the chart.

You can set as many labels as there are points available in the chart. These labels will be shown in the major tick marks of the selected axis. Some of them can be left empty so no labels are shown on that specific major tick mark.

Developers may control how frequent these labels are displayed by using the Step property. Each label's visual attributes such as rotation, color and font are also accessible through the LabelAngle, TextColor, and Font properties, respectively.

By default, Chart FX does not associate any labels to the categorical axis (X-axis) and a "TAG" number will be shown. When you use this property, the TAG numbers will be replaced by settings specified in this property.

The following code can be employed to set the X-axis labels to the months of the year:


Axis xaxis = chart1.getAxisX();
xaxis.getLabel().setItem(0, "January");
xaxis.getLabel().setItem(1, "February");
xaxis.getLabel().setItem(2, "March");
xaxis.getLabel().setItem(3, "April");
xaxis.getLabel().setItem(4, "May");
xaxis.getLabel().setItem(5, "June");
xaxis.getLabel().setItem(6, "July");
xaxis.getLabel().setItem(7, "August");
xaxis.getLabel().setItem(8, "September");
xaxis.getLabel().setItem(9, "October");
xaxis.getLabel().setItem(10,"November");
xaxis.getLabel().setItem(11,"December");

Note: The Chart class Legend property may also be used to configure X-axis labels.

KeyLeg Property

You normally use the Label or Legend property to assign labels to the tick marks in the X-axis (when it is categorical). However, depending on the number of points in the chart, these labels may be too long to fit properly in the space allotted for each tick mark in the X-axis.

Although you can control how frequently the labels are displayed and other properties supported by the Axis object, sometimes there will still be space limitations because of the size of the chart or the number of points. The KeyLeg property can help you display shorter labels in the X-axis while maintaining longer labels in the legend window. Let's suppose you have assigned the following labels to the X-axis with the Legend property:

"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November" and "December".

Now, these labels may or may not fit in the space allotted for each tick mark in the X-axis. If one of them does not fit, Chart FX will immediately display a TAG (red number) indicating that such label cannot fit in the space allotted for it. This problem increases as the number of points in the chart increase.

Using the KeyLeg property, developers can assign key labels (shorter strings):

"Jan", "Feb", "Mar", "Apr", etc.

These will be displayed on the X-axis itself, while the Legend window will display both the Key Legend and the Legend set with the Label property as depicted in the following figure:


chart1.setKeyLeg(0, "Jan");
chart1.setKeyLeg(1, "Feb");
chart1.setKeyLeg(2, "Mar");
chart1.setKeyLeg(3, "Apr");
...

Series Lables

Setting the Labels for series may be done using the Series class Legend member. This property is used to read/write legends keys for selected series in the chart. It is important to specify the correct index in for the Series object in order to set the correct legend string. These legend keys will be displayed in the chart when the Series Legend is enabled.

To configure the series legend for a chart, the following code may be used:


chart1.getSeries(0).setLegend("Product A");
chart1.getSeries(1).setLegend("Product B");
chart1.getSeries(2).setLegend("Product C");
chart1.getSeries(3).setLegend("Product D");
chart1.getSeries(4).setLegend("Product F");

The SerLeg property may also be used to accomplish the same series legend assignment:


chart1.getSerleg().setItem(0, "Product A");
chart1.getSerleg().setItem(1, "Product B");
chart1.getSerleg().setItem(2, "Product C");
chart1.getSerleg().setItem(3, "Product D");
chart1.getSerleg().setItem(4, "Product F");

Once you have set series labels for the series in the chart, they will be visible when the series legend is enabled in the chart. This legend may be treated as an additional tool with different properties that can be accessed using the Series Legend object. This object may be returned by the SerLegBoxObj property exposed by the Chart class.

SerKey Property

The SerKey property may be used to label the series in the Z-Axis when a chart is clustered (3D). It may also be used to configure a short series legend label, much like the KeyLeg proeperty. Here is a sample showing how to set labels for the Z-Axis of a clustered bar chart:


chart1.openData(COD.VALUES,5,6);
for (int i=0;i<5;i++)
for (int j=0;j<6;j++)
{
chart1.setValue(i,j,java.lang.Math.random() * 100);
chart1.getSerleg().setItem(i, "S" + (i+1));
chart1.getSerKey().setItem(i, "S" + (i+1));
}
chart1.closeData(COD.VALUES);
chart1.setCluster(true);
chart1.setChart3D(true);
chart1.setSerLegBox(true);