getSeriesKey() in LegendItemEntity for pie chart always null

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
predrag
Posts: 2
Joined: Fri Jun 22, 2007 4:20 pm

getSeriesKey() in LegendItemEntity for pie chart always null

Post by predrag » Fri Jun 22, 2007 4:40 pm

It looks like getSeriesKey() method in LegendItemEntity in v1.0.6 always returns null for pie charts. Just like deprecated getSeriesIndex() always returned 0. In other words, it is not possible to decipher what item you clicked on in the pie chart legend.

Here is modified PieChartDemo1 code that illustrates this problem:

Code: Select all

import java.awt.*;

import javax.swing.*;

import org.jfree.chart.*;
import org.jfree.chart.entity.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.title.*;
import org.jfree.data.general.*;
import org.jfree.ui.*;

public class PieChartDemo1 extends ApplicationFrame implements ChartMouseListener {
    public static final long serialVersionUID = 1L;
    public static JFreeChart chart;

    public PieChartDemo1(String title) {
        super(title);
        JPanel chartPanel = createDemoPanel();
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);
    }

    private static PieDataset createDataset() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("One", new Double(43.2));
        dataset.setValue("Two", new Double(10.0));
        dataset.setValue("Three", new Double(27.5));
        dataset.setValue("Four", new Double(17.5));
        dataset.setValue("Five", new Double(11.0));
        dataset.setValue("Six", new Double(19.4));
        return dataset;
    }

    private static JFreeChart createChart(PieDataset dataset) {

        JFreeChart chart = ChartFactory.createPieChart(
            "Pie Chart Demo 1",
            dataset,
            true,
            true,
            false
        );
        TextTitle title = chart.getTitle();
        title.setToolTipText("A title tooltip!");

        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
        plot.setNoDataMessage("No data available");
        plot.setCircular(false);
        plot.setLabelGap(0.02);
        return chart;

    }

    public JPanel createDemoPanel() {
        chart = createChart(createDataset());
        ChartPanel panel = new ChartPanel(chart);
        panel.addChartMouseListener(this);
        return panel;
    }

    public void chartMouseClicked(ChartMouseEvent event) {
        ChartEntity e = event.getEntity();
        if (e != null) {
            if (e instanceof LegendItemEntity) {
                LegendItemEntity entity = (LegendItemEntity) e;
                Comparable seriesKey = entity.getSeriesKey(); // always null!!!
                System.out.println(entity.toString());
                PiePlot plot = (PiePlot) this.chart.getPlot();
                DefaultPieDataset dataset = (DefaultPieDataset)plot.getDataset();
                for (int i = 0; i < dataset.getItemCount(); i++) {
                    plot.setSectionOutlineStroke(dataset.getKey(i), new BasicStroke(1.0f));
                    if (dataset.getKey(i).equals(seriesKey)) {
                        plot.setSectionOutlineStroke(dataset.getKey(i), new BasicStroke(2.0f));
                    }
                }
            }
        }
    }

    public void chartMouseMoved(ChartMouseEvent event) {
        // ignore
    }

    public static void main(String[] args) {

        PieChartDemo1 demo = new PieChartDemo1("Pie Chart Demo 1");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }
}
Am I missing something?

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Fri Jun 22, 2007 5:57 pm

Thanks for the report. I won't have time to look at this until Monday, but it is noted.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

cchab
Posts: 3
Joined: Tue May 22, 2007 4:07 pm

Post by cchab » Thu Aug 14, 2008 10:48 am

Hi,

I have the same problem with jfreechart-1.0.10.
I tested the sample above and it still outputs seriesKey=null

Code: Select all

LegendItemEntity: seriesKey=null, dataset=org.jfree.data.general.DefaultPieDataset@b4e6429f

cchab
Posts: 3
Joined: Tue May 22, 2007 4:07 pm

Post by cchab » Thu Aug 14, 2008 10:58 am

I have added a bug report for this problem (request id 2051168)

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Thu Aug 14, 2008 8:56 pm

Thanks for adding the bug report. I'll try to fix this before 1.0.11 is out.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Fri Aug 15, 2008 1:38 pm

Fix is going into Subversion now.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked