Subclass LegendTitle problems

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
timw
Posts: 20
Joined: Fri Mar 18, 2005 12:36 pm
Location: London

Subclass LegendTitle problems

Post by timw » Mon Mar 21, 2005 2:53 pm

Hi,
I have tried to subclass the LegendTitle class to allow me to remove the colour in the Legend but have run into some problems.

Even if i just extend LegendTitle and change nothing else, the plot will not render. It gives an error :

java.lang.RuntimeException: Not implemented.
at org.jfree.chart.block.FlowArrangement.arrange(FlowArrangement.java:145)
at org.jfree.chart.block.BlockContainer.arrange(BlockContainer.java:174)
at ColourLegendTitle.arrange(ColourLegendTitle.java:285)

This surprised me a bit as FlowArrangement is the default arrangement for LegendTitle and it seems that constructing a LegendTitle by doing the following:

Code: Select all

 LegendTitle title = new LegendTitle(plot);
will not work either.

Am I missing something here?

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 » Tue Mar 22, 2005 6:00 pm

Can you post a small self-contained program that demonstrates the problem?
David Gilbert
JFreeChart Project Leader

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

timw
Posts: 20
Joined: Fri Mar 18, 2005 12:36 pm
Location: London

Post by timw » Wed Mar 23, 2005 9:57 am

Thanks for your response David. Here is a modified version of PieChartDemo1 provided with the latest release to demonstrate the problem I am seeing. If there is a better way that i should be setting the LegendTitle please let me know and I'll try that out.

Code: Select all


import java.awt.Font;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.Title;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/**
 * A simple demonstration application showing how to create a pie chart using 
 * data from a {@link DefaultPieDataset}.
 */
public class LegendTitleDemo extends ApplicationFrame {

    /**
     * Default constructor.
     *
     * @param title  the frame title.
     */
    public LegendTitleDemo(String title) {
        super(title);
        setContentPane(createDemoPanel());
    }

    /**
     * Creates a sample dataset.
     * 
     * @return a sample dataset.
     */
    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;        
    }
    
    /**
     * Creates a chart.
     * 
     * @param dataset  the dataset.
     * 
     * @return a chart.
     */
    private static JFreeChart createChart(PieDataset dataset) {
        
        JFreeChart chart = ChartFactory.createPieChart(
            "Pie Chart Demo 1",  // chart title
            dataset,             // data
            true,               // include legend
            true,
            false
        );
        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
        plot.setNoDataMessage("No data available");
        plot.setCircular(true);
        plot.setLabelGap(0.02);
        //Get the list of subtitles
        ArrayList subtitles = (ArrayList) chart.getSubtitles();
        //Create new List to store new subtitles
        ArrayList newtitles = new ArrayList();
        //Iterate through subtitles to create new list
        for(Iterator iter = subtitles.iterator();iter.hasNext();){
            Title t = (Title) iter.next();
            //If a LegendTitle is found, create a new one and add that to new List
            if(t instanceof LegendTitle){
                LegendTitle legtitle = new LegendTitle(plot);
                newtitles.add(legtitle);
            }
            else{
                newtitles.add(t);
            }
        }
        //Set the subtitles of the chart with the new List
        chart.setSubtitles(newtitles);
        return chart;
    }
    
    /**
     * Creates a panel for the demo (used by SuperDemo.java).
     * 
     * @return A panel.
     */
    public static JPanel createDemoPanel() {
        JFreeChart chart = createChart(createDataset());
        return new ChartPanel(chart);
    }
    
    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(String[] args) {

        LegendTitleDemo demo = new LegendTitleDemo("LegendTitle Demo 1");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }

}
Here is the error I am getting:

Code: Select all

java.lang.RuntimeException: Not implemented.
	at org.jfree.chart.block.FlowArrangement.arrange(FlowArrangement.java:145)
	at org.jfree.chart.block.BlockContainer.arrange(BlockContainer.java:174)
	at org.jfree.chart.title.LegendTitle.arrange(LegendTitle.java:329)
	at org.jfree.chart.JFreeChart.drawTitle(JFreeChart.java:1061)
	at org.jfree.chart.JFreeChart.draw(JFreeChart.java:959)
	at org.jfree.chart.ChartPanel.paintComponent(ChartPanel.java:1132)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JComponent.paintChildren(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JLayeredPane.paint(Unknown Source)
	at javax.swing.JComponent.paintChildren(Unknown Source)
	at javax.swing.JComponent.paintWithOffscreenBuffer(Unknown Source)
	at javax.swing.JComponent.paintDoubleBuffered(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
	at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
	at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
	at java.awt.Container.paint(Unknown Source)
	at sun.awt.RepaintArea.paint(Unknown Source)
	at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)

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 » Wed Mar 23, 2005 2:12 pm

Thanks for the code. Here is the change I made to fix the problem:

http://cvs.sourceforge.net/viewcvs.py/j ... .9&r2=1.10
David Gilbert
JFreeChart Project Leader

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

timw
Posts: 20
Joined: Fri Mar 18, 2005 12:36 pm
Location: London

Post by timw » Wed Mar 23, 2005 2:34 pm

Excellent! Those changes work perfectly. Thanks a lot David. I can now extend LegendTitle to my heart's delight.

Locked