| 1 | |
package net.sf.statcvs.charts; |
| 2 | |
|
| 3 | |
import java.awt.Dimension; |
| 4 | |
import java.util.ArrayList; |
| 5 | |
import java.util.Date; |
| 6 | |
import java.util.Iterator; |
| 7 | |
import java.util.LinkedHashMap; |
| 8 | |
import java.util.List; |
| 9 | |
import java.util.Map; |
| 10 | |
|
| 11 | |
import net.sf.statcvs.model.Module; |
| 12 | |
import net.sf.statcvs.model.Repository; |
| 13 | |
import net.sf.statcvs.model.Revision; |
| 14 | |
import net.sf.statcvs.output.ReportConfig; |
| 15 | |
import net.sf.statcvs.util.IntegerMap; |
| 16 | |
|
| 17 | |
import org.jfree.chart.JFreeChart; |
| 18 | |
import org.jfree.chart.axis.ColorBar; |
| 19 | |
import org.jfree.chart.axis.DateAxis; |
| 20 | |
import org.jfree.chart.axis.SymbolAxis; |
| 21 | |
import org.jfree.chart.axis.ValueAxis; |
| 22 | |
import org.jfree.chart.plot.ContourPlot; |
| 23 | |
import org.jfree.data.contour.ContourDataset; |
| 24 | |
import org.jfree.data.contour.DefaultContourDataset; |
| 25 | |
import org.jfree.ui.RectangleEdge; |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
public class ModuleEvolutionChartMaker { |
| 34 | |
private static final int SLICE_MIN_PERCENT = 5; |
| 35 | |
|
| 36 | |
private final ReportConfig config; |
| 37 | |
private final String title; |
| 38 | |
private final String fileName; |
| 39 | 0 | private final List files = new ArrayList(); |
| 40 | |
private final String chartName; |
| 41 | |
private final Repository repository; |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | 0 | public ModuleEvolutionChartMaker(final String chartName, final ReportConfig config, final String title, final String fileName) { |
| 51 | 0 | this.chartName = chartName; |
| 52 | 0 | this.config = config; |
| 53 | 0 | this.title = title; |
| 54 | 0 | this.fileName = fileName; |
| 55 | 0 | this.repository = config.getRepository(); |
| 56 | 0 | } |
| 57 | |
|
| 58 | |
public ChartImage toFile() { |
| 59 | |
|
| 60 | 0 | final ContourDataset data = buildDs(); |
| 61 | |
|
| 62 | 0 | if (data == null) { |
| 63 | 0 | return null; |
| 64 | |
} |
| 65 | |
|
| 66 | 0 | final ValueAxis xAxis = new DateAxis("Date"); |
| 67 | 0 | final SymbolAxis yAxis = new SymbolAxis("Module", (String[]) repository.getModules().keySet().toArray(new String[0])); |
| 68 | |
|
| 69 | |
|
| 70 | 0 | yAxis.setInverted(true); |
| 71 | 0 | yAxis.setLowerMargin(0.0); |
| 72 | 0 | yAxis.setUpperMargin(0.0); |
| 73 | |
|
| 74 | 0 | final ColorBar zAxis = new ColorBar("Commit Activity (%)"); |
| 75 | 0 | zAxis.getAxis(); |
| 76 | |
|
| 77 | 0 | final ContourPlot plot = new ContourPlot(data, xAxis, yAxis, zAxis); |
| 78 | |
|
| 79 | |
|
| 80 | 0 | plot.setDataAreaRatio(0.0); |
| 81 | 0 | plot.setColorBarLocation(RectangleEdge.BOTTOM); |
| 82 | |
|
| 83 | 0 | final JFreeChart chart = new JFreeChart(config.getProjectName(), null, plot, false); |
| 84 | |
|
| 85 | 0 | plot.setBackgroundPaint(ChartConfigUtil.getPlotColor(chartName)); |
| 86 | 0 | chart.setBackgroundPaint(ChartConfigUtil.getBackgroundColor(chartName)); |
| 87 | 0 | ChartConfigUtil.configureCopyrightNotice(chartName, chart); |
| 88 | 0 | ChartConfigUtil.configureChartBackgroungImage(chartName, chart); |
| 89 | 0 | ChartConfigUtil.configurePlotImage(chartName, chart); |
| 90 | 0 | final Dimension dim = ChartConfigUtil.getDimension(chartName, config.getLargeChartSize()); |
| 91 | 0 | return this.config.createChartImage(this.fileName, this.title, chart, dim); |
| 92 | |
} |
| 93 | |
|
| 94 | |
private ContourDataset buildDs() { |
| 95 | 0 | final Map mapByDate = new LinkedHashMap(); |
| 96 | 0 | int max = 0; |
| 97 | 0 | final long elapsed = repository.getLastDate().getTime() - repository.getFirstDate().getTime(); |
| 98 | 0 | final long windowSize = Math.max(elapsed / 60, 1); |
| 99 | |
|
| 100 | 0 | IntegerMap commitsPerModule = new IntegerMap(); |
| 101 | |
|
| 102 | 0 | final Iterator itRev = repository.getRevisions().iterator(); |
| 103 | |
|
| 104 | 0 | long currentDate = -1; |
| 105 | 0 | while (itRev.hasNext()) { |
| 106 | 0 | final Revision rev = (Revision) itRev.next(); |
| 107 | |
|
| 108 | 0 | final Date date = rev.getDate(); |
| 109 | 0 | System.out.println("| Consider Rev " + rev.getDate() + " Mod:" + rev.getFile().getModule().getName() + " currentDate:" + currentDate); |
| 110 | 0 | if (currentDate == -1) { |
| 111 | 0 | currentDate = date.getTime(); |
| 112 | 0 | } else if (date.getTime() > currentDate + windowSize) { |
| 113 | |
|
| 114 | 0 | max = Math.max(commitsPerModule.max(), max); |
| 115 | 0 | mapByDate.put(new Date(currentDate), commitsPerModule); |
| 116 | |
|
| 117 | |
|
| 118 | 0 | commitsPerModule = new IntegerMap(); |
| 119 | |
|
| 120 | |
|
| 121 | 0 | final int fill = (int) ((date.getTime() - currentDate) / windowSize); |
| 122 | 0 | if (fill > 1) { |
| 123 | 0 | mapByDate.put(new Date(currentDate + windowSize), null); |
| 124 | |
} |
| 125 | 0 | currentDate += fill * windowSize; |
| 126 | |
} |
| 127 | 0 | commitsPerModule.inc(rev.getFile().getModule().getName()); |
| 128 | |
} |
| 129 | 0 | if (currentDate != -1) { |
| 130 | 0 | mapByDate.put(new Date(currentDate), commitsPerModule); |
| 131 | 0 | max = Math.max(commitsPerModule.max(), max); |
| 132 | |
} |
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | 0 | System.out.println("Module Commit"); |
| 172 | 0 | Iterator it = mapByDate.keySet().iterator(); |
| 173 | 0 | while (it.hasNext()) { |
| 174 | 0 | final Object key = it.next(); |
| 175 | 0 | final IntegerMap map = (IntegerMap) mapByDate.get(key); |
| 176 | 0 | System.out.println("Key:" + key); |
| 177 | 0 | if (map != null) { |
| 178 | 0 | final Iterator it2 = map.iteratorSortedByKey(); |
| 179 | 0 | while (it2.hasNext()) { |
| 180 | 0 | final Object o = it2.next(); |
| 181 | 0 | System.out.println("Val:" + o + "\t -> " + map.get(o)); |
| 182 | |
} |
| 183 | |
} |
| 184 | |
} |
| 185 | |
|
| 186 | 0 | final int groupCount = repository.getModules().size(); |
| 187 | |
|
| 188 | 0 | final int dateCount = mapByDate.size(); |
| 189 | 0 | final int numValues = dateCount * groupCount; |
| 190 | 0 | if (numValues == 0 || max == 0 || dateCount == 1) { |
| 191 | 0 | return null; |
| 192 | |
} |
| 193 | |
|
| 194 | 0 | final Date[] oDateX = new Date[numValues]; |
| 195 | 0 | final Double[] oDoubleY = new Double[numValues]; |
| 196 | 0 | final Double[] oDoubleZ = new Double[numValues]; |
| 197 | |
|
| 198 | 0 | it = mapByDate.keySet().iterator(); |
| 199 | 0 | for (int x = 0; x < dateCount; x++) { |
| 200 | 0 | if (!it.hasNext()) { |
| 201 | 0 | throw new RuntimeException("Invalid date count"); |
| 202 | |
} |
| 203 | 0 | final Date date = (Date) it.next(); |
| 204 | |
|
| 205 | 0 | final IntegerMap map = (IntegerMap) mapByDate.get(date); |
| 206 | 0 | if (map != null) { |
| 207 | 0 | final Iterator it2 = repository.getModules().values().iterator(); |
| 208 | 0 | for (int y = 0; y < groupCount; y++) { |
| 209 | 0 | if (!it2.hasNext()) { |
| 210 | 0 | throw new RuntimeException("Invalid group count"); |
| 211 | |
} |
| 212 | 0 | final Module group = (Module) it2.next(); |
| 213 | |
|
| 214 | 0 | final int index = (x * groupCount) + y; |
| 215 | 0 | oDateX[index] = date; |
| 216 | 0 | oDoubleY[index] = new Double(y); |
| 217 | 0 | final double value = map.get(group.getName()) * 100.0 / max; |
| 218 | 0 | oDoubleZ[index] = (value != 0) ? new Double(value) : null; |
| 219 | |
} |
| 220 | |
} else { |
| 221 | 0 | for (int y = 0; y < groupCount; y++) { |
| 222 | 0 | final int index = (x * groupCount) + y; |
| 223 | 0 | oDateX[index] = date; |
| 224 | 0 | oDoubleY[index] = new Double(y); |
| 225 | |
|
| 226 | |
} |
| 227 | |
} |
| 228 | |
} |
| 229 | 0 | return new DefaultContourDataset(null, oDateX, oDoubleY, oDoubleZ); |
| 230 | |
} |
| 231 | |
} |