9

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!

Heisenbug
  • 154
  • 2
  • 14
  • 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 data](http://stackoverflow.com/questions/30052685/recreate-bar-chart-without-it-remembering-data) – Uluk Biy Aug 03 '15 at 04:23
  • 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 Aug 04 '15 at 12:32
  • What about disabling animation before adding the data and enabling it there after. – Uluk Biy Aug 04 '15 at 13:10
  • 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 Aug 04 '15 at 14:45
  • 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 Aug 05 '15 at 04:41

4 Answers4

6

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.

chanklor
  • 487
  • 5
  • 11
  • Wanted to point out that I had a similar problem with rendering charts to png images using `scene.snapshot()` and `SwingFXUtils.fromFXImage()`. Axis values weren't showing up until I disabled axis animations using this method. – NanoWizard Jul 29 '19 at 14:05
5

change your code like this

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

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();
}
Uluk Biy
  • 48,655
  • 13
  • 146
  • 153
  • If i use this code inside my function, i also only get the last Period label. – Heisenbug Aug 05 '15 at 09:56
  • Only after clicking the add Button the second time. After clicking it the first time the axis is still wrong. This is the same with my code. If i add something later during programm execution the axis gets updated correctly – Heisenbug Aug 05 '15 at 11:14
  • @Heisenbug what is your JavaFX version and OS? – Uluk Biy Aug 05 '15 at 11:19
  • javafx.runtime.version: 8.0.51-b16 JDK Version: 1.8.0_51 – Heisenbug Aug 05 '15 at 13:18
  • @Heisenbug I tested it with 8.0.51 on Ubuntu and working as expected. Try to provide a MVCE of your use case in your question. – Uluk Biy Aug 07 '15 at 04:24
0

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.

NonlinearFruit
  • 2,509
  • 1
  • 25
  • 27