JFreechart ChartPanel not getting Transparenent
Asked Answered
S

3

7

I want to give the chart background a transparent look (not fully transparent but a little bit). Here is my code. I have added few code lines to add transparency, but I guess the ChartPanel is not getting transparent. After writing those code lines, the chart backgound is appearing gray.

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);
Scouting answered 22/4, 2012 at 15:26 Comment(1)
Without your sscce, we can only guess what is gray and why.Cressler
O
6

I found that I have to use a transparent color both for chart and plot:

val trans = new Color(0xFF, 0xFF, 0xFF, 0)
chart.setBackgroundPaint(trans)
plot .setBackgroundPaint(trans)
Overmatter answered 23/10, 2013 at 11:52 Comment(1)
It works, but I had to also set the background paint on the legend.Ellipsoid
C
2

Because this can be rather dependent on platform and version, you might look at setBackgroundImageAlpha() on the Plot to get the desired effect.

Cressler answered 23/4, 2012 at 1:28 Comment(0)
S
1

I faced the similar problem however it got solved after i set the background image to 0.0f i.e

setBacgroundImageAlpha(0.0f). since it Sets the alpha transparency used when drawing the background image.

the alpha transparency (in the range 0.0f to 1.0f, where 0.0f is fully transparent, and 1.0f is fully opaque).

This works because the PNG (Portable Network Graphics) format supports alpha channel transparency.

The only difference i found between your code and mine was in

p.setBackgroundPaint(new Color(127, 127, 127, 64)); // your tranparency code

p.setBackgroundPaint(new Color(255,255,255,0)); // my transparency code

Scudo answered 22/5, 2012 at 6:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.