In a BarChart, the x-axis has to be categorical (and a String).
The relative heights of each category is plotted for all series. We make class changes and a few name changes.
package ex84;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Ex84 extends Application {
ObservableList<XYChart.Series<String, Double>> obList
= FXCollections.observableArrayList();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
CategoryAxis xAxis = new CategoryAxis();
NumberAxis yAxis = new NumberAxis();
initData();
BarChart barChart = new BarChart(xAxis, yAxis, obList);
barChart.setTitle("A bar plot");
StackPane root = new StackPane();
root.getChildren().add(barChart);
primaryStage.setTitle("Example 84. BarChart");
primaryStage.setScene(new Scene(root, 400, 250));
primaryStage.show();
}
private void initData() {
Series series1 = new Series<>();
Series series2 = new Series<>();
series1.getData().addAll(
new XYChart.Data("0", 5),
new XYChart.Data("1", 3),
new XYChart.Data("2", 2)
);
series1.setName("series 1");
series2.getData().addAll(
new XYChart.Data("0", 2),
new XYChart.Data("1", 4),
new XYChart.Data("2", 7)
);
series2.setName("series 2");
obList.addAll(series1, series2);
}
}
This is the output:
No comments:
Post a Comment