Mapping JFreeChart Series Name to Series Index
Asked Answered
K

1

6

I'm plotting a TimeTableXYDataset using a StackedXYBarRenderer. Unfortunately the colours of each series change on refresh.

I know how to set colours using the setSeriesPaint method of the renderer, but that takes an integer series index as the argument. I create my datapoints using a string as the series name:

ds.add(new SimpleTimePeriod(us.getDate(), 
                            new Date(us.getDate().getTime() + 1000*60)),
       us.getTotal(), us.getName()));

How do I discover the mapping between series name and series index so I can call setSeriesPaint?

Kiddy answered 21/6, 2010 at 7:44 Comment(0)
A
6

The easiest approach is to update a suitable Map as the data accumulates. Alternatively, the methods getSeriesKey() and indexOf() may be used to convert in either direction. For example,

for (int i = 0; i < ds.getSeriesCount(); i++) {
    String name = (String) ds.getSeriesKey(i);
    System.out.println(ds.indexOf(name) + ": " + name);
}
Array answered 21/6, 2010 at 18:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.