Anshu
Anshu

Reputation: 295

Reduce space between legend and chart in jfree Pie chart

I placed the legend in the top left corner and there is an unnecessary gap between the chart and the legend. How to reduce this?

There is considerable amount of space between pie chart and its border.

public void setPie() {
    RingPlot plot = (RingPlot) this.chart.getPlot();

    plot.setSectionPaint("", WHITE_COLOR);
    plot.setCircular(true);
    plot.setShadowPaint(null);
}

public void setLegend() {
    LegendTitle legend = this.chart.getLegend();
    legend.setVerticalAlignment(VerticalAlignment.TOP);
    legend.setVisible(true);
    legend.setPosition(RectangleEdge.LEFT);

    Rectangle rect = new Rectangle(LEGEND_LENGTH, LEGEND_WIDTH);
    RingPlot plot = (RingPlot) this.chart.getPlot();
    plot.setLegendItemShape((Shape) rect);
}

Upvotes: 0

Views: 933

Answers (1)

Eric Leibenguth
Eric Leibenguth

Reputation: 4277

You could try tweaking the margins around the LegendTitle. Something like:

chart.getLegend().setMargin(top, left, bottom, right);

EDIT: As suggested in the post in the comments, you could try to draw the legend manually at a given position:

//Print legend at X & Y location 
Rectangle2D r2DD = new Rectangle2D.Double(119, 47, 120, 180);
LegendTitle legend = new LegendTitle(chart.getPlot());
legendTitle.draw(g2,r2DD);

But I'm not sure how to implement it, as you need access to the ChartPanel's Graphics2D. Perhaps by extending ChartPanel and overriding paintComponent(Graphics g) ?

Upvotes: 4

Related Questions