Exemplo n.º 1
0
        private void CalculateSeriesForVehicle(Vehicle vehicle, StatisticSeries series, DateTime?startDate, DateTime?endDate)
        {
            Debug.Assert(series != null);

            DateTime startFilterDate = startDate ?? DateTime.MinValue;
            DateTime endFilterDate   = endDate ?? DateTime.UtcNow;

            var fillUps = _fillupRepository.GetFillups(vehicle.VehicleId);

            var fillupGroups = from fillUp in fillUps
                               where ((fillUp.Date >= startFilterDate) && (fillUp.Date <= endFilterDate))
                               group fillUp by new { Year = fillUp.Date.Year, Month = fillUp.Date.Month }
            into g
            orderby g.Key.Year, g.Key.Month
            select g;

            var firstFillUp = fillUps.OrderBy(x => x.Date).FirstOrDefault();

            VehicleStatisticsModel statistics;

            foreach (var fillupGroup in fillupGroups)
            {
                var includeFirstFillup = (fillupGroup.Key.Year != firstFillUp.Date.Year) ||
                                         (fillupGroup.Key.Month != firstFillUp.Date.Month);

                statistics = CalculateStatistics.Calculate(fillupGroup, includeFirstFillup);

                Debug.Assert(firstFillUp != null);


                var seriesEntry = new StatisticSeriesEntry
                {
                    Id    = vehicle.VehicleId,
                    Name  = vehicle.Name,
                    Year  = fillupGroup.Key.Year,
                    Month = fillupGroup.Key.Month,
                    AverageFuelEfficiency = Math.Round(statistics.AverageFuelEfficiency, 2),
                    TotalCost             = Math.Round(statistics.TotalCost, 2),
                    TotalDistance         = statistics.TotalDistance,
                };
                series.Entries.Add(seriesEntry);
            }
        }
        public static bool PlotSingleChartLine(Chart chart, IEnumerable <StatisticSeriesEntry> seriesData, Func <StatisticSeriesEntry, double> yValueAccessor)
        {
            if (chart == null)
            {
                throw new ArgumentNullException("chart");
            }

            bool isDataPlotted = false;

            if (seriesData != null && seriesData.Count() > 0)
            {
                var xValues = new List <DateTime>();
                var yValues = new List <double>();

                // I add these as DateTime types as charts need them for proper sorting of the x axis.
                DateTime             date      = DateTime.UtcNow.Date;
                StatisticSeriesEntry lastEntry = null;
                foreach (var entry in seriesData)
                {
                    date = new DateTime(entry.Year, entry.Month, 1);
                    xValues.Add(date);
                    yValues.Add(yValueAccessor(entry));
                    lastEntry = entry;
                }

                // I add a previous data point when there is only a single one to help the graph draw a line.
                if (xValues.Count == 1)
                {
                    xValues.Insert(0, date.AddMonths(-1));
                    yValues.Insert(0, 0.0);
                }

                chart.AddSeries(chartType: "Line",
                                name: lastEntry.Name,
                                xValue: xValues.ToArray(),
                                yValues: yValues.ToArray());

                isDataPlotted = true;
            }

            return(isDataPlotted);
        }