Java Reference
In-Depth Information
Next, we want to add a title to the chart, and we want to have names near the symbols in the legend node. Adding
a title to the chart is no different from adding a title to the
PieChart
and is achieved by the code:
scatterChart.setTitle("Speculations");
By adding a name to the three instances of
XYChart.Series
, we add labels to the symbols in the legend node.
The relevant part of the
getChartData
method becomes
Series<Integer, Double> java = new Series<>();
Series<Integer, Double> c = new Series<>();
Series<Integer, Double> cpp = new Series<>();
java.setName("java");
c.setName("C");
cpp.setName("C++");
Running the application again after applying both changes results in output similar to that shown in Figure
8-8
.
Figure 8-8.
ScatterChart
with a title and named symbols
Until now, we used a
NumberAxis
for the
xAxis
. Inasmuch as years can be represented as
Number
instances,
that works. However, because we don't do any numerical operation on the years, and because the distance between
consecutive data entries is always one year, we can also use a
String
value to represent this information.
We now modify the code to work with a
CategoryAxis
for the
xAxis
. Changing the
xAxis
from a
NumberAxis
to a
CategoryAxis
also implies that the
getChartData()
method should return an instance of
ObservableList<XYChart.Series<String, Double>>a
nd that implies that the different elements in a single
Series
should have the type
XYChart.Data<String, Double>
.
In Listing 8-6, the original code has been modified to use the
CategoryAxis
.
Search WWH ::
Custom Search