如何重新启动NumberAxis并在JavaFX LineChart中移动到下一个值? [英] How to reinit the NumberAxis and move to next value in JavaFX LineChart?

查看:262
本文介绍了如何重新启动NumberAxis并在JavaFX LineChart中移动到下一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently trying to create a chart, which allows me to restart the x-axis and continue drawing. The axis range is 0-100, but when the graph reaches 100 need the following values is again 0. But what makes the chart is to be returned to the initial zero.

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

in the next two picture I show how to work the chart currently.

是什么使图表返回到初始零并继续。

what makes the chart is to be returned to the initial zero and continue.

我需要这样的东西:

I need something like this:

非常感谢你的帮助!!

Thank you very much for your help!!

推荐答案

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

You can utilize the axis.setTickLabelFormatter() to format tick labels:

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 );
    }

}

这篇关于如何重新启动NumberAxis并在JavaFX LineChart中移动到下一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