如何自定义盒须图的图例

How to customize legend of a BoxAndWhisker plot

我使用 JFreeChart 创建了一个 BoxAndWhiskerRenderer 图,它似乎自动创建了一种图例(见附图)。

有没有办法去除这个图例的外边框并自定义图例项中标签的字体?

这是我的代码示例:

//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));

如图here, the simplest JFreeChart constructor adds a legend by default. The code for doing so is seen here;只需替换您想要的框架、颜色和位置。

从此example开始,进行以下更改生成如下所示的图表:

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);
}

与此默认图例比较: