Java 如何自定义BoxAndWhisker绘图的图例

Java 如何自定义BoxAndWhisker绘图的图例,java,user-interface,jfreechart,Java,User Interface,Jfreechart,我已经使用JFreeChart创建了一个绘图,它似乎自动创建了一种图例(见附件) 是否有方法删除此图例的外部边框并自定义图例项中标签的字体 下面是我的代码示例: //Get the desired BoxAndWhiskerCategoryDataset from a LinkedHashMap BoxAndWhiskerCategoryDataset dataset = values.get(b); //Create X axis CategoryAxis xAxis = new Cate

我已经使用
JFreeChart
创建了一个绘图,它似乎自动创建了一种图例(见附件)

是否有方法删除此图例的外部边框并自定义图例项中标签的字体

下面是我的代码示例:

//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));
//从LinkedHashMap获取所需的BoxAndWhiskerCategory数据集
BoxAndWhiskerCategoryDataset=values.get(b);
//创建X轴
CategoryAxis xAxis=新CategoryAxis();
xAxis.setAxisLineVisible(false);
//创建Y轴
NumberAxis yAxis=新的NumberAxis(b.getLabel());
yAxis.setAxisLineVisible(false);
yAxis.setTickLabelFont(FDFont.getFont(12f));
setLabelFont(FDFont.getFont());
yAxis.setLabelPaint(FDPalette.secondaryText);
yAxis.setTickLabelPaint(FDPalette.secondaryText);
yAxis.SetAutoRangeIncludeZero(假);
//创建渲染器
BoxAndWhiskerRender渲染器=新的BoxAndWhiskerRender();
整数计数=0;
对于(Map.Entry映射:clusterColor.entrySet()){
//设置系列的颜色(我有一个以前创建的链接颜色和系列的地图)
renderer.setSeriesPaint(count,map.getValue());
计数++;
}
renderer.setFillBox(true);
renderer.setToolTipGenerator(新的boxAndWhisterToolTipGenerator());
CategoryPlot=新的CategoryPlot(数据集、xAxis、yAxis、渲染器);
JFreeChart图表=新JFreeChart(绘图);
图表.立根底色漆(白色);
图表.可见(假);
ChartPanel ChartPanel=新的ChartPanel(图表);
setPreferredSize(新java.awt.Dimension(300270));
如图所示,最简单的构造函数默认添加图例。可以看到这样做的代码;只需替换所需的框架、颜色和位置

从这一点开始,以下更改生成如下图表:

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

与此默认图例相比:


请编辑您的问题,包括一个显示您当前方法的问题。谢谢您的建议。我在测试您发布的示例时遇到问题。例如,如果我尝试使用
LegendTitle legend=chart.getLegend(),它说
类型不匹配:无法从图例转换为LegendTitle
,并且不允许我将
图例
转换为
LegendTitle
。我编辑了我的前一篇文章,其中包括我的代码示例。你不允许以什么方式进行转换?请编辑您的问题,以包括一个显示您当前调用
getLegend()
方法的问题;你可以跳过这个的自定义字体和颜色。如果我写
LegendTitle legend=(LegendTitle)chart.getLegend()它抛出一个编译错误:
无法从Legend转换为LegendTitle
我无法复制这个。你在添加其他传奇吗?没有你的帮助,我只能猜测。非常感谢。问题是我的JFreeChart版本没有更新到最新版本。现在,您的代码可以完美地工作了。