So I have a javafx line chart in my program. The program generates a random value from 1-6 every second and plots it on the graph. So the X axis represents the time in seconds and the Y axis represents the random value chosen. How would I go about moving the graph on the x axis as time goes by? ie animating the graph going in the right direction.
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);
}
}
I think you should try setting:
xAxis.setForceZeroInRange(false);
And then remove data indexed 0 (first one) from your series when certain condition is met.
Here is some code that does something similar, I hope it helps.
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.