ChaimKut
ChaimKut

Reputation: 2809

JFree Chart: Painting the correct data series

When I define my XYLineAndShapeRenderer (or for that matter any AbstractRenderer such as ScatterRenderer, LineAndShapeRenderer, XYAreaRenderer, etc.), I define the various characteristics of the drawn lines using the data series's index. For instance, setSeriesPaint, setSeriesStroke allow you to specify the index of the data series being characterized. But when I'm using a TimeTableXYDataset and adding data points, the API requests the 'seriesName' (defined as a Comparable). Huh? How does the XYLineAndShapeRenderer match between the series index (series #0, series #1) with seriesName (series "dogs", series "cats")? How can I be sure that series "dogs" will be drawn in blue and "cats" drawn in red (aside from trial and error). It seems like these two APIs are not well matched...

Upvotes: 0

Views: 131

Answers (1)

David Gilbert
David Gilbert

Reputation: 4477

TimeTableXYDataset will assign series indices (starting at 0) on an as-required basis, in the order that you add items to the dataset. So for example:

dataset.add(day1, 12.2, "Dogs");  // "Dogs" is series 0
dataset.add(day2, 13.1, "Dogs");
dataset.add(day1, 9.4, "Cats"); // "Cats" is series 1
dataset.add(day2, 8.3, "Cats");

TimeTableXYDataset is a specialised implementation of the XYDataset interface in JFreeChart that ensures that every series has a consistent set of x-values (that's not a general requirement of the XYDataset interface, which allows each series to have an independent set of x-values). This special property of the dataset is useful when creating stacked area charts, but the implementation has resulted in the API quirk that you've noted.

Upvotes: 2

Related Questions