简体   繁体   English

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

[英]axis.getCategories().remove(index number) doesn't work in JavaFX Chart

I want to design a real time linechart, which has a x-axis that shows datetime(hh:mm:ss). 我想设计一个实时折线图,它的x轴显示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. 日期显示在categoryaxis中,我想将类别计数限制为8。因此,时间过去时,它将仅显示8个时间标签。

In order to do that, i used xAxis.getCategories().remove(0) but it just doesn't work. 为了做到这一点,我使用了xAxis.getCategories().remove(0)但它根本不起作用。

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 .) (您可以通过在调用remove之后的if子句中放置另一个System.out.println(xAxis.getCategories().size());来看到此信息。)

The categories are essentially determined by the data that's present. categories基本上由当前数据确定。 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. 我认为应该应该编写getCategories()以返回不可修改的列表,但似乎并非如此,也许有一些很好的理由让我看不到。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM