簡體   English   中英

如何在JavaFX LineChart中重新初始化NumberAxis並移至下一個值?

[英]How to reinit the NumberAxis and move to next value in JavaFX LineChart?

我目前正在嘗試創建一個圖表,該圖表可讓我重新啟動x軸並繼續繪制。 軸范圍是0-100,但是當圖表達到100時,需要再次將以下值設為0。但是,使圖表返回初始零的原因是什么。

在接下來的兩張圖片中,我顯示了當前如何處理圖表。

在此處輸入圖片說明

是什么使圖表返回到初始零並繼續。 是什么使圖表返回到初始零。

我需要這樣的東西: 在此處輸入圖片說明

非常感謝您的幫助!!

您可以利用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 );
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM