JFreeChart - 听取系列的变化

时间:2012-08-14 09:17:45

标签: java jfreechart

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

1 个答案:

答案 0 :(得分: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;
        }
    }
});