在运行时更改XYSeries的键

时间:2013-04-16 18:19:37

标签: java jfreechart

我知道我可以在创建时设置图表的标题:

 XYSeries series = new XYSeries("Random Data");

但是如果在我的代码中我想在运行时将标题“随机数据”更改为其他内容呢?完整代码在此处提供:JFreeChart: XY Series Demo 3

3 个答案:

答案 0 :(得分:3)

XYSeriesSeries,因此您可以使用setKey()更改名称。因为名称也是系列键,所以监听XYSeriesCollection可以否决更改。一种解决方法是删除系列,更改名称,并将系列还原到数据集:

private XYSeriesCollection dataset;
private XYSeries series;
...
dataset.removeSeries(series);
series.setKey("Stochastic Data");
dataset.addSeries(series);

image

答案 1 :(得分:1)

XYSeries继承自ApplicationFrame继承自JFrame的{​​{1}}; FrameFrame方法。

http://www.jfree.org/jcommon/api/org/jfree/ui/ApplicationFrame.html

答案 2 :(得分:1)

您可以通过构造函数参数传递系列标题。您只需修改代码:

public XYSeriesDemo3(final String title, String mySeriesTitle) {
        super(title);
        IntervalXYDataset dataset = createDataset(mySeriesTitle);
        ...
}

private IntervalXYDataset createDataset(String mySeriesTitle) {
        final XYSeries series = new XYSeries(mySeriesTitle);
        ...
} 

然后在你的主要方法中:

public static void main(final String[] args) {
        XYSeriesDemo3 demo = new XYSeriesDemo3("XY Series Demo 3", "my series A");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

        demo = new XYSeriesDemo3("XY Series Demo 3", "my series B");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }