Question on creating "transparent" plots.

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Sigint
Posts: 2
Joined: Sun Sep 14, 2014 5:55 pm
antibot: No, of course not.

Question on creating "transparent" plots.

Post by Sigint » Sun Sep 14, 2014 6:34 pm

There are several references to creation of transparent backgrounds on charts, but these questions may be referring to an issue different than what I want to accomplish.
I would like to be able to create two charts and then drag one over the other and be able to see through the top chart's background to see the plot beneath.
The top chart should contain the axis, tick marks (lines), and data but its background should be as if the chart was produced on a piece of glass which would let me see through it to the underlying chart.
Is this possible? I am assuming it is but I have not had any results in using the following functions in producing a plot I can see through.

I am running:
Jfreechart 1.0.19 (tried it with 1.0.16 with same results earlier)
Windows 7
The charts are built with Eclipse "Juno" and/or "Kepler" with JRE 7.0

This forum has multiple references to setting the background paint and the background alpha and even setting the background images.
I have tried multiple combinations of these and cannot get the transparent look I desire.

plot.setBackgroundPaint (null); // produces the same thing as Color.white
adding setBackgroundAlpha(0 or 1) doesn't appear to change anything.

I can change the background to other themes or colors, but I desire a plot that looks like it was drawn on a transparent piece of glass.
following is a simple example of the test code I used to try modifying the background look.
comments are in code for the various options that were tried that did not appear to do anything.

public void plotDataNew( XYSeriesCollection dataset, String fileName, String Xlabel,
String Ylabel, JTextArea textWindow )
{
// Generate the graph
JFreeChart chart = ChartFactory.createScatterPlot(
fileName, // Title
Xlabel, // x-axis Label
Ylabel, // y-axis Label
dataset, // Dataset
PlotOrientation.VERTICAL, // Plot Orientation
true, // Show Legend
true, // Use tooltips
false // Configure chart to generate URLs
);

// chart.setBackgroundPaint(null); // didn't help transparency
// chart.setBackgroundAlpha(0); // didn't help transparency

ChartFrame frame = new ChartFrame("Scatter Plot NEW", chart);
// Color trans = new Color(0xFF, 0xFF, 0xFF, 0); // doesnt' help transparency

XYPlot plot = (XYPlot) chart.getPlot();
// chart.setBackgroundPaint(trans);// doesnt' help transparency
// plot.setBackgroundPaint(trans);// doesnt' help transparency

// plot.getBackgroundImage(); // try hack to override not working
// plot.setBackgroundImageAlpha((float) (0.0)); // doesn't help
// Paint p = new GradientPaint(0, 0, Color.white, 1000, 0, Color.green); // doesn't help
// plot.setBackgroundPaint(new Color(27, 27, 220));
// plot.setBackgroundImage(null);
plot.setBackgroundPaint (null); // always get White????
plot.setBackgroundAlpha(0); // 0 is fully transparent? doesn't appear to change anything.
plot.setDomainGridlinePaint (Color.BLACK);
plot.setRangeGridlinePaint (Color.BLACK);

plot.setAxisOffset (new RectangleInsets(50, 0, 0, 5));
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible (true);

NumberAxis domainAxis = (NumberAxis)chart.getXYPlot().getDomainAxis();
NumberAxis rangeAxis = (NumberAxis)chart.getXYPlot().getRangeAxis();
final DecimalFormat formatx = new DecimalFormat("######.###");
final DecimalFormat formaty = new DecimalFormat("######.#");
domainAxis.setNumberFormatOverride(formatx);
rangeAxis.setNumberFormatOverride(formaty);

XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled (true);
double size = 3.0;
double delta = size / 10.0;
Shape shape1 = new Rectangle2D.Double(-delta, -delta, size, size);
Shape shape2 = new Ellipse2D.Double(-delta, -delta, size, size);
renderer.setSeriesShape(0, shape1);
renderer.setSeriesShape(1, shape2);

frame.setSize (700, 700);
frame.setVisible(true);

}

// end of example code.

thank you for any responses on this question.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Question on creating "transparent" plots.

Post by david.gilbert » Wed Sep 17, 2014 8:50 pm

I get a transparent chart background (all the way through to the background of the JFrame) using:

Code: Select all

plot.setBackgroundPaint(null);
chart.setBackgroundPaint(null);
chartPanel.setBackground(null);
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Sigint
Posts: 2
Joined: Sun Sep 14, 2014 5:55 pm
antibot: No, of course not.

Re: Question on creating "transparent" plots.

Post by Sigint » Thu Sep 18, 2014 4:40 am

Thank you for your response. This produces something closer to what I want but I may not have explained the original problem well enough.

Think of what I am trying to do as this:
I will be creating a plot on a piece of paper (not transparent) and then I am going to draw a similar plot on viewfoil material like one uses for a projector presentation.
When you lay the viewfoil over the paper plot you can see both results.
All that should show on the "viewfoil plot" would be the data that was plotted, title, axis, gridlines, etc.
I wish to make the whole background of a plot disappear so I can move it over the top of other plots for comparison.
I think it would be very useful to be able to stack several of these transparent plots on top of each other for quick comparisons.
If the background can be made to disappear so that it appears to have been drawn on a transparent background then you can produce a reference plot which can be moved over the top of other plots and the results can be quickly compared without needing to plot all the results together on one chart.

Again, thanks for your original response.

Locked