-1

I have a line chart in my scene that shows several Series in it.each second one or two data is added to the series and is shown on chart. the problem is after a while that the program is running the data are being more and more and chart becomes almost useless.I want to show only last 50 data in the chart and older now shown anymore. The approach I had was to disable "Force Zero in the range" for XAxis and remove index-50 data from series in each data insertion .but I face with ConcurrentModificationException and indexOutofBoundaryException. Is there a better way for this problem? This is my code for chart and series and data insertion.

gxSeries = new XYChart.Series<Number, Number>();
 gySeries = new XYChart.Series<Number, Number>();
 gzSeries = new XYChart.Series<Number, Number>();
gxSeries.setName("p(Rad/s)");
gySeries.setName("q(Rad/s)");
gzSeries.setName("r(Rad/s)");
gSeriesChart.getData().add(gxSeries);
gSeriesChart.getData().add(gySeries);
gSeriesChart.getData().add(gzSeries);
    gSeriesChart.setCreateSymbols(false);
          gSeriesChart.getStyleClass().add("thick-chart");
    gSeriesX.setForceZeroInRange(false);

gxSeries,... are Series gSeriesChart is the lineChart gSeriesX is the xAxis

 gxSeries.getData().add(new XYChart.Data<Number, Number>(tele.missionTime/1000.0, tele.gx));
gySeries.getData().add(new XYChart.Data<Number, Number>(tele.missionTime/1000.0, tele.gy));
gzSeries.getData().add(new XYChart.Data<Number, Number>(tele.missionTime/1000.0, tele.gz));
a.ghaderi
  • 11
  • 3

1 Answers1

0

Check for the number of datasets in your collection and if you have 51 datasets in your collection, remove the oldest one. It should be feasible with something like yourSeries.getData().remove(0); The IOOB exception occurs because you have only indices from 0 to 49. The oldest value is the value with index 0. ‘hope that helps!

benji2505
  • 106
  • 1
  • 5
  • if I remove the indices zero, all other data will be shifted automatically one down"I mean after than one becomes zero and two becomes one ..." or I should do it manually ? what about the the concurrentModificationException? it seems because data is being add periodically when I remove a data this exception happens. – a.ghaderi Apr 12 '18 at 02:15
  • @a.ghaderi what happened when you tried it (with a very simple example, f.i. from the basic ui tutorial referenced in the info page of the javafx tag)? Don't forget: you are (or want to become) a developer and will spend a large portion of your working time with finding solutions to problems - a successfull strategy is to go back to the very basics (which you know are working) and learn how to implement what you want to achieve with that simple example. If you do, you'll know the answer to "will they be shifted" :) – kleopatra Apr 12 '18 at 10:01
  • And please read the api doc of ConcurrentModificationException ... it will at least give you the reason which then you have to find/apply in/to your context. Hint: nodes in the scenegraph must be modified on the fx-application thread – kleopatra Apr 12 '18 at 10:06
  • the ConcurrentModificationException appears because you are removing the dataset from the collection incorrectly. Please check this: https://stackoverflow.com/questions/223918/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re – benji2505 Apr 12 '18 at 12:23