10
votes

I have the following problem when using any JavaFX Chart: I dynamically add data to the chart and only the last X-Axis label shows up.

I already noticed that the chart is displayed fine when animations are disabled.

    XYChart.Series<String,Double> series1= new Series<String, Double>();
    series1.setName(scenario1.getName());

    XYChart.Series<String,Double> series2= new Series<String, Double>();
    series2.setName(scenario2.getName());


    for(int period = 0; period < config1.getPeriods(); period++){
        series1.getData().add(new Data<String, Double>("Period "+(period+1), rmList1.get(0).getCashflowsPerPeriod(config1)[period]));
        System.out.println("Series1: "+rmList1.get(0).getCashflowsPerPeriod(config1)[period]);
    }


    for(int period = 0; period < config2.getPeriods(); period++){
        series2.getData().add(new Data<String, Double>("Period "+(period+1), rmList2.get(0).getCashflowsPerPeriod(config2)[period]));
        System.out.println("Series2: "+rmList2.get(0).getCashflowsPerPeriod(config2)[period]);
    }

    sacCashflows.getData().addAll(series1,series2);

Chart with missing axis labels Can you help me out here? Thank you!

4
I recall that there was problem showing the data when the animation is enabled. Try to do sacCashflows.layout(); after adding the data. See Recreate bar chart without it remembering dataUluk Biy
I already tried that, but this makes it only worse: If i call .layout() after adding the data, the Y-Axis is scaled wrong and the labels are still missing.Heisenbug
What about disabling animation before adding the data and enabling it there after.Uluk Biy
I also tried that. If i set animated to false at the beginning of the method posted above and set it to true at the end of the method it still looks like in the picture. The animation is playing aswell.Heisenbug
At this point it is better to provide some code that demonstrates the problem. Since you didn't it. I posted an example code which works without problem. Include on it your use case.Uluk Biy

4 Answers

7
votes

Disabling the animation worked for me.

sacCashflows.setAnimated(false);

I know you said in the comments that you had already tried that and it hadn't worked, but maybe for someone else having the same problem it will.

5
votes

change your code like this

    xAxis1.setAnimated(false);
    yAxis1.setAnimated(true);
    barChart.setAnimated(true);
0
votes

Let's try with sample code (JavaFX-8-b40):

@Override
public void start( Stage stage )
{

    CategoryAxis xAxis = new CategoryAxis();
    NumberAxis yAxis = new NumberAxis();

    AreaChart<String, Number> sacCashflows = new AreaChart<>( xAxis, yAxis );

    Button b = new Button( "Add" );
    b.setOnAction( new EventHandler<ActionEvent>()
    {
        @Override
        public void handle( ActionEvent event )
        {
            XYChart.Series<String, Number> series1 = new XYChart.Series<>();
            series1.setName( "series1" );

            XYChart.Series<String, Number> series2 = new XYChart.Series<>();
            series2.setName( "series2" );

            for ( int period = 0; period < 10; period++ )
            {
                series1.getData().add( new XYChart.Data<>( "Period " + (period + 1), 5.0 * period ) );
            }

            for ( int period = 0; period < 5; period++ )
            {
                series2.getData().add( new XYChart.Data<>( "Period " + (period + 1), 10.0 * period ) );
            }

            sacCashflows.getData().addAll( series1, series2 );

        }
    } );

    final Scene scene = new Scene( new VBox( sacCashflows, b ), 400, 300 );
    stage.setScene( scene );
    stage.show();
}
0
votes

Here is a quick-n-dirty fix for this bug:

chartVariable.getData().add(new XYChart.Series(FXCollections.observableArrayList(new XYChart.Data("",0))));
chartVariable.getData().clear();

While initializing the chart, add fake data and then remove it. This works because the bug gets resolved after the first update/change to the chart. Setting animation to false also works, but I like the animations.