Jump to content
We've recently updated our Privacy Statement, available here ×

rreganjr

Members
  • Posts

    2
  • Joined

  • Last visited

rreganjr's Achievements

Newbie

Newbie (1/14)

  • Week One Done
  • One Month Later
  • One Year In
  • First Post Rare

Recent Badges

0

Reputation

  1. I think the problem isn't that day 2 has a value of 0, its day 2 has no entry, I had a similar problem drawing a chart of sparse data. The only solution I found was using a JRChartCustomizer to add the missing periods with a zero value. Code:package utils;import java.util.Date;import org.jfree.chart.JFreeChart;import org.jfree.chart.plot.XYPlot;import org.jfree.data.time.Day;import org.jfree.data.time.Hour;import org.jfree.data.time.Minute;import org.jfree.data.time.Month;import org.jfree.data.time.Second;import org.jfree.data.time.TimeSeries;import org.jfree.data.time.TimeSeriesCollection;import org.jfree.data.time.Week;import org.jfree.data.time.Year;import net.sf.jasperreports.engine.JRAbstractChartCustomizer;import net.sf.jasperreports.engine.JRChart;/** * This is a Jasper Reports chart customizer for time series chart that will fill in * missing time periods in the dataset with 0 value entries. For example if your plotting * the number of hits to a website by hour and there were 10 hits at midnight and 10 hits * at 6 am, but no hits from 1 am to 5 am by default the time series chart will draw a strait * line from the midnight plot point to the 6 am plot point, if you want the line to drop to * zero this class can be set in the chart element customizerClass attribute and Jasper Reports * will call it before generating the chart. * * NOTE: this has dependencies on the jrxml file that a startDateInternal and endDateInternal * property exists that holds the start and end date to be plotted. * * @author ron@nellymoser.com * */public class JRTimeSeriesCustomizerEmptyDateFiller extends JRAbstractChartCustomizer { public void customize(JFreeChart chart, JRChart jasperChart) { Date startDate = (Date) getParameterValue("startDateInternal", true); Date endDate = (Date) getParameterValue("endDateInternal", true); if (chart.getPlot() != null && chart.getPlot() instanceof XYPlot) { XYPlot plot = (XYPlot) chart.getPlot(); for (int i = 0; i < plot.getDatasetCount(); i++) { Object genericDataSet = plot.getDataset(i); if (genericDataSet != null && genericDataSet instanceof TimeSeriesCollection) { TimeSeriesCollection tsc = (TimeSeriesCollection) genericDataSet; for (Object genericTimeSeries : tsc.getSeries()) { if (genericTimeSeries != null && genericTimeSeries instanceof TimeSeries) { TimeSeries timeSeries = (TimeSeries) genericTimeSeries; fillInMissingPeriods(timeSeries, startDate, endDate, timeSeries.getTimePeriodClass()); } } } } } } // TODO: it would be more effecient to move the while inside each if statement so the if is only executed // once instead of each period from start to end. private void fillInMissingPeriods(TimeSeries timeSeries, Date startDate, Date endDate, Class<?> period) { Date dateIndex = startDate; while (dateIndex.before(endDate)) { if (Day.class.equals(period)) { dateIndex = DateUtils.startOfDay(dateIndex); if (timeSeries.getDataItem(new Day(dateIndex)) == null) { timeSeries.add(new Day(dateIndex), 0.0d); } dateIndex = DateUtils.addDays(dateIndex, 1); } else if (Hour.class.equals(period)) { dateIndex = DateUtils.startOfHour(dateIndex); if (timeSeries.getDataItem(new Hour(dateIndex)) == null) { timeSeries.add(new Hour(dateIndex), 0.0d); } dateIndex = DateUtils.addHours(dateIndex, 1); } else if (Minute.class.equals(period)) { dateIndex = DateUtils.startOfMinute(dateIndex); if (timeSeries.getDataItem(new Minute(dateIndex)) == null) { timeSeries.add(new Minute(dateIndex), 0.0d); } dateIndex = DateUtils.addMinutes(dateIndex, 1); } else if (Second.class.equals(period)) { dateIndex = DateUtils.startOfSecond(dateIndex); if (timeSeries.getDataItem(new Second(dateIndex)) == null) { timeSeries.add(new Second(dateIndex), 0.0d); } dateIndex = DateUtils.addSeconds(dateIndex, 1); } else if (Week.class.equals(period)) { dateIndex = DateUtils.startOfWeek(dateIndex); if (timeSeries.getDataItem(new Week(dateIndex)) == null) { timeSeries.add(new Week(dateIndex), 0.0d); } dateIndex = DateUtils.addDays(dateIndex, 7); } else if (Month.class.equals(period)) { dateIndex = DateUtils.startOfMonth(dateIndex); if (timeSeries.getDataItem(new Month(dateIndex)) == null) { timeSeries.add(new Month(dateIndex), 0.0d); } dateIndex = DateUtils.addMonths(dateIndex, 1); } else if (Year.class.equals(period)) { dateIndex = DateUtils.startOfYear(dateIndex); if (timeSeries.getDataItem(new Year(dateIndex)) == null) { timeSeries.add(new Year(dateIndex), 0.0d); } dateIndex = DateUtils.addYears(dateIndex, 1); } else { // un-expected time period throw new Exception("The supplied time period " + period.getName() + " is unsupported, use Second, Minute, Hour, Day, Week, Month, and Year."); } } }}
  2. new java.lang.Integer( new java.lang.Integer(100).intValue() -($V{averageMem}.intValue()) )
×
×
  • Create New...