仍然会出现克隆和禁用JFreeChart中的系列 - 图例项目

时间:2015-07-15 01:02:48

标签: java jfreechart

我正在克隆JFreeChart,然后使用setSeriesVisible()方法禁用其中一个系列。该系列没有出现。但是,图例项仍会出现。如何使图例项不显示。我究竟做错了什么?

这是我的代码:

package test;

import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class Test2 extends ApplicationFrame {


public Test2(final String title) {

    super(title);

    try {
        final XYDataset dataset = createDataset();
        final JFreeChart chart = createChart(dataset);
        JFreeChart tempChart = (JFreeChart) chart.clone();
        tempChart.getXYPlot().getRenderer().setSeriesVisible(1, false, true);
        final ChartPanel chartPanel = new ChartPanel(tempChart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);
    } catch (CloneNotSupportedException ex) {
        Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
    }

}

private XYDataset createDataset() {

    final XYSeries series1 = new XYSeries("First");
    series1.add(1.0, 1.0);
    series1.add(2.0, 4.0);
    series1.add(3.0, 3.0);
    series1.add(4.0, 5.0);
    series1.add(5.0, 5.0);
    series1.add(6.0, 7.0);
    series1.add(7.0, 7.0);
    series1.add(8.0, 8.0);

    final XYSeries series2 = new XYSeries("Second");
    series2.add(1.0, 5.0);
    series2.add(2.0, 7.0);
    series2.add(3.0, 6.0);
    series2.add(4.0, 8.0);
    series2.add(5.0, 4.0);
    series2.add(6.0, 4.0);
    series2.add(7.0, 2.0);
    series2.add(8.0, 1.0);

    final XYSeries series3 = new XYSeries("Third");
    series3.add(3.0, 4.0);
    series3.add(4.0, 3.0);
    series3.add(5.0, 2.0);
    series3.add(6.0, 3.0);
    series3.add(7.0, 6.0);
    series3.add(8.0, 3.0);
    series3.add(9.0, 4.0);
    series3.add(10.0, 3.0);

    final XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(series1);
    dataset.addSeries(series2);
    dataset.addSeries(series3);

    return dataset;

}


private JFreeChart createChart(final XYDataset dataset) {

    final JFreeChart chart = ChartFactory.createXYLineChart(
        "Line Chart Demo 6",      // chart title
        "X",                      // x axis label
        "Y",                      // y axis label
        dataset,                  // data
        PlotOrientation.VERTICAL,
        true,                     // include legend
        true,                     // tooltips
        false                     // urls
    );


    chart.setBackgroundPaint(Color.white);

    final XYPlot plot = chart.getXYPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);

    final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    renderer.setSeriesLinesVisible(0, false);
    renderer.setSeriesShapesVisible(1, false);
    plot.setRenderer(renderer);

    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    return chart;

}

public static void main(final String[] args) {

    final Test2 demo = new Test2("Line Chart Demo 6");
    demo.pack();
    RefineryUtilities.centerFrameOnScreen(demo);
    demo.setVisible(true);

}

}

1 个答案:

答案 0 :(得分:0)

如您所知,默认情况下,Java clone()方法执行“浅拷贝”,这意味着它不会克隆对象的每个字段(它只是复制对象的引用)他们)。因此,除非他们在文档中明确地解释它,否则clone()方法在被开发人员覆盖时会做什么并不清楚。在目前的情况下,我会说it is still a bit ambiguous

  

克隆对象,并负责监听器。注意:调用者应在克隆图上注册自己的监听器。

所以我不确定Plot对象,渲染器,数据集,轴,图例等会发生什么。

我的建议:不要使用clone()。创建需要复制的对象的新实例,这样您就可以精确控制哪些对象是唯一的,哪些对象是兄弟对象。希望传奇与可见系列不一致的问题也会消失。