gpt4 book ai didi

java - 如何重新初始化 NumberAxis 并移动到 JavaFX LineChart 中的下一个值?

转载 作者:行者123 更新时间:2023-12-01 11:39:50 25 4
gpt4 key购买 nike

我目前正在尝试创建一个图表,它允许我重新启动 x 轴并继续绘图。坐标轴范围是0-100,但是当图表达到100时需要后面的值再次为0。但是使图表返回到初始的零。

在接下来的两张图片中,我展示了当前如何使用图表。

enter image description here

使图表返回到初始零并继续。 what makes the chart is to be returned to the initial zero.

我需要这样的东西: enter image description here

非常感谢您的帮助!!

最佳答案

您可以利用axis.setTickLabelFormatter()来格式化刻度标签:

public class TickLabelFormatterDemo extends Application
{

private static final int RANGE = 100;
private int last_X_Axis_Val = 20;


@Override
public void start( Stage stage )
{
stage.setTitle( "Sample" );

final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setForceZeroInRange( false);
xAxis.setTickLabelFormatter( new StringConverter<Number>()
{

@Override
public String toString( Number object )
{
int i = object.intValue() % RANGE;
return String.valueOf( i == 0 ? RANGE : i );
}

@Override
public Number fromString( String string )
{
return null;
}
} );

final LineChart<Number, Number> lineChart
= new LineChart<>( xAxis, yAxis );

lineChart.setTitle( "Monitoring" );
XYChart.Series series = new XYChart.Series();
series.setName( "Values" );

Random random = new Random();

Timeline timeline = new Timeline( new KeyFrame( Duration.seconds( 2 ), new EventHandler<ActionEvent>()
{
@Override
public void handle( ActionEvent event )
{
if ( series.getData().size() > 5 )
{
series.getData().remove( 0 );
}
series.getData().add( new XYChart.Data( last_X_Axis_Val, random.nextInt( 50 ) ) );
last_X_Axis_Val += 20;
}
} ) );
timeline.setCycleCount( Timeline.INDEFINITE );
timeline.play();

Scene scene = new Scene( lineChart, 800, 600 );
lineChart.getData().add( series );

stage.setScene( scene );
stage.show();
}


public static void main( String[] args )
{
launch( args );
}

}

关于java - 如何重新初始化 NumberAxis 并移动到 JavaFX LineChart 中的下一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29622202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com