Heisenbug
Heisenbug

Reputation: 154

JavaFX Chart Axis only shows last label

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!

Upvotes: 9

Views: 4099

Answers (4)

NonlinearFruit
NonlinearFruit

Reputation: 2579

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.

Upvotes: 0

Hosein Ghasemi
Hosein Ghasemi

Reputation: 51

change your code like this

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

Upvotes: 5

chanklor
chanklor

Reputation: 527

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.

Upvotes: 6

Uluk Biy
Uluk Biy

Reputation: 49185

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();
}

Upvotes: 0

Related Questions