Is there a way to get plot list added to CombinedDomainXYPlot if I don't keep any references to them?
I'd like to get there plots, work with them and possibly remove them from the compined plot.
This is example code for adding plots to CombinedDomainXYPlot:
// axis
DateAxis domainAxis = new DateAxis("Date");
// plot container
CombinedDomainXYPlot plotCombined = new CombinedDomainXYPlot(domainAxis);
// plot 1
XYPlot plot1 = new XYPlot();
plot1.setDomainAxis(domainAxis);
plotCombined.add(plot1);
// plot 2
XYPlot plot2 = new XYPlot();
plot2.setDomainAxis(domainAxis);
plotCombined.add(plot2);
Update 1:
I've just tried this code but it doesn't return all the plots. It's not reliable.
for (Object sp : plotCombined.getSubplots()) {
plotCombined.remove((XYPlot)sp);
}
It this method of removing the plots correct?
Full example code:
import javax.swing.JFrame;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.XYPlot;
public class sample27 extends JFrame {
public sample27() {
super("sample27");
// axis
DateAxis domainAxis = new DateAxis("Date");
// plot container
CombinedDomainXYPlot plotCombined = new CombinedDomainXYPlot(domainAxis);
// plot 1
XYPlot plot1 = new XYPlot();
plot1.setDomainAxis(domainAxis);
plotCombined.add(plot1);
// plot 2
XYPlot plot2 = new XYPlot();
plot2.setDomainAxis(domainAxis);
plotCombined.add(plot2);
System.out.println("plot count before: " + plotCombined.getSubplots().size());
for (Object sp : plotCombined.getSubplots()) {
System.out.println("removing subplot: " + sp);
plotCombined.remove((XYPlot)sp);
}
System.out.println("plot count after: " + plotCombined.getSubplots().size());
}
public static void main(String[] args) {
new sample27();
}
}
Output:
plot count before: 2
removing subplot: org.jfree.chart.plot.XYPlot@15615099
plot count after: 1
getSubplots
returns a List
containing all the items - this List
is copy from the standpoint that it uses Collections.unmodifiableList
, which returns a new List
backed by the original. As you iterate over the List
, items are in fact being removed from the underlying List
, affecting the iteration over the Collection
.
Rather than rely on iteration (eg for (Object sp : plotCombined.getSubplots())
), loop over the array backwards and use the index to remove the item.
for ( int i = plotCombined.getSubplots().size() - 1; i >= 0; i-- ){
plotCombined.remove((XYPlot)plotCombined.getSubplots().get(i));
}