Multiple Axes

Chart FX for Java supports the creation of multiple axes so you can plot values in different scales on the same chart. This section describes how you can create and assign series to additional axes in the chart.

Please note that additional axes must be referenced through the Axis object that takes an index of the axis and lists all properties and techniques described previously in this chapter.

Creating a Secondary Y-axis

In order to create a two series chart with the first series' values ranging between 1-100 and the second series' values ranging between 100000-200000, the first series will not be visible because the scale chosen by Chart FX for Java will be between 0 - 200000, making all values between 0-100 invisible to the eye.

In this case, you may want to assign one of the series to a secondary Y-axis and assign a completely different scale by accessing the Y-axis property in the Series object as follows:


chart1.getSeries(0).setYAxis(YAxis.SECONDARY);

Important Note: You must assign the series to the secondary Y-axis BEFORE the chart is populated. This will allow Chart FX to initialize the secondary Y-axis appropriately. If you can't assign the series before the data is passed, you must invoke the RecalcScale method so all of the axes are recalculated.

Essentially, you will use the same properties that you have read about in this chapter to handle scales, gridlines and different settings for the secondary Y-axis. For example, if you want to set the Min, Max and Step and Decimals for the secondary Y-axis, you will use the Axis object with the 1 index to access the secondary Y Axis as follows:


Axis axis = chart1.getAxis(1);
axis.setMin(300000);
axis.setMax(2500000);
axis.setStep(200000);
axis.getLabelsFormat().setDecimals(0);

The secondary Y-axis will be shown to the right of the chart as depicted in the following figure:

Creating Additional Axes

Creating and displaying additional axes in the chart is very similar to assigning a secondary Y-axis.

For example, if you want to add a third series to the previous chart, which values range between 0 and 1; both scales (0-100 and 0-2000000) are still inadequate to represent fractions between 0 and 1. Therefore, we need a fourth axis to represent these values. Please remember that three axes are already displayed in the chart: Y-axis, Secondary Y-axis and the X Axis.

To do this we will assign the third series to the Y-axis who's index is 3, as follows:


chart1.getSeries(2).setYAxis(3);

Please remember Chart FX for Java indexes are zero based, therefore the code shown above should be read: assign the third series to a fourth axis.

Also remember this code must be invoked before the chart is populated so Chart FX can initialize the fourth axis properly. If this can't be done, then use the RecalcScale method after populating the chart.

Then you can use properties exposed by the Axis(3) object to configure the chart. For example, to show the new axis on the left side you can use the Position property as follows:


Axis axis = chart1.getAxis(3);
axis.setPosition(AxisPosition.NEAR);
axis.setMax(100);
axis.setMin(0);

The example shown above can be repeated for as many axes as needed in your chart.