KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > chart > TimeChartData


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.chart;
38
39 import java.text.NumberFormat JavaDoc;
40 import java.text.SimpleDateFormat JavaDoc;
41 import java.util.Calendar JavaDoc;
42 import java.util.Date JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.Map JavaDoc;
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 JavaDoc produceDataset(Map JavaDoc 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 JavaDoc iter = summary.iterator(); iter.hasNext();) {
72             BuildInfo buildInfo = (BuildInfo) iter.next();
73             Date JavaDoc 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     /**
91      * @param buildTime
92      * @return Only the time part of the Date
93      */

94     private double extractTimeOfDay(Date JavaDoc buildTime) {
95         Calendar JavaDoc 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 JavaDoc getProducerId() {
105         return "TimeChartData DatasetProducer";
106     }
107     
108
109     /**
110      * @see ChartPostProcessor#processChart(Object, Map)
111      */

112     public void processChart(Object JavaDoc chartObject, Map JavaDoc params) {
113         JFreeChart chart = (JFreeChart) chartObject;
114         XYPlot plot = chart.getXYPlot();
115         configurePlotRendererForShapesOnly(plot);
116         setXAxisFormat(plot);
117         setYAxisFormat(plot);
118     }
119
120     /**
121      * @param plot
122      */

123     private void setYAxisFormat(XYPlot plot) {
124         NumberAxis yAxis = (NumberAxis) plot.getVerticalAxis();
125         NumberFormat JavaDoc 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     /**
133      * @param plot
134      */

135     private void setXAxisFormat(XYPlot plot) {
136         DateAxis xAxis = (DateAxis) plot.getHorizontalValueAxis();
137         xAxis.setDateFormatOverride(new SimpleDateFormat JavaDoc("dd/MM"));
138     }
139
140     /**
141      * @param plot
142      */

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