使用任何JavaFX图表时出现以下问题: 我动态地将数据添加到图表中,只显示最后一个X轴标签。
我已经注意到,当动画被禁用时,图表会正常显示。
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);
答案 0 :(得分:5)
禁用动画对我有效。
sacCashflows.setAnimated(false);
我知道你在评论中说过你已经尝试过但它没有用,但也许对于那些遇到同样问题的人来说。
答案 1 :(得分:2)
像这样更改您的代码
xAxis1.setAnimated(false);
yAxis1.setAnimated(true);
barChart.setAnimated(true);
答案 2 :(得分:0)
让我们尝试使用示例代码(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();
}
答案 3 :(得分:0)
以下是针对此错误的快速解决方法:
chartVariable.getData().add(new XYChart.Series(FXCollections.observableArrayList(new XYChart.Data("",0))));
chartVariable.getData().clear();
在初始化图表时,添加虚假数据然后将其删除。这是有效的,因为错误在第一次更新/更改到图表后得到解决。将动画设置为false也有效,但我喜欢动画。