Search code examples
javajfreechart

Changing series outline stroke and paint in scatter plot


I have a scatter plot created with ChartFactory.createScatterPlot. I would like to draw an outline around each point to get better visual feedback in regions with clustered points. I am trying it this way:

Shape cross  = new Ellipse2D.Double(0,0,5,5);
XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
xyPlot.setDomainCrosshairVisible(true);
xyPlot.setRangeCrosshairVisible(true)
XYItemRenderer renderer = xyPlot.getRenderer();
renderer.setSeriesShape(0, cross);
renderer.setSeriesPaint(0, Color.red);
renderer.setSeriesOutlinePaint(0, Color.black);
renderer.setSeriesOutlineStroke(0, new BasicStroke(2));
renderer.setSeriesStroke(0, new BasicStroke(1));

But only the new shape is drawn, the points get no outline:

enter image description here


Solution

  • The factory method cited instantiates XYLineAndShapeRenderer, so to see the change you need to invoke setUseOutlinePaint(), as well as setSeriesOutlinePaint() and (optionally) setSeriesOutlineStroke().

    XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) xyPlot.getRenderer();
    renderer.setUseOutlinePaint(true);
    renderer.setSeriesOutlinePaint(0, Color.black);
    renderer.setSeriesOutlineStroke(0, new BasicStroke(2));
    

    A complete example is shown here.