TheGaME
TheGaME

Reputation: 453

Legend Title not displayed correctly within chart

I created a Bar chart using JFreeChart API v1.5.0 and added Legend with Title to the chart.

I used the example from this Bar Chart Demo and added the following legend code to the example.

//Legend default properties
protected static final boolean LEGEND_ON = true;
private static final RectangleEdge LEGEND_POSITION = RectangleEdge.RIGHT;
private static final String LEGEND_TITLE_TEXT = "Legend";
private static final Font LEGEND_TITLE_FORMAT = new Font(Font.SANS_SERIF, Font.PLAIN, 12);
private static final Color LEGEND_TITLE_COLOR = Color.BLACK;
private static final double LEGEND_MAX_WIDTH = 0.0;
private static final Color LEGEND_BG_COLOR = Color.WHITE;
private static final Color LEGEND_BORDER_COLOR = Color.WHITE;
private static final Font LEGEND_TEXT_FORMAT = new Font(Font.SANS_SERIF, Font.PLAIN, 12);
private static final Color LEGEND_TEXT_COLOR = Color.BLACK;

LegendTitle legend = chart.getLegend();
legend.setPosition(LEGEND_POSITION);
legend.setWidth(LEGEND_MAX_WIDTH);
legend.setBackgroundPaint(LEGEND_BG_COLOR);
legend.setFrame(new BlockBorder(1, 1, 1, 1, LEGEND_BORDER_COLOR));
legend.setItemFont(LEGEND_TEXT_FORMAT);
legend.setItemPaint(LEGEND_TEXT_COLOR);

if (LEGEND_TITLE_TEXT != null) {
    TextTitle legendTitle = new TextTitle();
    legendTitle.setText(LEGEND_TITLE_TEXT);
    legendTitle.setPosition(LEGEND_POSITION);
    legendTitle.setPaint(LEGEND_TITLE_COLOR);
    legendTitle.setFont(LEGEND_TITLE_FORMAT);
    legendTitle.setHorizontalAlignment(HorizontalAlignment.CENTER);
    legendTitle.setVerticalAlignment(VerticalAlignment.CENTER);
    chart.addSubtitle(1, legendTitle);
}

I get the following output when I run the example along with my legend code above.

Legend Title not upright

I did notice that the legend title is not displayed correctly, as it is placed vertically instead of horizontally within the legend container on the right side(as shown in below image for Expected Output). I did try few steps such as using TextUtils.drawRotatedString() and TextUtils.drawAlignedString() but could not fix the legend title position and its alignment.

How to place the legend title on top of the legend container and align it horizontally?

I am looking for a similar output for legend title as shown below, which was generated using different chart framework.

Expected Output for legend title

I would like to align and position my legend title as shown in the above output.

Upvotes: 3

Views: 733

Answers (1)

TheGaME
TheGaME

Reputation: 453

Based on the example provided in this link, I modified the code to add custom legend using BlockContainer to get the desired output as shown in image 1 from the question above.

Updated Code:

//Legend default properties
pro tected static final boolean LEGEND_ON = true;
private static final RectangleEdge LEGEND_POSITION = RectangleEdge.RIGHT;
private static final String LEGEND_TITLE_TEXT = "Legend";
private static final Font LEGEND_TITLE_FORMAT = new Font(Font.SANS_SERIF, Font.PLAIN, 12);
private static final Color LEGEND_TITLE_COLOR = Color.BLACK;
private static final double LEGEND_MAX_WIDTH = 0.0;
private static final Color LEGEND_BG_COLOR = Color.WHITE;
private static final Color LEGEND_BORDER_COLOR = Color.WHITE;
private static final Font LEGEND_TEXT_FORMAT = new Font(Font.SANS_SERIF, Font.PLAIN, 12);
private static final Color LEGEND_TEXT_COLOR = Color.BLACK;

LegendTitle legend = chart.getLegend();
legend.setPosition(LEGEND_POSITION);
legend.setWidth(LEGEND_MAX_WIDTH);
legend.setBackgroundPaint(LEGEND_BG_COLOR);
legend.setFrame(new BlockBorder(1, 1, 1, 1, LEGEND_BORDER_COLOR));
legend.setItemFont(LEGEND_TEXT_FORMAT);
legend.setItemPaint(LEGEND_TEXT_COLOR);

if (LEGEND_TITLE_TEXT != null) {
    TextTitle legendTitle = new TextTitle();
    legendTitle.setText(LEGEND_TITLE_TEXT);
    legendTitle.setPosition(LEGEND_POSITION);
    legendTitle.setPaint(LEGEND_TITLE_COLOR);
    legendTitle.setFont(LEGEND_TITLE_FORMAT);
    legendTitle.setHorizontalAlignment(HorizontalAlignment.CENTER);
    legendTitle.setVerticalAlignment(VerticalAlignment.CENTER);

    BlockContainer legendCont = new BlockContainer(new ColumnArrangement());
    legendCont.add(legendTitle, RectangleEdge.TOP);
    BlockContainer items = legend.getItemContainer();
    legendCont.add(items);      
    legend.setWrapper(legendCont);
    //Remove existing legend to avoid duplicate legend display.
    chart.removeLegend();
    //show legend container with title and items
    chart.addSubtitle(legend);
}

After executing the above code, I got the following output.

Legend Title Position Upright

Upvotes: 2

Related Questions