3

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.

TheGaME
  • 443
  • 2
  • 8
  • 21
  • @trashgod added expected output for legend title alignment and title position. – TheGaME Oct 18 '19 at 18:06
  • See also this [example](https://stackoverflow.com/a/13309587/230513) and maybe check the [demo](http://www.jfree.org/jfreechart/samples.html). – trashgod Oct 19 '19 at 08:22
  • @trashgod the example shows adding legend as separate panel which is not my use case. In my question, the first image shows legend within the chart but the title legend as such is not displayed on top of legend container. This is my issue. – TheGaME Oct 30 '19 at 18:42
  • Right; the one in the demo looks more like your mockup; more [here](https://stackoverflow.com/q/7684796/230513). – trashgod Oct 31 '19 at 08:38
  • @trashgod Since, legend title is a TextTitle instance. Can we rotate it and also change its X and Y position? Is it possible? – TheGaME Nov 01 '19 at 14:36
  • I don't know about rotate; it looks like you want to change the `Arrangement` of the legend's enclosing `BlockContainer`. – trashgod Nov 01 '19 at 15:10
  • @trashgod do you have an example that I can use to try out the way I want? – TheGaME Nov 02 '19 at 14:15
  • Just the link I updated [here](https://stackoverflow.com/a/7686791/230513); follow it through the `LegendTitle` constructor. – trashgod Nov 02 '19 at 19:48
  • @trashgod I updated my code based on an example which demonstrated how to add custom legend title using `BlockContainer` interface. This example helped me fix the legend title issue. – TheGaME Nov 12 '19 at 22:06

1 Answers1

2

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

TheGaME
  • 443
  • 2
  • 8
  • 21