axis.getCategories().remove(index number) 在 JavaFX 图表中不起作用



我想设计一个实时线形图,它的x轴显示日期时间(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.
       }
    }
}

它删除类别,但随后根据图表中显示的数据替换它。(您可以通过在if子句中放置另一个System.out.println(xAxis.getCategories().size());来看到这一点,就在对remove的调用之后。)

categories本质上是由存在的数据决定的。我认为getCategories()可能应该被写为返回一个不可修改的列表,但似乎它不是,也许有一些很好的原因,我没有看到。

您应该操作数据:

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

最新更新