gpt4 book ai didi

java - 如何自定义 BoxAndWhisker 图的图例

转载 作者:行者123 更新时间:2023-11-30 02:52:46 26 4
gpt4 key购买 nike

我创建了一个 BoxAndWhiskerRenderer使用 JFreeChart 进行绘图,它似乎会自动创建一种图例(请参阅附图)。

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

最佳答案

如图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

关于java - 如何自定义 BoxAndWhisker 图的图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38149759/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com