从CombinedDomainXYPlot获取图(并删除它们) [英] Get plots from CombinedDomainXYPlot (and remove them)

查看:48
本文介绍了从CombinedDomainXYPlot获取图(并删除它们)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我不保留对它们的引用,是否有办法将其列表添加到CombinedDomainXYPlot ?

我想到达那里,与它们一起工作,并有可能将它们从组合图中删除.

I'd like to get there plots, work with them and possibly remove them from the compined plot.

这是用于将绘图添加到CombinedDomainXYPlot的示例代码:

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);

更新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?

完整的示例代码:

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-从它使用Collections.unmodifiableList,它返回一个新的List,它由原始文件支持.遍历List时,实际上已从基础List中删除了项目,从而影响了Collection上的迭代.

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.

而不是依靠迭代(例如for (Object sp : plotCombined.getSubplots())),而是向后循环遍历数组,并使用索引删除该项目.

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));
}

这篇关于从CombinedDomainXYPlot获取图(并删除它们)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