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
31
32
33
34
35 public class ModifyAddChartMaker {
36 private static final int MODIFYING = 0;
37 private static final int ADDING = 1;
38
39 private final ReportConfig config;
40 private final Repository repository;
41 private final int width;
42 private double[][] categories;
43 private final ArrayList categoryNames = new ArrayList();
44
45
46
47
48
49
50 public ModifyAddChartMaker(final ReportConfig config, final int width) {
51 this.config = config;
52 this.repository = config.getRepository();
53 this.width = width;
54 }
55
56 public ChartImage toFile() {
57 final Collection authors = this.repository.getAuthors();
58 final Iterator it = authors.iterator();
59 while (it.hasNext()) {
60 final Author author = (Author) it.next();
61 if (!config.isDeveloper(author)) {
62 continue;
63 }
64 categoryNames.add(author.getRealName());
65 }
66 Collections.sort(categoryNames);
67
68 categories = new double[2][categoryNames.size()];
69 for (int j = 0; j < categoryNames.size(); j++) {
70 categories[MODIFYING][j] = 0;
71 categories[ADDING][j] = 0;
72 }
73
74 final Iterator commitIt = this.repository.getCommits().iterator();
75 while (commitIt.hasNext()) {
76 final Commit commit = (Commit) commitIt.next();
77 final Set commitRevList = commit.getRevisions();
78 final Iterator commitRevIt = commitRevList.iterator();
79 final String authorName = commit.getAuthor().getRealName();
80 if (authorName == null) {
81 continue;
82 }
83 final int author = categoryNames.indexOf(authorName);
84 if (author == -1) {
85 continue;
86 }
87 int linesAdded = 0;
88 int linesRemoved = 0;
89 while (commitRevIt.hasNext()) {
90 final Revision revision = (Revision) commitRevIt.next();
91 if (revision.getLinesDelta() > 0) {
92 linesAdded += revision.getLinesDelta() + revision.getReplacedLines();
93 linesRemoved += revision.getReplacedLines();
94 } else {
95 linesAdded += revision.getReplacedLines();
96 linesRemoved += -revision.getLinesDelta() + revision.getReplacedLines();
97 }
98 }
99 if (linesAdded == linesRemoved) {
100 categories[MODIFYING][author] += linesAdded;
101 }
102 if (linesAdded < linesRemoved) {
103 categories[MODIFYING][author] += linesRemoved;
104 }
105 if (linesAdded > linesRemoved) {
106 categories[ADDING][author] += linesAdded - linesRemoved;
107 categories[MODIFYING][author] += linesRemoved;
108 }
109 }
110
111 for (int i = 0; i < categoryNames.size(); i++) {
112 final double maxLines = categories[MODIFYING][i] + categories[ADDING][i];
113 for (int k = 0; k < 2; k++) {
114 categories[k][i] *= (100 / maxLines);
115 }
116 }
117
118 final DefaultCategoryDataset data = new DefaultCategoryDataset();
119 for (int i = 0; i < categories[MODIFYING].length; i++) {
120 data.addValue(categories[MODIFYING][i], "modifying", (Comparable) categoryNames.get(i));
121 }
122 for (int j = 0; j < categories[ADDING].length; j++) {
123 data.addValue(categories[ADDING][j], "adding", (Comparable) categoryNames.get(j));
124 }
125
126
127
128
129 final JFreeChart chart = ChartFactory.createStackedBarChart(this.config.getProjectName() + ": " + Messages.getString("AUTHOR_ACTIVITY_TITLE"), "", "%",
130 data, PlotOrientation.HORIZONTAL, true, false, false);
131
132 final CategoryPlot plot = chart.getCategoryPlot();
133
134 final CategoryItemRenderer renderer = plot.getRenderer();
135 renderer.setSeriesPaint(0, Color.yellow);
136 renderer.setSeriesPaint(1, Color.green);
137
138 final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
139 rangeAxis.setTickUnit(new NumberTickUnit(20.0, new DecimalFormat("0")));
140 rangeAxis.setUpperBound(100.0);
141
142 chart.getLegend().setPosition(RectangleEdge.TOP);
143
144 return this.config.createChartImage("activity.png", Messages.getString("AUTHOR_ACTIVITY_TITLE"), chart, new Dimension(width, 19 * this.categoryNames
145 .size() + 110));
146 }
147 }