如何自定义BoxAndWhisker图的图例

时间:2016-07-01 16:07:08

标签: java user-interface jfreechart

我使用JFreeChart创建了一个BoxAndWhiskerRenderer地块,似乎它会自动创建一种传奇(见附图)。

enter image description here

有没有办法删除此图例的外边框并自定义图例项目中标签的字体?

以下是我的代码示例:

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

1 个答案:

答案 0 :(得分:2)

如图所示here,最简单的JFreeChart构造函数默认添加图例。这样做的代码是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);
}

image

与此默认图例比较:

image