1 37 package net.sourceforge.cruisecontrol.chart; 38 39 import java.text.NumberFormat ; 40 import java.text.SimpleDateFormat ; 41 import java.util.Calendar ; 42 import java.util.Date ; 43 import java.util.Iterator ; 44 import java.util.Map ; 45 46 import net.sourceforge.cruisecontrol.BuildInfo; 47 import net.sourceforge.cruisecontrol.BuildInfoSummary; 48 import net.sourceforge.cruisecontrol.util.TimeNumberFormat; 49 50 import org.jfree.chart.JFreeChart; 51 import org.jfree.chart.axis.DateAxis; 52 import org.jfree.chart.axis.NumberAxis; 53 import org.jfree.chart.axis.NumberTickUnit; 54 import org.jfree.chart.plot.XYPlot; 55 import org.jfree.chart.renderer.StandardXYItemRenderer; 56 import org.jfree.data.time.Minute; 57 import org.jfree.data.time.TimeSeries; 58 import org.jfree.data.time.TimeSeriesCollection; 59 60 import de.laures.cewolf.ChartPostProcessor; 61 import de.laures.cewolf.DatasetProduceException; 62 63 public class TimeChartData extends AbstractCruiseControlChartData implements ChartPostProcessor { 64 65 private static final long serialVersionUID = -5159867264828131088L; 66 67 public Object produceDataset(Map params) throws DatasetProduceException { 68 BuildInfoSummary summary = (BuildInfoSummary) params.get("buildInfo"); 69 TimeSeries brokenSeries = new TimeSeries("Broken Builds", Minute.class); 70 TimeSeries goodSeries = new TimeSeries("Good Builds", Minute.class); 71 for (Iterator iter = summary.iterator(); iter.hasNext();) { 72 BuildInfo buildInfo = (BuildInfo) iter.next(); 73 Date buildTime = buildInfo.getBuildDate(); 74 double timeValue = extractTimeOfDay(buildTime); 75 Minute timePeriod = new Minute(buildTime); 76 TimeSeries seriesToAddTo = buildInfo.isSuccessful() ? goodSeries 77 : brokenSeries; 78 if (seriesToAddTo.getDataPair(timePeriod) == null) { 79 seriesToAddTo.add(timePeriod, timeValue); 80 } else { 81 System.err.println("multiple logs in the same minute; ignoring: " + buildInfo.getLogName()); 82 } 83 } 84 TimeSeriesCollection dataset = new TimeSeriesCollection(); 85 dataset.addSeries(brokenSeries); 86 dataset.addSeries(goodSeries); 87 return dataset; 88 } 89 90 94 private double extractTimeOfDay(Date buildTime) { 95 Calendar buildCalendar = Calendar.getInstance(); 96 buildCalendar.setTime(buildTime); 97 double timeValue = buildCalendar.get(Calendar.HOUR_OF_DAY); 98 timeValue = timeValue * 60 + buildCalendar.get(Calendar.MINUTE); 99 timeValue = timeValue * 60 + buildCalendar.get(Calendar.SECOND); 100 timeValue = timeValue * 1000 + buildCalendar.get(Calendar.MILLISECOND); 101 return timeValue; 102 } 103 104 public String getProducerId() { 105 return "TimeChartData DatasetProducer"; 106 } 107 108 109 112 public void processChart(Object chartObject, Map params) { 113 JFreeChart chart = (JFreeChart) chartObject; 114 XYPlot plot = chart.getXYPlot(); 115 configurePlotRendererForShapesOnly(plot); 116 setXAxisFormat(plot); 117 setYAxisFormat(plot); 118 } 119 120 123 private void setYAxisFormat(XYPlot plot) { 124 NumberAxis yAxis = (NumberAxis) plot.getVerticalAxis(); 125 NumberFormat yAxisFormat = new TimeNumberFormat(); 126 yAxis.setNumberFormatOverride(yAxisFormat); 127 yAxis.setMinimumAxisValue(0); 128 yAxis.setMaximumAxisValue(24 * 60 * 60 * 1000); 129 yAxis.setTickUnit(new NumberTickUnit(2 * 60 * 60 * 1000)); 130 } 131 132 135 private void setXAxisFormat(XYPlot plot) { 136 DateAxis xAxis = (DateAxis) plot.getHorizontalValueAxis(); 137 xAxis.setDateFormatOverride(new SimpleDateFormat ("dd/MM")); 138 } 139 140 143 private void configurePlotRendererForShapesOnly(XYPlot plot) { 144 StandardXYItemRenderer renderer = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES); 145 renderer.setDefaultShapeFilled(true); 146 plot.setRenderer(renderer); 147 } 148 } | Popular Tags |