JavaFX沿折线图的轴移动

时间:2016-03-20 03:18:25

标签: java javafx linechart

所以我的程序中有一个javafx折线图。程序每秒生成一个1-6的随机值,并在图表上绘制。因此,X轴表示以秒为单位的时间,Y轴表示所选的随机值。随着时间的推移,我将如何在x轴上移动图形?即使图表朝正确的方向动画。

public class MainDisplayController implements Initializable {
LineChart<String,Number> lineChart;

Timer timer = new Timer();
TimerTask task;
Random dice = new Random();

@Override
public void initialize(URL arg0, ResourceBundle arg1) {


    XYChart.Series<String,Number> series = new XYChart.Series<String,Number>();



    series.getData().add(new XYChart.Data<String,Number>("1",0));

    lineChart.getData().add(series);

    task = new TimerTask(){
        int secondsPassed = 0;
        @Override
        public void run() {
            secondsPassed++;
            System.out.println(secondsPassed);
            int number;
            number = 1+dice.nextInt(6);
            series.getData().add(new XYChart.Data<String,Number>(String.valueOf(secondsPassed),number));
        }

    };

    timer.scheduleAtFixedRate(task, 1000, 1000);
}

}

1 个答案:

答案 0 :(得分:0)

我认为你应该尝试设置:

xAxis.setForceZeroInRange(false);

然后在满足特定条件时从系列中删除索引为0(第一个)的数据。

这是一些类似的代码,我希望它有所帮助。

public class PointEverySecondLineChart extends Application {
        private BorderPane root = new BorderPane();
        private Scene scene = new Scene(root,800,600);

        private Random rand = new Random();

        private SimpleIntegerProperty
                lastX = new SimpleIntegerProperty(0);
        private XYChart.Series <Integer,Integer> mySeries;
        private NumberAxis 
                xAxis = new NumberAxis(),
                yAxis = new NumberAxis();
        private LineChart lineChart = new LineChart<>(xAxis,yAxis);    

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

        @Override
        public void start(Stage primaryStage) {
            Timeline addPointEverySecond = new Timeline(new KeyFrame(Duration.millis(500),event->{
               lastX.set(lastX.add(1).get());
               mySeries.getData().add(new XYChart.Data<>(lastX.get(), rand.nextInt(100)));
               if (mySeries.getData().size()>10) mySeries.getData().remove(0);
            }));
            addPointEverySecond.setCycleCount(Timeline.INDEFINITE);
            ObservableList <XYChart.Series<Integer,Integer>> data = FXCollections.observableArrayList();

            LineChart<Integer,Integer> chart = makeLineChart(data);
            chart.setPrefWidth(600);
            chart.setPrefHeight(600);
            root.setCenter(chart);

            Button btGo = new Button("GO!");
            btGo.setOnAction(action -> {
                mySeries = new XYChart.Series<>();
                data.add(mySeries);
                lastX.set(0);
                addPointEverySecond.playFromStart();
            });

            btGo.disableProperty().bind(addPointEverySecond.statusProperty().isEqualTo(Animation.Status.RUNNING));
            root.setBottom(btGo);        

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

        LineChart<Integer, Integer> makeLineChart(ObservableList <XYChart.Series<Integer,Integer>> series) {
            xAxis.setForceZeroInRange(false);
            lineChart.setCreateSymbols(false);
            lineChart.setData(series);
            return lineChart;
        }

    }