JFreeChart making XYSeriescollection iterable

137 views Asked by At

Background info

I wanted to create a quasi dynamic dataset containing few different data series that share the same timeframe to certain extent. Since I never know when a new series may be required or old one updated I'm making a method that takes a generic entry as name, time, score to plot it as a chart.

The actual problem

I have noticed that I can not iterate through XYSeriesCollection which would suggest the class itself does not implement iterable. I guess i could modify the original files to implement the interface, but I am a bit concerned as the JFreechart appears to be a great library so perhaps there is a good reason why the implementation was not done?

summary:

Is it OK to modify the code - is it safe, has anyone tried it, can it cause some issues within the library, etc?

Is there a better way to add the behaviour than editing the sources, an override of a kind?

What about different approach? I'm not adamant with my solution, however I can not alter the input data and have no way of knowing anything about it in advance except that it will arrive at some point and that it has the same format(since it's an object of a class I know)

1

There are 1 answers

1
trashgod On BEST ANSWER

XYSeriesCollection#getSeries() returns a List that is Iterable.

XYSeriesCollection dataset = new XYSeriesCollection(…);
List<XYSeries> list = dataset.getSeries();
for (XYSeries s : list) {
    //alter s as desired
}

Note that the Collection returned in unmodifiable, but you can alter the elements individually. Use the XYSeriesCollection API to alter the list itself.