Example #1
0
        public async Task <OperationDataResult <StatByMonth> > GetSingleFractionByMonth(int fractionId, int year)
        {
            try
            {
                var statByMonth = new StatByMonth();
                statByMonth.StatByMonthListData1 = new List <Period>();
                var months = new List <string>
                {
                    "Jan", "Feb", "Mar", "Apr", "Maj", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"
                };
                var outcomes = new List <string>
                {
                    "Godkendt", "Betinget Godkendt", "Ikke Godkendt"
                };
                var trashInspectionsQuery =
                    _dbContext.TrashInspections.AsQueryable();
                var linePeriod = new Period
                {
                    Name = "Compliance"
                };
                linePeriod.Series     = new List <SeriesObject>();
                trashInspectionsQuery = trashInspectionsQuery.Where(x => x.Date.Year == year && x.FractionId == fractionId);
                //double wheigingsPrYear = await trashInspectionsQuery.CountAsync();
                //double wheigingsPrYearControlled = await trashInspectionsQuery.CountAsync(x => x.Status == 100);
                //var avgControlPercentagePrYear = (wheigingsPrYearControlled / wheigingsPrYear) * 100;
                for (int i = 0, j = 0; i < months.Count; i++, j++)
                {
                    var month = months[i];
                    trashInspectionsQuery = trashInspectionsQuery.Where(x => x.Date.Month == i);
                    //double wheigingsPrMonth = await trashInspectionsQuery.CountAsync();
                    double wheigingsPrMonthControlled = await trashInspectionsQuery.CountAsync(x => x.Status == 100);

                    double wheighingsApprovedPrMonth =
                        await trashInspectionsQuery.CountAsync(x => x.IsApproved && x.Status == 100);

                    double wheighingsNotApprovedPrMonth =
                        await trashInspectionsQuery.CountAsync(x => x.ApprovedValue == "3" && x.Status == 100);

                    double wheighingsPartiallyApprovedPrMonth =
                        await trashInspectionsQuery.CountAsync(x => x.ApprovedValue == "2" && x.Status == 100);

                    double approvedWheighingsPercentage         = 0;
                    double notApprovedWheighingPercentage       = 0;
                    double partiallyApprovedWheighingPercentage = 0;
                    if (Math.Abs(wheigingsPrMonthControlled) > 0)
                    {
                        approvedWheighingsPercentage = Math.Round(
                            (wheighingsApprovedPrMonth / wheigingsPrMonthControlled) * 100, 1);
                        notApprovedWheighingPercentage =
                            Math.Round((wheighingsNotApprovedPrMonth / wheigingsPrMonthControlled) * 100, 1);
                        partiallyApprovedWheighingPercentage =
                            Math.Round((wheighingsPartiallyApprovedPrMonth / wheigingsPrMonthControlled) * 100, 1);
                    }

                    var period = new Period {
                        Name = month, Series = new List <SeriesObject>()
                    };
                    //Bar Chart Data
                    var seriesObject1 = new SeriesObject
                    {
                        Name  = outcomes[0],
                        Value = approvedWheighingsPercentage
                    };
                    period.Series.Add(seriesObject1);
                    var seriesObject2 = new SeriesObject
                    {
                        Name  = outcomes[1],
                        Value = partiallyApprovedWheighingPercentage
                    };
                    period.Series.Add(seriesObject2);
                    var seriesObject3 = new SeriesObject
                    {
                        Name  = outcomes[2],
                        Value = notApprovedWheighingPercentage
                    };
                    period.Series.Add(seriesObject3);
                    statByMonth.StatByMonthListData1.Add(period);

                    //Line Chart Data
                    var lineSeriesObject1 = new SeriesObject
                    {
                        Name  = months[j],
                        Value = approvedWheighingsPercentage
                    };
                    linePeriod.Series.Add(lineSeriesObject1);
                }

                statByMonth.StatByMonthListData2.Add(linePeriod);

                return(new OperationDataResult <StatByMonth>(true,
                                                             statByMonth));
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                _coreHelper.LogException(e.Message);
                return(new OperationDataResult <StatByMonth>(false,
                                                             _trashInspectionLocalizationService.GetString("ErrorObtainingStatsByMonth")));
            }
        }
 public ChartDataReponse()
 {
     Series = new SeriesObject();
 }