如果我不保留对它们的任何引用,是否有办法将绘图列表添加到CombinedDomainXYPlot?
我想到达那里,与它们一起工作,并可能将它们从复合图中删除。
这是向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);
更新1:
我刚刚尝试了这段代码,但它没有返回所有的图。这不可靠。
for (Object sp : plotCombined.getSubplots()) {
plotCombined.remove((XYPlot)sp);
}
这种去除绘图的方法正确吗?
完整示例代码:
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();
}
}
输出:
plot count before: 2
removing subplot: org.jfree.chart.plot.XYPlot@15615099
plot count after: 1
最佳答案
getSubplots
返回一个包含所有项目的 List
- 从它使用的角度来看,此 List
是复制 Collections.unmodifyingList
,它返回一个由原始列表支持的新List
。当您迭代 List
时,实际上会从底层 List
中删除项目,从而影响 Collection
上的迭代。
不要依赖迭代(例如for (Object sp :plotCombined.getSubplots())
),而是向后循环数组并使用索引来删除项目。
for ( int i = plotCombined.getSubplots().size() - 1; i >= 0; i-- ){
plotCombined.remove((XYPlot)plotCombined.getSubplots().get(i));
}
关于java - 从CombinedDomainXYPlot 获取绘图(并删除它们),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30647790/