This discussion is archived
2 Replies Latest reply: Nov 30, 2012 2:21 AM by 976950 RSS

How do i create a StackedAreaChart with an CategoryAxis (xAxis)?

976950 Newbie
Currently Being Moderated
Hello at all,

while it's no problem to create an AreaChart with an CategoryAxis i wonder why i cant create an StackedAreaChart with an CategoryAxis.

When executing this Code i am getting and java.lang.ClassCastException (java.lang.Double cannot be cast to java.lang.String) in the last Line "primaryStage.show();"

I would be thankfull for your suggestions.
public class JavaFXCharts extends Application {

    final ObservableList<String> categories2 = FXCollections.observableArrayList("1", "3","6", "9","12","15","18","21","24","27","30","31");
    final CategoryAxis xAxis = new CategoryAxis(categories2);
    final NumberAxis yAxis = new NumberAxis();
    final StackedAreaChart<String, Number> sac =
            new StackedAreaChart<>(xAxis, yAxis);    
    
    
    @Override
    public void start(Stage primaryStage) {

        primaryStage.setTitle("Area Chart Sample");
        sac.setTitle("Temperature Monitoring (in Degrees C)");
        XYChart.Series<String, Number> seriesApril =
                new XYChart.Series<>();
        seriesApril.setName("April");
        seriesApril.getData().add(new XYChart.Data("1", 4));
        seriesApril.getData().add(new XYChart.Data("3", 10));
        seriesApril.getData().add(new XYChart.Data("6", 15));
        seriesApril.getData().add(new XYChart.Data("9", 8));
        seriesApril.getData().add(new XYChart.Data("12", 5));
        seriesApril.getData().add(new XYChart.Data("15", 18));
        seriesApril.getData().add(new XYChart.Data("18", 15));
        seriesApril.getData().add(new XYChart.Data("21", 13));
        seriesApril.getData().add(new XYChart.Data("24", 19));
        seriesApril.getData().add(new XYChart.Data("27", 21));
        seriesApril.getData().add(new XYChart.Data("30", 21));
        XYChart.Series<String, Number> seriesMay =
                new XYChart.Series<>();
        seriesMay.setName("May");
        seriesMay.getData().add(new XYChart.Data("1", 20));
        seriesMay.getData().add(new XYChart.Data("3", 15));
        seriesMay.getData().add(new XYChart.Data("6", 13));
        seriesMay.getData().add(new XYChart.Data("9", 12));
        seriesMay.getData().add(new XYChart.Data("12", 14));
        seriesMay.getData().add(new XYChart.Data("15", 18));
        seriesMay.getData().add(new XYChart.Data("18", 25));
        seriesMay.getData().add(new XYChart.Data("21", 25));
        seriesMay.getData().add(new XYChart.Data("24", 23));
        seriesMay.getData().add(new XYChart.Data("27", 26));
        seriesMay.getData().add(new XYChart.Data("31", 26));
        Scene scene = new Scene(sac, 800, 600);
        sac.getData().addAll(seriesApril, seriesMay);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
Greetings
Thomas

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points