Cloning and disabling series in Chart - legend item appears

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
chrisrhyno2003
Posts: 30
Joined: Thu Jun 18, 2015 5:42 pm
antibot: No, of course not.

Cloning and disabling series in Chart - legend item appears

Post by chrisrhyno2003 » Wed Jul 15, 2015 2:13 am

Hi,

I am cloning a JFreeChart and disabling one of the series in the Chart. if I disable the series on the original chart, the series and the legend item do not appear. But when do the same command on the cloned chart, the legend item still appears.
What am I doing wrong?
Here's my code:




package test;

import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

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();
//chart.getXYPlot().getRenderer().setSeriesVisible(1, false, true); This statement correct disables the series and the legend item.
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);

}

}

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Cloning and disabling series in Chart - legend item appe

Post by John Matthews » Wed Jul 15, 2015 3:01 am

Cross-posted here.

chrisrhyno2003
Posts: 30
Joined: Thu Jun 18, 2015 5:42 pm
antibot: No, of course not.

Re: Cloning and disabling series in Chart - legend item appe

Post by chrisrhyno2003 » Wed Jul 15, 2015 5:00 pm

Hi John, there is no exact reply there and the comments link it to the same page - the page where I posted my question.
So if there's a better reply, please let me know.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Cloning and disabling series in Chart - legend item appe

Post by paradoxoff » Thu Jul 16, 2015 12:05 pm

What John is saying is if you post the same question in two forums separately, you should post links to the respective other forum.
Otherwise somebody might invest time in writing a post in one forum just to find out that the question has already been answered in the other.
Regarding your question: what version of JFreeChart are you using? I am using a heavily customized version of 1.0.13, and I found that the colors in the plot and the legend are not the same when the cloned chart is used to create the chart panel. Looks like a tricky bug in the cloning meachanism.

chrisrhyno2003
Posts: 30
Joined: Thu Jun 18, 2015 5:42 pm
antibot: No, of course not.

Re: Cloning and disabling series in Chart - legend item appe

Post by chrisrhyno2003 » Thu Jul 16, 2015 4:58 pm

I am using JFreeChart 1.0.19 - the latest version on the website. Is this a bug ?Or am I the only one experiencing it ?

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Cloning and disabling series in Chart - legend item appe

Post by paradoxoff » Fri Jul 17, 2015 7:45 am

Looks like a bug to me.
The cloning of a JfreeChart should be possible withput seeing side effects.
Note that I am not using the latest version, so the behaviour that I see is not necessarily what you see. Do the colors of the cloned chart look ok?

chrisrhyno2003
Posts: 30
Joined: Thu Jun 18, 2015 5:42 pm
antibot: No, of course not.

Re: Cloning and disabling series in Chart - legend item appe

Post by chrisrhyno2003 » Fri Jul 17, 2015 5:09 pm

The colours are all good. The chart seems to be good. Except that when I disable a series, the legend item does not disappear. That was the only issue which I was facing.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Cloning and disabling series in Chart - legend item appe

Post by paradoxoff » Sun Jul 19, 2015 5:47 pm

I have downloaded JFreeChart 1.0.19 and tried to reproduce the behaviour. I have used a simplified dataset in order to make it easier to see which data points come from which dataset:

Code: Select all

    private XYDataset createDataset() {

        final XYSeries series1 = new XYSeries("First");
        series1.add(1.0, 1.0);
        series1.add(2.0, 1.0);
        series1.add(3.0, 1.0);

        final XYSeries series2 = new XYSeries("Second");
        series2.add(1.0, 2.0);
        series2.add(2.0, 2.0);
        series2.add(3.0, 2.0);

        final XYSeries series3 = new XYSeries("Third");
        series3.add(1.0, 3.0);
        series3.add(2.0, 3.0);
        series3.add(3.0, 3.0);

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

        return dataset;

    }
Based on what I see, the fact that the series is appearing in the legend of the cloned chart is not the only thing that is wrong with the cloned chart:
- The cloned chart shows blue circles for the first series, and red rectangles for the third. In the original chart, the first series appears as red rectangles, the second as a blue line, and the third as green rectangles.
- The legend of the original and the cloned chart are identical. That means, that the series format in the legend is not consistent with what is shown in the plot.
Can you verify that you see something similar?

[edit] Unfortunately, I have no access to my development machine, so I can´t try this myself: after cloning the chart, remove the existing legend and add a new one, using the plot as argument for the LegendTitle. Does that give you a legend that does not show the legend? I had a look at the LegendTitle class, and it doesn´t overrride clone. As a result, a cloned LegendTitle should use the same legend item source as the original legend title.

chrisrhyno2003
Posts: 30
Joined: Thu Jun 18, 2015 5:42 pm
antibot: No, of course not.

Re: Cloning and disabling series in Chart - legend item appe

Post by chrisrhyno2003 » Wed Jul 22, 2015 7:56 pm

Yes, you are correct - the series shapes change and also the line types as well.

Locked