Java Reference
In-Depth Information
Listing 8-6. Using CategoryAxis Instead of NumberAxis for the xAxis
package projavafx ;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.ScatterChart;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class ChartApp6 extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
CategoryAxis xAxis = new CategoryAxis();
NumberAxis yAxis = new NumberAxis();
ScatterChart scatterChart = new ScatterChart(xAxis, yAxis);
scatterChart.setData(getChartData());
scatterChart.setTitle("speculations");
primaryStage.setTitle("ScatterChart example");
StackPane root = new StackPane();
root.getChildren().add(scatterChart);
primaryStage.setScene(new Scene(root, 400, 250));
primaryStage.show();
}
private ObservableList<XYChart.Series<String, Double>>getChartData() {
double javaValue = 17.56;
double cValue = 17.06;
double cppValue = 8.25;
ObservableList<XYChart.Series<String, Double>>answer = FXCollections.observableArrayList();
Series<String, Double> java = new Series<>();
Series<String, Double> c = new Series<>();
Series<String, Double> cpp = new Series<>();
java.setName("java");
c.setName("C");
cpp.setName("C++");
for (int i = 2011; i < 2021; i++) {
java.getData().add(new XYChart.Data(Integer.toString(i), javaValue));
javaValue = javaValue + 4 * Math.random() - .2;
 
Search WWH ::




Custom Search