I'm not sure if this is possible but here we go. Is there a way to convert a string to a Dataset? Let me explain. I have a list of persons that i need to represent on a JFreeChart line-chart and I therefore need to create a dataset for each person. Can I create datasets (possibly named after each person) for each person that is in the list? Something like this...
public XYDataset createDatasets(List<String> personList) {
XYSeriesCollection seriesColletion = new XYSeriesCollection();
for(String person : personList) {
XYSeries person = new XYSeries(person);
seriesColletion.addseries(person);
}
return seriesColletion;
}
Or is there a smarter, better way of going about it? I'm sure this code doesn't work (particularly lines 3-6) but i'm only using it to better explain this.
The direct approach is to parse the values out of the String
and create an XYSeries
for each person. Then you can add multiple series to an XYSeriesCollection
, as shown in this related example.
Is there a smarter, better way of going about it?
Much depends on your application's data model. Assuming that you maintain a List<Person>
, let each Person
manage his or her own data internally; provide a convenience method that returns an XYSeries
on demand, e.g.
List<Person> personList = new ArrayList<>(…);
…
public XYDataset createDatasets(List<String> personList) {
XYSeriesCollection seriesColletion = new XYSeriesCollection();
for (Person person : personList) {
seriesColletion.addseries(person.getXYSeries());
}
return seriesColletion;
}