2

我如何订阅给定的 JFreeChart 对象以收到有关系列集更改的通知,即插入的新系列或添加到系列的新数据点?

4

1 回答 1

2

对于数据集:

Dataset coll = new XYSeriesCollection();
coll.addChangeListener(new DatasetChangeListener() {
    @Override
    public void datasetChanged(DatasetChangeEvent arg0) {
        // TODO Auto-generated method stub
    }
});

或者对于 JFreeChart 本身:

JFreeChart chart = createChart();
chart.addChangeListener(new ChartChangeListener() {
    @Override
    public void chartChanged(ChartChangeEvent arg0) {
        // TODO Auto-generated method stub
    }
});

我敢肯定,这也适用于中间的情节和最后的系列。

编辑:只收听添加/删除的系列:

final XYSeriesCollection coll = new XYSeriesCollection();
coll.addChangeListener(new DatasetChangeListener() {
    private int numberOfSeries = -1;

    @Override
    public void datasetChanged(DatasetChangeEvent arg0) {
        int numberOfSeriesNew = coll.getSeriesCount();
        if (numberOfSeriesNew == numberOfSeries) {
            // no series was added/removed
        } else if (numberOfSeriesNew < numberOfSeries) {
            // series was removed
            numberOfSeries = numberOfSeriesNew;
        } else {
            // series was added
            numberOfSeries = numberOfSeriesNew;
        }
    }
});
于 2012-08-14T09:36:37.027 回答