问题
i want to display the cpu usage in a line chart. The Problem it, that if there are to many entrys i becomes confused. I tried to delete the first entry after X entrys but then the chart is not fully used. I want, that the line chart is filled with data but from begining to end.
Example with many entrys and delet the first one
Here is some example code:
This is the Controller, where i create the series and start the Thread to update my data.
public class FXMLDocumentController implements Initializable {
@FXML private LineChart chart;
XYChart.Series series = new XYChart.Series();
@Override
public void initialize(URL url, ResourceBundle rb) {
chart.getData().add(series);
ThreadTest updateChart = new ThreadTest(series);
Thread test = new Thread(updateChart);
test.start();
}
}
Here is the thread to update the data and after more then 10 entrys have been added i remove the first one, so that there are not to many entrys on the chart. I tried also to move after remove the first one to get the rest of the entrys and set them to 0-X so that there is always an first one. But i think the series do this autmaticly but i dont get an exception if i try to remove the first item over and over again.
public class ThreadTest extends Task<Void>{
private int chartCounter;
private XYChart.Series series;
private boolean running=true;
public ThreadTest(XYChart.Series series){
this.series=series;
}
@Override
protected Void call() throws Exception {
while(running){
series.getData().add(new XYChart.Data(chartCounter, (int)(Math.random() * 10)));
System.out.println("Test");
chartCounter++;
if(series.getData().size()>10){
series.getData().remove(0);
}
Thread.sleep(1000);
}
return null;
}
}
Thanks for help!!
来源:https://stackoverflow.com/questions/32291239/javafx-linechart-with-many-entrys