1

I've created a BoxAndWhiskerRenderer plot using JFreeChart and it seems that it is automatically creating a sort of legend (see attached picutre).

enter image description here

Is there a way to remove the outside border of this legend and customize the font of the labels in the legend items?

Here is an example of my code:

//Get the desired BoxAndWhiskerCategoryDataset from a LinkedHashMap
BoxAndWhiskerCategoryDataset dataset = values.get(b);

//Create X axis
CategoryAxis xAxis = new CategoryAxis();
xAxis.setAxisLineVisible(false);

//Create Y axis
NumberAxis yAxis = new NumberAxis(b.getLabel());
yAxis.setAxisLineVisible(false);
yAxis.setTickLabelFont(FDFont.getFont(12f));
yAxis.setLabelFont(FDFont.getFont());
yAxis.setLabelPaint(FDPalette.secondaryText);
yAxis.setTickLabelPaint(FDPalette.secondaryText);
yAxis.setAutoRangeIncludesZero(false);

//Create renderer
BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer();
int count = 0;
for(Map.Entry<Integer,Color> map : clusterColor.entrySet()){
    //Set color for the series (I have a previously created map which links colors and series)
    renderer.setSeriesPaint(count,map.getValue());
    count++;
}
renderer.setFillBox(true);
renderer.setToolTipGenerator(new BoxAndWhiskerToolTipGenerator());
CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);

JFreeChart chart = new JFreeChart(plot);
chart.setBackgroundPaint(white);
chart.setBorderVisible(false);

ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(300, 270));
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Roberto
  • 243
  • 1
  • 5
  • 15
  • Please edit your question to include a [mcve] that shows your current approach, for [example](http://stackoverflow.com/q/6844759/230513). – trashgod Jul 02 '16 at 17:24

1 Answers1

2

As shown here, the simplest JFreeChart constructor adds a legend by default. The code for doing so is seen here; simply substitute your desired frame, color and position.

Starting from this example, the following changes produce the chart shown below:

private void createChartPanel() {
    …
    JFreeChart chart = new JFreeChart("BoxAndWhiskerDemo", plot);
    LegendTitle legend = chart.getLegend();
    legend.setFrame(new LineBorder(Color.white, new BasicStroke(1.0f),
        new RectangleInsets(1.0, 1.0, 1.0, 1.0)));
    legend.setItemFont(legend.getItemFont().deriveFont(16f));
    chartPanel = new ChartPanel(chart);
}

image

Compare to this default legend:

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • thanks for your suggestion. I'm having problems in testing the examples you posted. E.g. If I try with `LegendTitle legend = chart.getLegend();`, it says `Type mismatch: cannot convert from Legend to LegendTitle` and I'm not allowed to cast `Legend` to `LegendTitle`. I edited my previous post including an example of my code. – Roberto Jul 04 '16 at 08:21
  • In what way are you _not allowed to cast_? Please edit your question to include a [mcve] that shows your current approach to calling `getLegend()`; you an skip the custom fonts & colors for this. – trashgod Jul 04 '16 at 14:26
  • If I write `LegendTitle legend = (LegendTitle)chart.getLegend();` it throws a compilation error: `Cannot cast from Legend to LegendTitle` – Roberto Jul 04 '16 at 14:31
  • I can't reproduce this. Are you adding other legends? Without your [mcve] I can only guess. – trashgod Jul 04 '16 at 14:36
  • My Minimal, Complete, and Verifiable example is in the post above. Probably, the example cannot be compiled because of the `BoxAndWhiskerCategoryDataset` and the color of the series, but I think they don't affect my problem. I'm not adding any legend (it is automatically added) nor doing anything but what is in the code above. – Roberto Jul 04 '16 at 14:43
  • I've illustrated a complete example above. – trashgod Jul 04 '16 at 15:12
  • 1
    Thank you very much. The problem was that my version of JFreeChart was not updated to the latest release. Now your code perfectly works. – Roberto Jul 04 '16 at 15:37