Properties- dialog

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
PollerJava
Posts: 81
Joined: Wed Jul 25, 2007 10:40 am

Properties- dialog

Post by PollerJava » Wed Dec 17, 2008 8:23 am

Hello,

is it possible to add a tab to the chart- properties- dialog (right mouse button onto the chart -> first item properties)
By default there are 3 tabs -> headline, diagramm an others,

Thanks a lot for responses,

All the best,

PollerJava
Posts: 81
Joined: Wed Jul 25, 2007 10:40 am

Post by PollerJava » Thu Dec 18, 2008 2:05 pm

Has anybody an idea how I can do this,

Thanks a lot for responses,
All the best,

titsworth
Posts: 3
Joined: Fri Dec 19, 2008 3:24 am

Post by titsworth » Fri Dec 19, 2008 3:37 am

There was some earlier discussion about this topic:
Poor newbies can't post URLs though... topic ID is 22660


I was going to add an Excel like "Series" editor for my plots by adding a tab for this in the chart editor, but I think I have decided to make a separate dialog altogether to do this instead of modifying ChartEditor.

Haven't started yet, but it sounds easy enough. :D

MitchBuell
Posts: 47
Joined: Thu Dec 11, 2008 7:59 pm

Post by MitchBuell » Fri Dec 19, 2008 3:50 pm

The general order of operation when you click on Properties...
- The DefaultChartEditorFactory creates a DefaultChartEditor with the JFreeChart that you are editing.
- The DefaultChartEditor builds a JPanel full of options for the user to play with.
- The updateChart method applies the user selections when the user clicks OK.

What you can do is take his existing chart editor and then modify it to meet your own needs. You will replace the DefaultChartEditorFactory and DefaultChartEditor with your own versions. You will need the JFreeChart source code included with the download.

1) Grab a copy of his chart editor. It’s located at JFreeChart.zip\source\org\jfree\chart\editor. There are 12 Java files, 10 Properties files, and a HTML file. You need everything except the HTML file. (The Properties files are for language localization, HTML file is for Javadoc).

2) Put the Java and Properties files into a folder in the source code for your program, for example my\java\app\myeditor.

3) Rename the file DefaultChartEditorFactory.java to MyChartEditorFactory.java. Open it and rename every occurrence of Default with My.

Code: Select all

package my.java.app.myeditor;

import org.jfree.chart.*;
import org.jfree.chart.editor.*;

/**
 * My implementation of the ChartEditorFactory interface.
 */
public class MyChartEditorFactory implements ChartEditorFactory
{
    /**
     * Creates a new instance.
     */
    public MyChartEditorFactory()
    {
    }

    /**
     * Returns a new instance of a ChartEditor.
     *
     * @param chart  the chart.
     *
     * @return A chart editor for the given chart.
     */
    public ChartEditor createEditor(JFreeChart chart)
    {
        return new MyChartEditor(chart);
    }
}
4) Rename the file DefaultChartEditor.java to MyChartEditor.java. Open it and rename references of DefaultChartEditor to MyChartEditor. Do NOT change anything related to the DefaultAxisEditor, DefaultColorBarEditor, DefaultNumberAxisEditor, DefaultPlotEditor, DefaultTitleEditor. Those we will leave alone for now.

Code: Select all

package my.java.app.myeditor;

import java.awt.*;
import javax.swing.*;

import org.jfree.chart.*;
import org.jfree.chart.editor.*;
import org.jfree.chart.plot.*;

/**
 * A panel for editing chart properties.
 */
class MyChartEditor extends JPanel implements ChartEditor
{
    /**
     * Constructs a panel for editing the specified chart.
     */
    public MyChartEditor(JFreeChart chart)
    {
         ....
    }
}
5) At this point we can test to see if your program can use your local chart editor instead of his. In your main class, add an import statement to include your local chart editor

Code: Select all

import my.java.app.myeditor;
Then somewhere, usually in the main() or Constructor, call

Code: Select all

ChartEditorManager.setChartEditorFactory(new MyChartEditorFactory());
Compile and run. You may get deprecated warnings; ignore them for now. The chart editor should be the same as before, but now it is your local copy.

6) Now you can modify the chart editor to your own needs. The top level file to start modifying is MyChartEditor.java. This is the JPanel that holds everything together, it is responsible for the tabs, where each tab is another JPanel contained in one of: DefaultAxisEditor.java, DefaultColorBarEditor.java, DefaultNumberAxisEditor.java, DefaultPlotEditor.java, DefaultTitleEditor.java. So start by modifying MyChartEditor.java and any sub JPanel you need.

PollerJava
Posts: 81
Joined: Wed Jul 25, 2007 10:40 am

Post by PollerJava » Mon Dec 22, 2008 11:18 am

Hi MitchBuell,

I did it step by step like in your description but it doesn't work.
It doesn't use my editor:

Behind is the code of mine, do you have any idea, whats wrong?
Do I have to set the ChartEditorManager onto my chart- panel,
My opinion is that jfreechart uses the internal editor instead of mine.

