KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > inspectors > metrics > reporting > LocCharts


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Johannes Bellert
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * URL: http://www.pavelvlasov.com/pv/content/menu.show?id=products.jtaste
21  * e-Mail: Johannes.Bellert@ercgroup.com
22  */

23 package org.hammurapi.inspectors.metrics.reporting;
24
25 import java.awt.Color JavaDoc;
26 import java.awt.GradientPaint JavaDoc;
27
28 import org.hammurapi.inspectors.metrics.statistics.DescriptiveStatistic;
29 import org.hammurapi.inspectors.metrics.statistics.IntVector;
30 import org.jfree.chart.ChartFactory;
31 import org.jfree.chart.ChartPanel;
32 import org.jfree.chart.JFreeChart;
33 import org.jfree.chart.axis.NumberAxis;
34 import org.jfree.chart.axis.ValueAxis;
35 import org.jfree.chart.plot.PlotOrientation;
36 import org.jfree.chart.plot.XYPlot;
37 import org.jfree.chart.renderer.xy.XYItemRenderer;
38 import org.jfree.data.DomainOrder;
39 import org.jfree.data.general.AbstractDataset;
40 import org.jfree.data.general.DatasetChangeListener;
41 import org.jfree.data.xy.IntervalXYDataset;
42 import org.jfree.ui.ApplicationFrame;
43 import org.jfree.ui.RefineryUtilities;
44
45
46 //!! job variablize criteria and job Path program and for XSL
47
public class LocCharts extends ApplicationFrame {
48
49     private IntVector locList = new IntVector();
50     IntVector distinctValues = new IntVector();
51     IntVector frequencies = new IntVector();
52     String JavaDoc chartName = "NA";
53     private int defectCriteria = 120;
54     private int graphicDimX = 500;
55     private int graphicDimY = 300;
56     private Integer JavaDoc chartDebugWindow;
57
58     public LocCharts( String JavaDoc _chartName, int _defectCriteria, IntVector _locList, Integer JavaDoc _chartDebugWindow) {
59         //!! job: Wrong super type .
60
super(_chartName);
61         chartName = _chartName;
62
63         defectCriteria = _defectCriteria;
64         locList = _locList;
65         chartDebugWindow = _chartDebugWindow;
66     }
67
68     public JFreeChart generateChart() {
69         JFreeChart chart;
70
71         new DescriptiveStatistic().frequencies(locList, distinctValues, frequencies);
72         //!! job: wrong method name and tailoring
73
chart = this.copyDeepXYSeries(distinctValues, frequencies);
74         // System.out.println( distinctValues );
75
// System.out.println( frequencies );
76
customizeChartBars(chart);
77         this.pack();
78
79         if ( chartDebugWindow.intValue() > 0){
80             RefineryUtilities.centerFrameOnScreen(this);
81             this.setVisible(true);
82         }
83         return chart;
84     }
85     private void customizeChartBars(JFreeChart chart) {
86         // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
87
// set the background color for the chart...
88
chart.setBackgroundPaint(new Color JavaDoc(0xBBBBDD));
89         // get a reference to the plot for further customisation...
90
XYPlot plot = chart.getXYPlot();
91         // set the range axis to display integers only...
92
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
93         rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
94         // disable bar outlines...
95
XYItemRenderer renderer = (XYItemRenderer) plot.getRenderer();
96         // renderer.ssetDrawBarOutline(false);
97
GradientPaint JavaDoc gp1 = new GradientPaint JavaDoc(0.0f, 0.0f, Color.green, 0.0f, 0.0f, Color.lightGray);
98         GradientPaint JavaDoc gp2 = new GradientPaint JavaDoc(0.0f, 0.0f, Color.red, 0.0f, 0.0f, Color.lightGray);
99         renderer.setSeriesPaint(0, gp1);
100         renderer.setSeriesPaint(1, gp2);
101         ValueAxis domainAxis = plot.getDomainAxis();
102         //domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
103
//domainAxis.setMaxCategoryLabelWidthRatio(5.0f);
104
// OPTIONAL CUSTOMISATION COMPLETED.
105
}
106
107
108
109     private JFreeChart copyDeepXYSeries(IntVector distinctValues, IntVector frequencies) {
110         IntervalXYDataset dataset = new LocIntervalXYDataset();
111         // create the chart...
112
JFreeChart chart = ChartFactory.createXYBarChart(chartName, // chart
113
// title
114
"NCSS", // domain axis label
115
"Occurance", // range axis label
116
dataset, // data
117
PlotOrientation.VERTICAL, true, // include legend
118
true, false);
119         // get a reference to the plot for further customisation...
120
XYPlot plot = chart.getXYPlot();
121         plot.setDomainAxis(new NumberAxis("Not Commented Source Statements"));
122         // add the chart to a panel...
123
ChartPanel chartPanel = new ChartPanel(chart);
124         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(graphicDimX, graphicDimY));
125         setContentPane(chartPanel);
126         return chart;
127     }
128
129
130     class LocIntervalXYDataset extends AbstractDataset implements IntervalXYDataset {
131         private int barWidth = -1;
132         /**
133          * Creates a new dataset.
134          */

135         public LocIntervalXYDataset() {
136             super();
137         }
138         /**
139          * Returns the number of series in the dataset.
140          *
141          * @return the number of series in the dataset.
142          */

143         public int getSeriesCount() {
144             return 2;
145         }
146         /**
147          * Returns the name of a series.
148          *
149          * @param series
150          * the series (zero-based index).
151          *
152          * @return the series name.
153          */

154         public String JavaDoc getSeriesName(int series) {
155             if (series == 0) {
156                 return "Good";
157             } else if (series == 1) {
158                 return "Defect";
159             }
160             return "N/A";
161         }
162         /**
163          * Returns the number of items in a series.
164          *
165          * @param series
166          * the series (zero-based index).
167          *
168          * @return the number of items within a series.
169          */

170         public int getItemCount(int series) {
171             return distinctValues.size();
172         }
173         /**
174          * Returns the x-value for an item within a series.
175          * <P>
176          * The implementation is responsible for ensuring that the x-values are
177          * presented in ascending order.
178          *
179          * @param series
180          * the series (zero-based index).
181          * @param item
182          * the item (zero-based index).
183          *
184          * @return the x-value for an item within a series.
185          */

186         public double getXValue(int series, int item) {
187             if (series == 0 && distinctValues.elementAt(item) < defectCriteria) {
188                 // "Good";
189
return distinctValues.elementAt(item);
190             } else if (series == 1 && distinctValues.elementAt(item) > defectCriteria) {
191                 // "Defect";
192
return distinctValues.elementAt(item);
193             }
194             return 0;
195         }
196         /**
197          * Returns the y-value for an item within a series.
198          *
199          * @param series
200          * the series (zero-based index).
201          * @param item
202          * the item (zero-based index).
203          *
204          * @return the y-value for an item within a series.
205          */

206         public double getYValue(int series, int item) {
207             // return new Integer( frequencies.elementAt(item) );
208
if (series == 0 && distinctValues.elementAt(item) < defectCriteria) {
209                 // "Good";
210
return frequencies.elementAt(item);
211             } else if (series == 1 && distinctValues.elementAt(item) >= defectCriteria) {
212                 // "Defect";
213
return frequencies.elementAt(item);
214             }
215             return 0;
216         }
217         /**
218          * Returns the starting X value for the specified series and item.
219          *
220          * @param series
221          * the series (zero-based index).
222          * @param item
223          * the item within a series (zero-based index).
224          *
225          * @return the start x value.
226          */

227         public double getStartXValue(int series, int item) {
228             //return new Integer( distinctValues.elementAt(item) ) ;
229
if (series == 0 && distinctValues.elementAt(item) < defectCriteria) {
230                 // "Good";
231
return distinctValues.elementAt(item);
232             } else if (series == 1 && distinctValues.elementAt(item) > defectCriteria) {
233                 // "Defect";
234
return distinctValues.elementAt(item);
235             }
236             return 0;
237         }
238         /**
239          * Returns the ending X value for the specified series and item.
240          *
241          * @param series
242          * the series (zero-based index).
243          * @param item
244          * the item within a series (zero-based index).
245          *
246          * @return the end x value.
247          */

248         public double getEndXValue(int series, int item) {
249             barWidth = computeBarWidth();
250
251             //!! job: compute bar width
252
if (series == 0 && distinctValues.elementAt(item) < defectCriteria) {
253                 // "Good";
254
return distinctValues.elementAt(item) + barWidth;
255             } else if (series == 1 && distinctValues.elementAt(item) > defectCriteria) {
256                 // "Defect";
257
return distinctValues.elementAt(item) + barWidth;
258             }
259             return 0;
260         }
261         /*
262          * barWidth = 1 if MAX of distinctValues > 300 barWidth = 2 if MAX of
263          * distinctValues < 300 > 200 barWidth = 3 if MAX of distinctValues <
264          * 100
265          */

266         public int computeBarWidth() {
267             // lazy init
268
/* if (barWidth < 0) {
269                 int max = distinctValues.elementAt(distinctValues.size() - 1);
270                 if (max > 350) {
271                     barWidth = 1;
272                 };
273                 if (max > 50 && max <= 350) {
274                     barWidth = 2;
275                 };
276                 if (max <= 50) {
277                     barWidth = 3;
278                 };
279                 // System.out.println(max + " barWidth " + barWidth);
280                  *
281                  */

282
283             return 1;
284         }
285         /**
286          * Returns the starting Y value for the specified series and item.
287          *
288          * @param series
289          * the series (zero-based index).
290          * @param item
291          * the item within a series (zero-based index).
292          *
293          * @return the start y value.
294          */

295         public double getStartYValue(int series, int item) {
296             // return new Integer( frequencies.elementAt(item) );
297
if (series == 0 && distinctValues.elementAt(item) < defectCriteria) {
298                 // "Good";
299
return frequencies.elementAt(item);
300             } else if (series == 1 && distinctValues.elementAt(item) > defectCriteria) {
301                 // "Defect";
302
return frequencies.elementAt(item);
303             }
304             return 0;
305         }
306         /**
307          * Returns the ending Y value for the specified series and item.
308          *
309          * @param series
310          * the series (zero-based index).
311          * @param item
312          * the item within a series (zero-based index).
313          *
314          * @return the end y value.
315          */

316         public double getEndYValue(int series, int item) {
317             // return new Integer( frequencies.elementAt(item) );
318
if (series == 0 && distinctValues.elementAt(item) < defectCriteria) {
319                 // "Good";
320
return frequencies.elementAt(item);
321             } else if (series == 1 && distinctValues.elementAt(item) > defectCriteria) {
322                 // "Defect";
323
return frequencies.elementAt(item);
324             }
325             return 0;
326         }
327         /**
328          * Registers an object for notification of changes to the dataset.
329          *
330          * @param listener
331          * the object to register.
332          */

333         public void addChangeListener(DatasetChangeListener listener) {
334         }
335         /**
336          * Deregisters an object for notification of changes to the dataset.
337          *
338          * @param listener
339          * the object to deregister.
340          */

341         public void removeChangeListener(DatasetChangeListener listener) {
342             
343         }
344         
345         public Number JavaDoc getStartX(int series, int item) {
346             return new Double JavaDoc(getStartXValue(series, item));
347         }
348         
349         public Number JavaDoc getEndX(int series, int item) {
350             return new Double JavaDoc(getEndXValue(series, item));
351         }
352         
353         public Number JavaDoc getStartY(int series, int item) {
354             return new Double JavaDoc(getStartYValue(series, item));
355         }
356         
357         public Number JavaDoc getEndY(int series, int item) {
358             return new Double JavaDoc(getEndYValue(series, item));
359         }
360         
361         public DomainOrder getDomainOrder() {
362             return DomainOrder.ASCENDING;
363         }
364         
365         public Number JavaDoc getX(int series, int item) {
366             return new Double JavaDoc(getXValue(series, item));
367         }
368         
369         public Number JavaDoc getY(int series, int item) {
370             return new Double JavaDoc(getYValue(series, item));
371         }
372         
373     }
374     /**
375      * @return Returns the graphicDimX.
376      */

