Java Reference
In-Depth Information
c.getData().add(new XYChart.Data(Integer.toString(i), cValue));
cValue = cValue + 4 * Math.random() - 2;
cpp.getData().add(new XYChart.Data(Integer.toString(i), cppValue));
cppValue = cppValue + 4 * Math.random() - 2;
}
answer.addAll(java, c, cpp);
return answer;
}
}
Running the modified application results in output similar to Figure
8-9
.
Figure 8-9.
Using a
ScatterChart
with a
CategoryAxis
on the
xAxis
Using the LineChart
The example in the previous section resulted in data entries being represented by single dots or symbols. Often, it is
desirable to have the dots connected by a line because this helps in seeing trends. The JavaFX
LineChart
is well
suited for this.
The API for the
LineChart
has many methods in common with the API for the
ScatterChart
. In fact, we can
reuse most of the code in Listing 8-6, and just replace the
ScatterChart
occurrences with
LineChart
and the import
for
javafx.scene.chart.ScatterChart
with
javafx.scene.chart.LineChart
. The data stay exactly the same, so we
only show the new
start()
method in Listing 8-7.
Listing 8-7.
Using a
LineChart
Instead of a
ScatterChart
public void start(Stage primaryStage) {
CategoryAxis xAxis = new CategoryAxis();
NumberAxis yAxis = new NumberAxis();
LineChart lineChart = new LineChart(xAxis, yAxis);
lineChart.setData(getChartData());
lineChart.setTitle("speculations");
primaryStage.setTitle("LineChart example");
Search WWH ::
Custom Search