001    /* PAVLOV -- Multiple Choice Study System
002     * Copyright (C) 2000 - 2004 T.J. Willis
003     *
004     * This program is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU General Public License
006     * as published by the Free Software Foundation; either version 2
007     * of the License, or (at your option) any later version.
008     *
009     * This program is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     * GNU General Public License for more details.
013     *
014     * You should have received a copy of the GNU General Public License
015     * along with this program; if not, write to the Free Software
016     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
017     *
018     * $Header: /cvsroot/pavlov/net/sourceforge/pavlov/pluglets/feedback/charts/JPieChartFeedbackPluglet.java,v 1.3 2004/05/10 15:22:10 tj_willis Exp $
019     *
020     *
021     * NOTE: This class is an interface between Pavlov and jCharts.
022     * jCharts is a registered trademark of Nathaniel G. Auvil
023     * see: http://jcharts.sourceforge.net
024     *      http://sourceforge.net/projects/jcharts
025     *
026     * jCharts includes software developed by the Apache Software Foundation 
027     * (http://www.apache.org/), namely the Apache XML Batik Project
028     */
029    package net.sourceforge.pavlov.pluglets.feedback.charts;
030    
031    import java.util.*;
032    import javax.swing.*;
033    import java.awt.*;
034    import net.sourceforge.pavlov.pluglets.feedback.AbstractFeedbackPluglet;
035    import net.sourceforge.pavlov.swing.HelpSystem;
036    
037    import org.jCharts.chartData.*;
038    import org.jCharts.nonAxisChart.*;
039    import org.jCharts.properties.*;
040    
041    
042    /**
043     * Describe class <code>JPieChartFeedbackPluglet</code> here.
044     *
045     * @author <a href="mailto:"></a>
046     * @version 1.0
047     */
048    public abstract class JPieChartFeedbackPluglet
049        extends AbstractFeedbackPluglet
050    {
051        /**
052         * This pluglet's JMenuBar.
053         *
054         */
055        protected JMenuBar foo;
056        /**
057         * This pluglet's HelpSystem.
058         *
059         */
060        protected HelpSystem help;
061        /**
062         * The ChartPanel to display the PieChart in.
063         *
064         */
065        protected ChartPanel hmm; 
066        private PieChart2D pieChart;
067        private String title;
068        private double[] data;
069        private String[] legendLabels;
070        private Paint[] paints;
071        private PieChartDataSet pieChartDataSet;
072        private ChartProperties chartProperties;
073        private PieChart2DProperties pieChart2DProperties;
074        private LegendProperties legendProperties;
075        private String[] labels;
076    
077        /**
078         * Creates a new <code>JPieChartFeedbackPluglet</code> instance.
079         *
080         */
081        public JPieChartFeedbackPluglet()
082        {
083            super();
084            setResizable(true);
085        }
086    
087        /**
088         * Describe <code>init</code> method here.
089         *
090         */
091        public void init(){
092            hmm = new ChartPanel();
093            Container cp = getContentPane();
094            cp.setLayout(new BorderLayout());
095            cp.add(hmm,BorderLayout.CENTER);
096    
097            pack();
098            //setVisible( true );
099    
100            preInitChart();
101    
102            hmm.setText(getDescription());
103    
104            foo = new JMenuBar();
105            setJMenuBar(foo);
106            pack();
107        }
108    
109        /**
110         * Implement to set the chart's title.
111         *
112         * @return a <code>String</code> value
113         */
114        public abstract String getChartTitle();
115        /**
116         * Implement to identify the slices.
117         *
118         * @return a <code>String[]</code> value
119         */
120        public abstract String[] getLegendLabels();
121    
122    
123        /**
124         * Implement to color the slices.
125         *
126         * @return a <code>Paint[]</code> value
127         */
128        public  abstract Paint[] getPaints();
129    
130        private void preInitChart()
131        {
132            labels= getLegendLabels();
133            paints= getPaints();
134            title = getChartTitle();
135    
136            pieChart2DProperties= new PieChart2DProperties();
137        }
138    
139        /**
140         * Describe <code>getLegendProperties</code> method here.
141         *
142         * @return a <code>LegendProperties</code> value
143         */
144        public LegendProperties getLegendProperties()
145        {
146            return new LegendProperties();
147        }
148        /**
149         * Describe <code>getChartProperties</code> method here.
150         *
151         * @return a <code>ChartProperties</code> value
152         */
153        public ChartProperties getChartProperties()
154        {
155            return new ChartProperties();
156        }
157        /**
158         * Describe <code>getPieChart2DProperties</code> method here.
159         *
160         * @return a <code>PieChart2DProperties</code> value
161         */
162        public PieChart2DProperties getPieChart2DProperties()
163        {
164            return new PieChart2DProperties();
165        }
166    
167        /**
168         * Describe <code>setData</code> method here.
169         *
170         * @param v an <code>ArrayList</code> value
171         */
172        protected void setData(ArrayList v)
173        {
174            AbstractListAdaptor ala = new AbstractListAdaptor(v.size(),1);
175            data= ala.toArray(v);
176    
177            pieChart2DProperties = getPieChart2DProperties();
178    
179            try {
180                pieChartDataSet= 
181                    new PieChartDataSet(title, data, labels, paints, pieChart2DProperties );
182            } catch (Exception e) {
183                e.printStackTrace();
184            }
185            legendProperties = getLegendProperties();
186            chartProperties = getChartProperties();
187            pieChart= 
188                new PieChart2D( pieChartDataSet, 
189                                legendProperties, 
190                                chartProperties, 
191                                hmm.getWidth(), hmm.getHeight() );
192    
193            hmm.setChart(pieChart);
194        }
195    
196        /**
197         * Lets the ChartPanel reshape itself effectively.
198         *
199         * @param x an <code>int</code> value
200         * @param y an <code>int</code> value
201         * @param w an <code>int</code> value
202         * @param h an <code>int</code> value
203         */
204        public void setBounds(int x, int y,int w,int h)
205        {
206            super.setBounds(x,y,w,h);
207            if(pieChart != null && pieChartDataSet !=null)
208                try {
209                    pieChart = 
210                        new PieChart2D( pieChartDataSet, 
211                                        legendProperties, 
212                                        chartProperties, 
213                                        hmm.getWidth(),
214                                        hmm.getHeight());
215                    //(int)((1.0*w/2.0)-10.0), 
216                    //          (int)((1.0*h/2.0)-10.0));
217                    hmm.setChart(pieChart);
218                } catch (Exception e) {
219                    e.printStackTrace();
220                }
221    
222        }
223    
224        /**
225         * Describe <code>getPreferredSize</code> method here.
226         *
227         * @return a <code>java.awt.Dimension</code> value
228         */
229        public java.awt.Dimension getPreferredSize()
230        {
231            return new java.awt.Dimension(360,250);
232        }
233    
234    
235        /**
236         * Trick to get the ChartPanel to redraw correctly.
237         *
238         * @param g a <code>Graphics2D</code> value
239         */
240        public void paint(Graphics2D g)
241        {
242            super.paintComponents(g);
243        }
244    
245    }
246    
247