377     public int getGraphicDimX() {
378         return graphicDimX;
379     }
380     /**
381      * @param graphicDimX The graphicDimX to set.
382      */

383     public void setGraphicDimX(int graphicDimX) {
384         this.graphicDimX = graphicDimX;
385     }
386     /**
387      * @return Returns the graphicDimY.
388      */

389     public int getGraphicDimY() {
390         return graphicDimY;
391     }
392     /**
393      * @param graphicDimY The graphicDimY to set.
394      */

395     public void setGraphicDimY(int graphicDimY) {
396         this.graphicDimY = graphicDimY;
397     }
398     /**
399      * @return Returns the chartName.
400      */

401     public String JavaDoc getChartName() {
402         return chartName;
403     }
404     /**
405      * @return Returns the defectCriteria.
406      */

407     public int getDefectCriteria() {
408         return defectCriteria;
409     }
410     /**
411      * @return Returns the distinctValues.
412      */

413     public IntVector getDistinctValues() {
414         return distinctValues;
415     }
416     /**
417      * @return Returns the frequencies.
418      */

419     public IntVector getFrequencies() {
420         return frequencies;
421     }
422     /**
423      * @return Returns the locList.
424      */

425     public IntVector getLocList() {
426         return locList;
427     }
428
429 }
430
Popular Tags