JavaFX chart label shows only 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 appears.

I already noticed that the chart displays fine when the animation is turned off.

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 Could you help me? Thanks!

+5
source share
4 answers

Disabling animations worked for me.

 sacCashflows.setAnimated(false); 

I know what you said in the comments that you have already tried this, and it did not work, but maybe for someone else who has the same problem.

+4
source

change your code as follows

  xAxis1.setAnimated(false); yAxis1.setAnimated(true); barChart.setAnimated(true); 
+1
source

Try the 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
source

Here is a short fix for this error:

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

During the initialization of the chart, add fake data and then delete it. This works because the error is fixed after the first update / change of the chart. Setting animations to false also works, but I like animations.

0
source

All Articles