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/JChartFeedbackPluglet.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    import java.util.*;
031    import javax.swing.*;
032    import java.awt.*;
033    import net.sourceforge.pavlov.pluglets.feedback.AbstractFeedbackPluglet;
034    import net.sourceforge.pavlov.swing.HelpSystem;
035    
036    import org.jCharts.axisChart.*;
037    import org.jCharts.chartData.*;
038    import org.jCharts.properties.*;
039    import org.jCharts.types.*;
040    
041    
042    /**
043     * A base class for FeedbackPluglets that use JCharts.
044     *
045     * @author <a href="mailto:"></a>
046     * @version 1.0
047     */
048    public abstract class JChartFeedbackPluglet
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        private ChartPanel hmm; 
062        private AxisChart axisChart;
063        private String[] xAxisLabels;
064        private String xAxisTitle;
065        private String yAxisTitle;
066        private String title;
067        private DataSeries dataSeries; 
068        private double[][] data;
069        private String[] legendLabels;
070        private Paint[] paints;
071        private Stroke[] strokes;
072        private Shape[] shapes;
073        private AxisChartDataSet axisChartDataSet;
074        private ChartProperties chartProperties;
075        private AxisProperties axisProperties;
076        private LegendProperties legendProperties;
077        //LineChartProperties lineChartProperties;
078    
079        /**
080         * Creates a new <code>JChartFeedbackPluglet</code> instance.
081         *
082         */
083        public JChartFeedbackPluglet()
084        {
085            super();
086            setResizable(true);
087        }
088    
089        /**
090         * Describe <code>init</code> method here.
091         *
092         */
093        public void init(){
094            hmm = new ChartPanel();
095            Container cp = getContentPane();
096            cp.setLayout(new BorderLayout());
097            cp.add(hmm,BorderLayout.CENTER);
098    
099            pack();
100            //setVisible( true );
101    
102            preInitChart();
103    
104            hmm.setText(getDescription());
105    
106            foo = new JMenuBar();
107            setJMenuBar(foo);
108            pack();
109        }
110    
111    
112        /**
113         * Describe <code>getXAxisTitle</code> method here.
114         *
115         * @return a <code>String</code> value
116         */
117        public abstract String getXAxisTitle();
118        /**
119         * Describe <code>getYAxisTitle</code> method here.
120         *
121         * @return a <code>String</code> value
122         */
123        public abstract String getYAxisTitle();
124        /**
125         * Describe <code>getChartTitle</code> method here.
126         *
127         * @return a <code>String</code> value
128         */
129        public abstract String getChartTitle();
130        /**
131         * Describe <code>getLegendLabels</code> method here.
132         *
133         * @return a <code>String[]</code> value
134         */
135        public abstract String[] getLegendLabels();
136    
137        /**
138         * Describe <code>getStrokes</code> method here.
139         *
140         * @return a <code>Stroke[]</code> value
141         */
142        public Stroke[] getStrokes() { 
143            Stroke[] ret = { LineChartProperties.DEFAULT_LINE_STROKE }; 
144            return ret;
145        }
146        /**
147         * Describe <code>getShapes</code> method here.
148         *
149         * @return a <code>Shape[]</code> value
150         */
151        public Shape[] getShapes() {
152            Shape[] ret ={ null }; 
153            return ret;
154        }
155    
156        /**
157         * Describe <code>getPaints</code> method here.
158         *
159         * @return a <code>Paint[]</code> value
160         */
161        public  Paint[] getPaints()
162        {
163            Paint [] ret = { Color.black };
164            return ret;
165        }
166    //     public abstract String getLegendLabels();
167    //     public abstract String getStrokes();
168    //     public abstract String getShapes();
169    //     public abstract String getLineChartProperties();
170    //     public abstract String getPaints();
171    //     public abstract String getChartProperties();
172    //     public abstract String getAxisProperties();
173    //     public abstract String getLegendProperties();
174    
175        /**
176         * Describe <code>getChartTypeProperties</code> method here.
177         *
178         * @return a <code>ChartTypeProperties</code> value
179         */
180        public ChartTypeProperties getChartTypeProperties()
181        {
182            strokes= getStrokes();
183            shapes= getShapes();
184            return new LineChartProperties( strokes, shapes );
185        }
186    
187        /**
188         * Describe <code>getChartType</code> method here.
189         *
190         * @return a <code>ChartType</code> value
191         */
192        public ChartType getChartType()
193        {
194            return ChartType.LINE;
195        }
196    
197        private void preInitChart()
198        {
199            xAxisTitle= getXAxisTitle(); //"Questions";
200            yAxisTitle= getYAxisTitle(); //"Score";
201            title= getChartTitle();     //"Your Progress This Quiz";
202            legendLabels= getLegendLabels(); //new String[] { "Percentage Score" };
203            //lineChartProperties= getLineChartProperties();
204            paints= getPaints();
205            chartProperties= new ChartProperties();
206            axisProperties= new AxisProperties();
207            legendProperties= new LegendProperties();
208        }
209    
210        private void makeXAxisLabels(int cols)
211        {
212            xAxisLabels=  new String[1];
213            xAxisLabels[0] = new String(".");  
214            // TOFIX: what's the deal with null labels...?
215            ArrayList<String> xlabs = new ArrayList<String>(cols);
216            for(int i=0;i<cols;i++)
217                {
218                    xlabs.add((String)new String("."));
219                }
220            xAxisLabels = (String [])xlabs.toArray((String [])xAxisLabels);
221        }
222    
223    
224    
225    
226        /**
227         * Describe <code>setData</code> method here.
228         *
229         * @param v an <code>AbstractList</code> value
230         */
231        protected void setData(AbstractList v)
232        {
233            AbstractListAdaptor ala = new AbstractListAdaptor(v.size(),1);
234            ala.setRow(0,v);
235            setData(ala);
236        }
237    
238        /**
239         * Describe <code>setData</code> method here.
240         *
241         * @param ala an <code>AbstractListAdaptor</code> value
242         */
243        protected void setData(AbstractListAdaptor ala)
244        {
245            makeXAxisLabels(ala.columns());
246            
247            dataSeries = new DataSeries(xAxisLabels, xAxisTitle, yAxisTitle,title );
248    
249            data= ala.getArray();
250    
251            axisChartDataSet=null;
252            try {
253                //axisChartDataSet= new AxisChartDataSet( data, legendLabels, paints, ChartType.LINE, lineChartProperties );
254                axisChartDataSet= new AxisChartDataSet( data, legendLabels, paints, getChartType(), getChartTypeProperties() );
255            } catch (Exception e) {e.printStackTrace();}
256            dataSeries.addIAxisPlotDataSet( axisChartDataSet );
257    
258            axisChart= new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, hmm.getWidth(),hmm.getHeight());
259            hmm.setChart(axisChart);
260        }
261    
262        /**
263         * If the containing component's size has changed, let the chart know
264         * and let it rerender itself.
265         *
266         * @param x an <code>int</code> value
267         * @param y an <code>int</code> value
268         * @param w an <code>int</code> value
269         * @param h an <code>int</code> value
270         */
271        public void setBounds(int x, int y,int w,int h)
272        {
273            super.setBounds(x,y,w,h);
274            if(axisChart != null && dataSeries !=null)
275                try {
276                    axisChart= new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, hmm.getWidth(),hmm.getHeight());
277                    hmm.setChart(axisChart);
278                } catch (Exception e) {
279                    
280                }
281    
282        }
283    
284        /**
285         * Describe <code>getPreferredSize</code> method here.
286         *
287         * @return a <code>java.awt.Dimension</code> value
288         */
289        public java.awt.Dimension getPreferredSize()
290        {
291            return new java.awt.Dimension(360,250);
292        }
293    
294    
295        /**
296         * Trick to get the chart to repaint correctly.
297         *
298         * @param g a <code>Graphics2D</code> value
299         */
300        public void paint(Graphics2D g)
301        {
302            super.paintComponents(g);
303        }
304    
305    }
306    
307