Search code examples
jfreechart

Remove legend border of rectangle in piechart


I am trying to set the legend item shape as rectangular using the below code. But I always see a black border around the rectangle. How do I remove it. I tried few things like setting legend border to zero, setting plot setOutlineVisible to false, but none worked

plot.setLegendItemShape(new Rectangle(8,6));

Full customizer code used within Jaspersoft Studio

public class PieCustomizer extends JRAbstractChartCustomizer{
public void customize(JFreeChart chart, JRChart jasperChart){

    PiePlot plot = (PiePlot) chart.getPlot();

    plot.setShadowXOffset(0);
    plot.setShadowYOffset(0);

    plot.setLegendItemShape(new Rectangle(8,6));
    LegendTitle legend = chart.getLegend();
    legend.setFrame(BlockBorder.NONE);
    legend.setPosition(RectangleEdge.LEFT);
    legend.setVerticalAlignment(VerticalAlignment.BOTTOM);

    plot.setSectionOutlinesVisible(false);
    plot.setDirection(Rotation.ANTICLOCKWISE);
    plot.setLabelLinkPaint(new Color(103,103,103));
    plot.setLabelOutlinePaint(Color.BLUE);
    plot.setLabelShadowPaint(Color.WHITE);
    }
}

Screenshot (can't paste inline image due to insufficient reputation points)


Solution

  • The rectangles are drawn using the sectionOutlinePaint and sectionOutlineStroke.

    If you wish to make them invisible, you can set the following for all keys in the dataset:

    plot.setSectionOutlinePaint(key, Color.WHITE);
    plot.setSectionOutlineStroke(key ,new BasicStroke(0f));
    

    If you wish to remove the border around the legend box, you should set the border of the LegendTitle object:

    legend.setBorder(0, 0, 0, 0);