From d7710c45a47b83cea963099ab45a946d0dabb85a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20Gonz=C3=A1lez=20Fern=C3=A1ndez?= Date: Tue, 26 Jan 2010 00:32:35 +0100 Subject: [PATCH] ItEr45S10CUAsignacionRecursosEspecificosAPlanificacionItEr44S16: Adding chart for interpolation --- .../streches/GraphicForStreches.java | 72 ++++++++++++++++++- 1 file changed, 69 insertions(+), 3 deletions(-) diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/streches/GraphicForStreches.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/streches/GraphicForStreches.java index b11ce0eed..1bc5543d1 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/streches/GraphicForStreches.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/streches/GraphicForStreches.java @@ -27,6 +27,8 @@ import org.joda.time.Days; import org.joda.time.LocalDate; import org.navalplanner.business.calendars.entities.BaseCalendar; import org.navalplanner.business.planner.entities.Stretch; +import org.navalplanner.business.planner.entities.StretchesFunction; +import org.navalplanner.business.planner.entities.StretchesFunction.Interval; import org.navalplanner.business.planner.entities.StretchesFunction.Type; import org.navalplanner.web.planner.allocation.streches.StretchesFunctionController.IGraphicGenerator; import org.zkoss.zul.SimpleXYModel; @@ -158,20 +160,84 @@ public abstract class GraphicForStreches implements IGraphicGenerator { @Override public boolean areChartsEnabled(IStretchesFunctionModel model) { - return false; + return canComputeChartFrom(model.getStretches(), model + .getTaskStartDate()); } @Override protected XYModel getAccumulatedHoursChartData(List stretches, LocalDate startDate, BigDecimal taskHours) { - return new SimpleXYModel(); + if (!canComputeChartFrom(stretches, startDate)) { + return new SimpleXYModel(); + } + int[] hoursForEachDayUsingSplines = hoursForEachDayInterpolatedUsingSplines( + stretches, startDate, taskHours); + return createModelFrom(startDate, + accumulatedFrom(hoursForEachDayUsingSplines)); } @Override protected XYModel getDedicationChart(List stretches, LocalDate startDate, BigDecimal totalHours, BaseCalendar taskCalendar) { - return new SimpleXYModel(); + if (!canComputeChartFrom(stretches, startDate)) { + return new SimpleXYModel(); + } + int[] dataForChart = hoursForEachDayInterpolatedUsingSplines( + stretches, startDate, totalHours); + return createModelFrom(startDate, dataForChart); + } + + private boolean canComputeChartFrom(List stretches, + LocalDate start) { + return stretches.size() >= 2 + && theFirstIntervalIsPosteriorToFirstDay(stretches, start); + } + + private boolean theFirstIntervalIsPosteriorToFirstDay( + List stretches, LocalDate start) { + List intervals = StretchesFunction + .intervalsFor(stretches); + Interval first = intervals.get(0); + return first.getEnd().compareTo(start) > 0; + } + + private int[] hoursForEachDayInterpolatedUsingSplines( + List stretches, LocalDate startDate, + BigDecimal taskHours) { + List intervals = StretchesFunction + .intervalsFor(stretches); + double[] dayPoints = Interval.getDayPointsFor(startDate, intervals); + double[] hourPoints = Interval.getHoursPointsFor(taskHours + .intValue(), intervals); + final Stretch lastStretch = stretches.get(stretches.size() - 1); + return StretchesFunction.Type.hoursForEachDayUsingSplines( + dayPoints, hourPoints, startDate, lastStretch.getDate()); + } + + private int[] accumulatedFrom(int[] hoursForEachDayUsingSplines) { + int[] result = new int[hoursForEachDayUsingSplines.length]; + int accumulated = 0; + for (int i = 0; i < hoursForEachDayUsingSplines.length; i++) { + accumulated += hoursForEachDayUsingSplines[i]; + result[i] = accumulated; + } + return result; + } + + private XYModel createModelFrom(LocalDate startDate, + int[] hoursForEachDay) { + SimpleXYModel result = new SimpleXYModel(); + for (int i = 0; i < hoursForEachDay.length; i++) { + result.addValue("series", + asMilliseconds(startDate.plusDays(i)), + hoursForEachDay[i]); + } + return result; + } + + private long asMilliseconds(LocalDate day) { + return day.toDateTimeAtStartOfDay().getMillis(); } }