我想设计一个实时折线图,它的x轴显示datetime(hh:mm:ss)。日期显示在categoryaxis中,我想将类别计数限制为8。因此,时间过去时,它将仅显示8个时间标签。

为了做到这一点,我使用了xAxis.getCategories().remove(0),但是它不起作用。

那是我的控制器。

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.
       }

    }

}

最佳答案

它删除了类别,然后根据图表中显示的数据替换了类别。 (您可以通过在调用System.out.println(xAxis.getCategories().size());之后的if子句中放置另一个remove来查看此内容。)

categories基本上由存在的数据确定。我认为getCategories()应该应该写为返回不可修改的列表,但似乎并非如此,也许有一些很好的理由让我看不到。

您应该改为处理数据:

series.getData().add(new XYChart.Data<String, Number>(x, dakika));
if (series.getData().size() > 8) {
    series.getData().remove(0);
}

09-05 10:17