Ejemplo n.º 1
0
 public Data(SeriesData[] data)
 {
     SeriesData = data;
 }
        public JsonResult TransactionsHighcharts(Guid dashboardId, Guid panelId)
        {
            var series = new List<Series>();
            var seriesDrilldown = new List<Series>();

            var panel = this.GetPanel(dashboardId, panelId);
            var transactions = transactionServiceOnlyTransaction.GetTransactionsFiltrated(panel.Filter);
            var hasOnlyOutputs = true;

            var type = panel.GetDataType();
            switch (type)
            {
                case Panel.PanelDataType.NonGroup:
                case Panel.PanelDataType.NonGroupAndSortDate:
                {
                    var group = new TransactionGroupDefinition
                    {
                        OrderBy = null,
                        OrderByClassification = null,
                        OrderByExpression = null,
                        OrderByName = null,
                        GroupBy = null,
                        //GroupByExpression = panel.GetGroupByExpression(panel.GroupBy, out groupByName),
                        GroupByExpression = f => f.Date.Date,
                        GroupByName = null,
                        OrderByGroup = null,
                        OrderByGroupClassification = null,
                        OrderByGroupExpression = null,
                        OrderByGroupName = null,
                    };

                    transactions = transactions.OrderBy(f => f.Date).ToList();
                    var transactionGroup = transactionServiceOnlyTransaction.GetTransactionGroupRoot(transactions, group);

                    //var transactionsDayGrouped = transactions.OrderBy(f => f.Date).GroupBy(f=>f.Date.Date).ToList();
                    //var total = transactions.Sum(f => f.Value);
                    //var count = transactions.Count();

                    var serieData = new SeriesData[transactionGroup.SubGroups.Count];
                    var serie = new Series
                    {
                        Name = " ",
                        Data = new DotNet.Highcharts.Helpers.Data(serieData),
                    };

                    series.Add(serie);

                    var index = 0;
                    foreach (var transactionDayGroup in transactionGroup.SubGroups)
                    {
                        var y = 0d;
                        switch(panel.DisplayY)
                        {
                            case TransactionDisplayY.Value:
                                y = (double)transactionDayGroup.Total;
                                break;
                            case TransactionDisplayY.ValuePercentage:
                                y = (double)transactionDayGroup.TotalPercentage.Last();
                                break;
                            case TransactionDisplayY.Count:
                                y = transactionDayGroup.Count;
                                break;
                            case TransactionDisplayY.CountPercentage:
                                y = (double)transactionDayGroup.CountPercentage.Last();
                                break;
                        }

                        var day = (DateTime)transactionDayGroup.Key;
                        serieData[index] = new SeriesData();
                        serieData[index].X = Helper.UnixTicks(day);
                        serieData[index].Y = (Number)y;
                        serieData[index].Name = day.ToString("d");

                        if (y > 0)
                            hasOnlyOutputs = false;

                        index++;
                    }
                    break;
                }
                case SpentBook.Domain.Panel.PanelDataType.OneGroup:
                case SpentBook.Domain.Panel.PanelDataType.TwoGroup:
                case SpentBook.Domain.Panel.PanelDataType.ThreeOrMoreGroup:
                {
                    var transactionGroup = transactionService.GetTransactionGroupRoot(transactions, panel.GetGroupDefinitions().ToArray());
                    var tryCategorize = true;

                    // Como só existe 1 nível, fica impossível criar categorização
                    if (type == Panel.PanelDataType.OneGroup)
                        tryCategorize = false;

                    var chartDataCategorizeds = transactionService.GetChartDataCategorized(transactionGroup, tryCategorize);

                    foreach (var item in chartDataCategorizeds)
                    {
                        var serieData = new SeriesData[item.Items.Length];
                        var serie = new Series
                        {
                            Id = item.ParentPath,
                            Name = string.IsNullOrWhiteSpace(item.ItemGroupName) ? "Todos" : item.ItemGroupName,
                            Data = new DotNet.Highcharts.Helpers.Data(serieData),
                        };

                        if (item.ParentPath == null)
                        {
                            series.Add(serie);
                        }
                        else
                        {
                            seriesDrilldown.Add(serie);
                            serie.PlotOptionsBar = new PlotOptionsBar { ColorByPoint = false };
                        }

                        var index = 0;
                        foreach (var data in item.Items)
                        {
                            if (data == null)
                            {
                                serieData[index++] = null;
                            }
                            else
                            {
                                var y = 0d;
                                switch (panel.DisplayY)
                                {
                                    case TransactionDisplayY.Value:
                                        y = (double)data.Total;
                                        break;
                                    case TransactionDisplayY.ValuePercentage:
                                        if (tryCategorize && item.ParentPath == null)
                                            y = (double)data.TotalPercentageGrandParentRelation;
                                        else
                                            y = (double)data.TotalPercentage;
                                        break;
                                    case TransactionDisplayY.Count:
                                        y = (double)data.Count;
                                        break;
                                    case TransactionDisplayY.CountPercentage:
                                        if (tryCategorize && item.ParentPath == null)
                                            y = (double)data.CountPercentageGrandParentRelation;
                                        else
                                            y = (double)data.CountPercentage;
                                        break;
                                }

                                serieData[index++] = new SeriesData
                                {
                                    Y = (Number)y,
                                    Name = data.Category,
                                    Id = data.Category,
                                    Drilldown = data.ItemPath
                                };

                                if (data.Category == "Despesa")
                                    serieData[index - 1].Color = System.Drawing.Color.Red;

                                if (data.Total > 0)
                                    hasOnlyOutputs = false;
                            }
                        }
                    }

                    break;
                }
            }

            return Json(new { Series = series, Drilldown = seriesDrilldown, Reversed = hasOnlyOutputs, DisplatY = panel.DisplayY }, JsonRequestBehavior.AllowGet);
        }