从 CombinedDomainXYPlot 获取绘图(并删除它们)
Get plots from CombinedDomainXYPlot (and remove them)
如果我不保留对它们的任何引用,是否有办法将绘图列表添加到 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:
我刚刚试过这段代码,但它没有 return 所有的情节。不靠谱。
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
returns 包含所有项目的 List
- 这个 List
从它使用的角度来看是 copy Collections.unmodifiableList
,其中 returns 一个新的 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));
}
作为所示方法的替代方法 , iterate over a modifiable list constructed from the unmodifiable list returned by getSubplots()
。
代码:
List<XYPlot> list = new ArrayList<>(plotCombined.getSubplots());
for (XYPlot plot : list) {
plotCombined.remove(plot);
}
控制台:
plot count before: 2
plot count after: 0
如果我不保留对它们的任何引用,是否有办法将绘图列表添加到 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:
我刚刚试过这段代码,但它没有 return 所有的情节。不靠谱。
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
returns 包含所有项目的 List
- 这个 List
从它使用的角度来看是 copy Collections.unmodifiableList
,其中 returns 一个新的 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));
}
作为所示方法的替代方法 getSubplots()
。
代码:
List<XYPlot> list = new ArrayList<>(plotCombined.getSubplots());
for (XYPlot plot : list) {
plotCombined.remove(plot);
}
控制台:
plot count before: 2 plot count after: 0