Coverage Report - net.sf.statcvs.charts.ModifyAddChartMaker
 
Classes in this File Line Coverage Branch Coverage Complexity
ModifyAddChartMaker
0%
0/97
0%
0/60
8.5
 
 1  
 package net.sf.statcvs.charts;
 2  
 
 3  
 import java.awt.Color;
 4  
 import java.awt.Dimension;
 5  
 import java.text.DecimalFormat;
 6  
 import java.util.ArrayList;
 7  
 import java.util.Collection;
 8  
 import java.util.Collections;
 9  
 import java.util.Iterator;
 10  
 import java.util.Set;
 11  
 
 12  
 import net.sf.statcvs.Messages;
 13  
 import net.sf.statcvs.model.Author;
 14  
 import net.sf.statcvs.model.Commit;
 15  
 import net.sf.statcvs.model.Repository;
 16  
 import net.sf.statcvs.model.Revision;
 17  
 import net.sf.statcvs.output.ReportConfig;
 18  
 
 19  
 import org.jfree.chart.ChartFactory;
 20  
 import org.jfree.chart.JFreeChart;
 21  
 import org.jfree.chart.axis.NumberAxis;
 22  
 import org.jfree.chart.axis.NumberTickUnit;
 23  
 import org.jfree.chart.plot.CategoryPlot;
 24  
 import org.jfree.chart.plot.PlotOrientation;
 25  
 import org.jfree.chart.renderer.category.CategoryItemRenderer;
 26  
 import org.jfree.data.category.DefaultCategoryDataset;
 27  
 import org.jfree.ui.RectangleEdge;
 28  
 
 29  
 /**
 30  
  * Class for producing the "Author Activity: Modifying/Adding" chart
 31  
  * @author jentzsch
 32  
  * @author Richard Cyganiak (richard@cyganiak.de)
 33  
  * @version $Id: ModifyAddChartMaker.java,v 1.9 2009/04/25 16:36:20 benoitx Exp $
 34  
  */
 35  
 public class ModifyAddChartMaker {
 36  
     private static final String CHART_NAME = "activity";
 37  
     private static final int MODIFYING = 0;
 38  
     private static final int ADDING = 1;
 39  
 
 40  
     private final ReportConfig config;
 41  
     private final Repository repository;
 42  
     private final int width;
 43  0
     private double[][] categories;
 44  0
     private final ArrayList categoryNames = new ArrayList();
 45  
 
 46  
     /**
 47  
      * Creates a new ModifyAddChartMaker
 48  
      * @param content Repository
 49  
      * @param width width of the chart in pixels
 50  0
      */
 51  0
     public ModifyAddChartMaker(final ReportConfig config, final int width) {
 52  0
         this.config = config;
 53  0
         this.repository = config.getRepository();
 54  0
         this.width = width;
 55  0
     }
 56  
 
 57  0
     public ChartImage toFile() {
 58  0
         final Collection authors = this.repository.getAuthors();
 59  0
         final Iterator it = authors.iterator();
 60  0
         while (it.hasNext()) {
 61  0
             final Author author = (Author) it.next();
 62  0
             if (!config.isDeveloper(author)) {
 63  0
                 continue;
 64  0
             }
 65  0
             categoryNames.add(author.getRealName());
 66  0
         }
 67  0
         Collections.sort(categoryNames);
 68  0
 
 69  0
         categories = new double[2][categoryNames.size()];
 70  0
         for (int j = 0; j < categoryNames.size(); j++) {
 71  0
             categories[MODIFYING][j] = 0;
 72  0
             categories[ADDING][j] = 0;
 73  
         }
 74  0
 
 75  0
         final Iterator commitIt = this.repository.getCommits().iterator();
 76  0
         while (commitIt.hasNext()) {
 77  0
             final Commit commit = (Commit) commitIt.next();
 78  0
             final Set commitRevList = commit.getRevisions();
 79  0
             final Iterator commitRevIt = commitRevList.iterator();
 80  0
             final String authorName = commit.getAuthor().getRealName();
 81  0
             if (authorName == null) {
 82  0
                 continue;
 83  0
             }
 84  0
             final int author = categoryNames.indexOf(authorName);
 85  0
             if (author == -1) {
 86  0
                 continue;
 87  0
             }
 88  0
             int linesAdded = 0;
 89  0
             int linesRemoved = 0;
 90  0
             while (commitRevIt.hasNext()) {
 91  0
                 final Revision revision = (Revision) commitRevIt.next();
 92  0
                 if (revision.getLinesDelta() > 0) {
 93  0
                     linesAdded += revision.getLinesDelta() + revision.getReplacedLines();
 94  0
                     linesRemoved += revision.getReplacedLines();
 95  0
                 } else {
 96  0
                     linesAdded += revision.getReplacedLines();
 97  0
                     linesRemoved += -revision.getLinesDelta() + revision.getReplacedLines();
 98  0
                 }
 99  0
             }
 100  0
             if (linesAdded == linesRemoved) {
 101  0
                 categories[MODIFYING][author] += linesAdded;
 102  0
             }
 103  0
             if (linesAdded < linesRemoved) {
 104  0
                 categories[MODIFYING][author] += linesRemoved;
 105  0
             }
 106  0
             if (linesAdded > linesRemoved) {
 107  0
                 categories[ADDING][author] += linesAdded - linesRemoved;
 108  0
                 categories[MODIFYING][author] += linesRemoved;
 109  0
             }
 110  0
         }
 111  0
 
 112  0
         for (int i = 0; i < categoryNames.size(); i++) {
 113  0
             final double maxLines = categories[MODIFYING][i] + categories[ADDING][i];
 114  0
             for (int k = 0; k < 2; k++) {
 115  0
                 categories[k][i] *= (100 / maxLines);
 116  
             }
 117  
         }
 118  0
 
 119  0
         final DefaultCategoryDataset data = new DefaultCategoryDataset();
 120  0
         for (int i = 0; i < categories[MODIFYING].length; i++) {
 121  0
             data.addValue(categories[MODIFYING][i], "modifying", (Comparable) categoryNames.get(i));
 122  0
         }
 123  0
         for (int j = 0; j < categories[ADDING].length; j++) {
 124  0
             data.addValue(categories[ADDING][j], "adding", (Comparable) categoryNames.get(j));
 125  
         }
 126  
         //data.setSeriesName(MODIFYING, "modifying");
 127  
         //data.setSeriesName(ADDING, "adding");
 128  
         //data.setCategories(categoryNames.toArray());
 129  0
 
 130  0
         final JFreeChart chart = ChartFactory.createStackedBarChart(this.config.getProjectName() + ": " + Messages.getString("AUTHOR_ACTIVITY_TITLE"), "", "%",
 131  
                 data, PlotOrientation.HORIZONTAL, true, false, false);
 132  0
 
 133  0
         final CategoryPlot plot = chart.getCategoryPlot();
 134  0
         //plot.setSeriesPaint(new Paint[] { Color.yellow, Color.green });
 135  0
         final CategoryItemRenderer renderer = plot.getRenderer();
 136  0
         renderer.setSeriesPaint(0, Color.yellow);
 137  0
         renderer.setSeriesPaint(1, Color.green);
 138  0
 
 139  0
         final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
 140  0
         rangeAxis.setTickUnit(new NumberTickUnit(20.0, new DecimalFormat("0")));
 141  0
         rangeAxis.setUpperBound(100.0);
 142  0
 
 143  0
         chart.getLegend().setPosition(RectangleEdge.TOP);
 144  0
 
 145  0
         plot.setBackgroundPaint(ChartConfigUtil.getPlotColor(CHART_NAME));
 146  0
         chart.setBackgroundPaint(ChartConfigUtil.getBackgroundColor(CHART_NAME));
 147  
         //        final XYItemRenderer renderer = plot.getRenderer();
 148  
         //        ChartConfigUtil.configureStroke(CHART_NAME, renderer, data);
 149  
         //        ChartConfigUtil.configureShapes(CHART_NAME, renderer);
 150  0
         ChartConfigUtil.configureCopyrightNotice(CHART_NAME, chart);
 151  0
         ChartConfigUtil.configureChartBackgroungImage(CHART_NAME, chart);
 152  0
         ChartConfigUtil.configurePlotImage(CHART_NAME, chart);
 153  0
 
 154  0
         final Dimension dim = ChartConfigUtil.getDimension(CHART_NAME, new Dimension(config.getSmallChartSize().width, 19));
 155  0
         final int totalHeight = dim.height * this.categoryNames.size() + 110;
 156  0
         return this.config.createChartImage(CHART_NAME + ".png", Messages.getString("AUTHOR_ACTIVITY_TITLE"), chart, new Dimension(dim.width, totalHeight));
 157  
     }
 158  
 }