Thanks a lot,
all the best,
Poller

Code: Select all

public ChartPanel getChart() {                                              // this methode is invoked to create the freechartpanel
        chart = createChart();       
        chart.addChangeListener(new TrendZoomListener(plot));
        plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);
        chartPanel = new MyChartPanel(chart, traceView);
        chartPanel.setPreferredSize(new java.awt.Dimension(600, 270));
        chartPanel.setDomainZoomable(true);
        chartPanel.setRangeZoomable(true); 
        chartPanel.setName(mainTitle);
        chartPanel.getPopupMenu().getComponent();
        MyChartEditorFactory cef = new MyChartEditorFactory();  // here are the important lines
        ChartEditorManager.setChartEditorFactory(cef);
        return chartPanel;
        }

MitchBuell
Posts: 47
Joined: Thu Dec 11, 2008 7:59 pm

Post by MitchBuell » Mon Dec 22, 2008 3:00 pm

First thing to do: move the

Code: Select all

MyChartEditorFactory cef = new MyChartEditorFactory();  // here are the important lines
ChartEditorManager.setChartEditorFactory(cef);
out of getChart() and into your public static void main(String[] args) or Constructor. These two lines have to be called before you make your chart.

Here's a quick demo application that works with my own chart editor. I set my chart editor before I use any other JFreeChart stuff.

Code: Select all

package demo;

import javax.swing.*;

import org.jfree.chart.*;
import org.jfree.chart.editor.ChartEditorManager;	// For ChartEditorManager.setChartEditorFactory()
import org.jfree.chart.plot.*;
import org.jfree.data.xy.*;

import demo.myeditor.MyChartEditorFactory;	// My chart editor

public class Demo
{
	public static void main(String[] args)
    {
		// Tell JFreeChart to use my chart editor
		ChartEditorManager.setChartEditorFactory(new MyChartEditorFactory());
		
		// Make a demo series
		XYSeries series1 = new XYSeries("Series1");
		for (int i = 0; i < 10; ++i)
			series1.add(i, Math.random());
		
		XYSeriesCollection collection = new XYSeriesCollection();
		collection.addSeries(series1);
		
		// Make a demo chart
		JFreeChart chart = ChartFactory.createXYLineChart(
	            "Chart Editor Demo",
	            "X",
	            "Y",
	            collection,
	            PlotOrientation.VERTICAL,
	            true,
	            true,
	            false
	        );
		
		ChartPanel chartPanel = new ChartPanel(chart);
		
		JFrame frame = new JFrame("Demo");
		frame.add(chartPanel);
		frame.pack();
		frame.setVisible(true);
    }
}

PollerJava
Posts: 81
Joined: Wed Jul 25, 2007 10:40 am

Post by PollerJava » Mon Dec 22, 2008 4:34 pm

Hello MitchBuell,

obviously I'am to stupid to create such an application,
Would it be possible to send you my little project, I don't know whats going wrong,

Thanks a lot,
All the best,
Poller

MitchBuell
Posts: 47
Joined: Thu Dec 11, 2008 7:59 pm

Post by MitchBuell » Mon Dec 22, 2008 5:38 pm

Did you try to run my Demo application?

PollerJava
Posts: 81
Joined: Wed Jul 25, 2007 10:40 am

Post by PollerJava » Tue Dec 23, 2008 8:10 am

MitchBuell wrote:Did you try to run my Demo application?
Yes, of course, but I got the exception:

Code: Select all

Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
        at demo.myeditor.MyChartEditorFactory.createEditor(MyChartEditorFactory.java:73)
        at org.jfree.chart.editor.ChartEditorManager.getChartEditor(ChartEditorManager.java:94)
        at org.jfree.chart.ChartPanel.doEditChartProperties(ChartPanel.java:2108)
        at org.jfree.chart.ChartPanel.actionPerformed(ChartPanel.java:1330)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)

at this line of code:

Code: Select all

package demo.myeditor;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.editor.ChartEditor;

public class MyChartEditorFactory implements org.jfree.chart.editor.ChartEditorFactory {

   
    public MyChartEditorFactory() {
        System.out.println("Das ist der Konstruktor MyChartEditorFactory");
        }
    public ChartEditor createEditor(JFreeChart chart) {
        System.out.println("*** createEditor in der Klasse MyChartEditorFactory");
        return new MyChartEditor(chart);   // here I get the fault!!!
        }
}

Thanks a lot for your help and patience,
All the best,
Poller

PollerJava
Posts: 81
Joined: Wed Jul 25, 2007 10:40 am

Post by PollerJava » Tue Dec 23, 2008 9:34 am

I solved the problem, thanks a lot,
all the best,
Poller

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Post by matinh » Tue Dec 23, 2008 2:52 pm

PollerJava wrote:I solved the problem, thanks a lot,
all the best,
Poller
It would be nice if you can post the solution to your problem, so others can take advantage of this.

thanks,
- martin

Locked