1

After creating a line chart by passing to JFreeChart constructor a XYSeriesCollection dataset, I'm trying to get either series Stroke/Paint/Shape as:

XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer)chart.getXYPlot().getRenderer();
for (int i = 0; i < dataset.getSeriesCount(); i++) {
     renderer.getSeriesStroke(i);
     renderer.getSeriesPaint(i);
     renderer.getSeriesShape(i);
}

but all return null.

Why is that? How can I get the non-null objects?

Line
  • 1,529
  • 3
  • 18
  • 42
heevmo
  • 15
  • 4

1 Answers1

2

An XYLineAndShapeRenderer is an XYItemRenderer, which supports "rendering the visual representation of a single (x, y) item on an XYPlot." Although the corresponding series properties are null, the item properties are accessible. Starting from this example, The following changes produce the output shown:

Code:

XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer)chart.getXYPlot().getRenderer();
for (int i = 0; i < xyPlot.getDataset().getSeriesCount(); i++) {
     System.out.println(renderer.getItemStroke(i, 0));
     System.out.println(renderer.getItemPaint(i, 0));
     System.out.println(renderer.getItemShape(i, 0));
     System.out.println(renderer.getItemShape(i, N));
}

Console:

$ java -cp .:$JFREE_LIB/* ScatterShape
java.awt.BasicStroke@d1a007c0
java.awt.Color[r=255,g=85,b=85]
java.awt.geom.Rectangle2D$Double[x=-3.0,y=-3.0,w=6.0,h=6.0]
java.awt.geom.GeneralPath@7ef51f0f

enter image description here

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • That's helpful, but I still don't understand why getSeriesPaint is null - I'm adding data to the chart using series, so I would expect them to have a color. – Line Jan 08 '19 at 17:08
  • 1
    See the abstract parent's `lookupSeriesPaint()` implementation for details. – trashgod Jan 08 '19 at 17:29