I want to design a real time linechart, which has a x-axis that shows datetime(hh:mm:ss). Date is shown in categoryaxis, and i want to limit category count to 8. So when time passes, it will only show 8 time labels.
In order to do that, i used xAxis.getCategories().remove(0)
but it just doesn't work.
That's my controller.
package prjlm;
import java.net.URL;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ResourceBundle;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.util.Duration;
/**
* FXML Controller class
*
* @author aloneL
*/
public class OnlineChartController implements Initializable {
private String TITLE = "deneme";
private String Y_EKSEN = "yekseni";
private String X_EKSEN = "xekseni";
private Timeline animation;
@FXML
private CategoryAxis xAxis;
@FXML
private NumberAxis yAxis;
XYChart.Series<String, Number> series;
SimpleDateFormat dateFormat;
int saat=19;
int dakika=56;
Date date;
String x;
/**
* Initializes the controller class.
*
* @param url
* @param rb
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
init();
// TODO
}
@FXML
private LineChart chartOnline;
private void init() {
dateFormat = new SimpleDateFormat("HH:mm:ss");
chartOnline.setTitle(TITLE);
xAxis.setLabel(X_EKSEN);
yAxis.setLabel(Y_EKSEN);
series = new XYChart.Series<String, Number>();
series.setName("Person Num");
series.getData().add(new XYChart.Data<String, Number>("19:50", 50));
series.getData().add(new XYChart.Data<String, Number>("19:51", 80));
series.getData().add(new XYChart.Data<String, Number>("19:52", 90));
series.getData().add(new XYChart.Data<String, Number>("19:53", 30));
series.getData().add(new XYChart.Data<String, Number>("19:54", 122));
series.getData().add(new XYChart.Data<String, Number>("19:55", 10));
chartOnline.getData().add(series);
animation = new Timeline();
animation.getKeyFrames().add(new KeyFrame(Duration.millis(1000), new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent actionEvent) {
nextTime();
plotTime();
}
}));
animation.setCycleCount(Animation.INDEFINITE);
animation.play();
}
public void nextTime()
{
dakika++;
saat++;
}
public void plotTime()
{
date=new Date();
date.setTime(date.getTime());
x=dateFormat.format(date);
series.getData().add(new XYChart.Data<String, Number>(x, dakika));
System.out.println(xAxis.getCategories().size()); //it increases once a second.
System.out.println( xAxis.getCategories().get(0)); //always shows "19:50"
if(xAxis.getCategories().size()>8){
xAxis.getCategories().remove(0); //program runs this line but nothing happens.
}
}
}
It removes the category, but then replaces it according to the data that's displayed in the chart. (You can see this by putting another System.out.println(xAxis.getCategories().size());
in the if
clause, right after the call to remove
.)
The categories
are essentially determined by the data that's present. I think getCategories()
should probably have been written to return an unmodifiable list, but it seems it wasn't, and maybe there's some good reason for this I'm not seeing.
You should instead manipulate the data:
series.getData().add(new XYChart.Data<String, Number>(x, dakika));
if (series.getData().size() > 8) {
series.getData().remove(0);
}
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.