Esempio n. 1
0
        public void RunMultiyearTest()
        {
            PrepareUnitTest();
            string fn = WorkingDirectory.Combine("trend.xlsx");

            Config.LimitToScenarios.Clear();
            Config.InitializeSlices(Logger);
            MultiyearMultiVariableTrend trend = new MultiyearMultiVariableTrend();
            Random rnd = new Random();

            if (Config.Slices == null)
            {
                throw new FlaException("No slices");
            }
            trend[Constants.PresentSlice].AddValue("MyValue", "mycategory", 50, DisplayUnit.Stk);
            trend[Constants.PresentSlice].AddValue("MyValue", "mycategory2", 50, DisplayUnit.Stk);
            foreach (var slice in Config.Slices)
            {
                Info("Slice " + slice);
                trend[slice].AddValue("MyValue", "mycategory", rnd.Next(100), DisplayUnit.Stk);
                trend[slice].AddValue("MyValue", "mycategory2", rnd.Next(100), DisplayUnit.Stk);
            }

            XlsxDumper.DumpMultiyearMultiVariableTrendToExcel(fn, trend);
            Info("Wrote to " + fn);
            Process.Start(fn);
        }
        private static List <RowCollection> MakeRowCollectionForFromMultiTrendVariable([NotNull] MultiyearMultiVariableTrend trend,
                                                                                       [NotNull] string variable,
                                                                                       out int categoryCount,
                                                                                       out int yearCount)
        {
            var scenarios  = trend.Dict.Keys.Select(x => x.DstScenario).Distinct().ToList();
            var categories = trend.Dict.Values.SelectMany(x => x.Values).Where(x => x.VariableName == variable).SelectMany(y => y.Values.Keys)
                             .Distinct().ToList();

            categoryCount = categories.Count;
            var years = trend.Dict.Keys.Select(x => x.DstYear).Distinct().OrderBy(x => x).ToList();

            yearCount = years.Count;
            List <RowCollection> rows = new List <RowCollection>();
            var presentSliceValues    = trend.Dict[Constants.PresentSlice];

            foreach (var scenario in scenarios)
            {
                if (scenario == Scenario.Present())
                {
                    continue;
                }

                RowCollection rc = new RowCollection(scenario.ShortName, scenario.Name);
                rows.Add(rc);
                foreach (var year in years)
                {
                    var rb = RowBuilder.Start("Year", year);
                    if (year == 2017)
                    {
                        foreach (var category in categories)
                        {
                            object o = null;
                            if (presentSliceValues != null)
                            {
                                o = presentSliceValues.GetSliceValueByName(variable, category);
                            }

                            rb.Add(category, o);
                        }
                    }
                    else
                    {
                        foreach (var category in categories)
                        {
                            var    slicevalues = trend.Dict.Values.FirstOrDefault(x => x.Slice.DstYear == year && x.Slice.DstScenario == scenario);
                            object o           = null;
                            if (slicevalues != null)
                            {
                                o = slicevalues.GetSliceValueByName(variable, category);
                            }

                            rb.Add(category, o);
                        }
                    }

                    rc.Add(rb);
                }
            }

            return(rows);
        }
        public static void DumpMultiyearMultiVariableTrendToExcel([NotNull] string fullFileName, [NotNull] MultiyearMultiVariableTrend trend)
        {
            if (trend.Dict.Count == 0)
            {
                throw new FlaException("No values");
            }

            if (!trend.Dict.ContainsKey(Constants.PresentSlice))
            {
                throw new FlaException("Missing present");
            }

            var allVariables = trend.Dict.Values.SelectMany(x => x.Values.Select(y => y.VariableName)).Distinct().ToList();

            using (var p = PrepareExcelPackage(fullFileName)) {
                foreach (var variable in allVariables)
                {
                    var rcs = MakeRowCollectionForFromMultiTrendVariable(trend, variable, out var categoryCount, out var yearCount);
                    var ws  = p.Workbook.Worksheets.Add(variable);
                    int idx = 2;
                    foreach (var rc in rcs)
                    {
                        ws.Cells[idx - 1, 1].Value = rc.SheetName;
                        FillExcelSheet(rc, ws, false, idx);
                        MakeSingleChartForTrends(ws, idx, categoryCount + 4, categoryCount, idx, idx + yearCount, rc.YAxisName, variable);

                        idx = idx + rc.Rows.Count + 10;
                    }
                }

                p.Save();
            }
        }