JFreechart ChartPanel无法透明化

7

我希望将图表背景设为半透明。这是我的代码。我添加了一些代码以使其半透明,但我猜 ChartPanel 没有变成半透明。在写入这些代码后,图表背景变为灰色。

JFreeChart chart = ChartFactory.createPieChart3D(
    "Full traffic view", pieDataset, true, true, true);

PiePlot3D p = (PiePlot3D) chart.getPlot();

PieRenderer renderer = new PieRenderer(sampleColors);
renderer.setColor(p, pieDataset);
p.setDepthFactor(0.07);
p.setCircular(true);
p.setLabelOutlinePaint(null);
p.setLabelBackgroundPaint(null);
p.setLabelShadowPaint(null);

p.setBackgroundPaint(new Color(127, 127, 127, 64));  // tranparency code
p.setBackgroundImageAlpha(0.0f);

p.setSimpleLabels(true);
p.setLabelGenerator(null);
p.setBackgroundPaint(
new GradientPaint(0, 0, Color.white, 0, 100, Color.white));
p.setDarkerSides(true);
ChartPanel frame1 = new ChartPanel(chart);
ChartPanel.setVisible(true);
ChartPanel.add(frame1);

ChartPanel.setSize(640, 400);

没有您提供 sscce,我们只能猜测为什么会出现问题。 - trashgod
3个回答

6
我发现我必须同时为图表和绘图使用透明颜色。
val trans = new Color(0xFF, 0xFF, 0xFF, 0)
chart.setBackgroundPaint(trans)
plot .setBackgroundPaint(trans)

它可以工作了,但我还需要在图例上设置背景绘制。 - Queeg

2

由于这可能相当依赖于平台和版本,您可以查看Plot上的setBackgroundImageAlpha()来达到所需的效果。


1

我曾经遇到过类似的问题,但是当我把背景图像设置为0.0f时,问题得到了解决。

setBacgroundImageAlpha(0.0f),因为它设置了绘制背景图像时使用的alpha透明度。

alpha透明度(范围在0.0f到1.0f之间,其中0.0f完全透明,1.0f完全不透明)。

这能奏效,是因为PNG(便携式网络图形)格式支持alpha通道透明度。

我发现你的代码与我的唯一区别在于

p.setBackgroundPaint(new Color(127, 127, 127, 64)); // 你的透明度代码

p.setBackgroundPaint(new Color(255,255,255,0)); // 我的透明度代码


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接