Exemple #1
0
        public ActionResult GenerateGraph(ChartsModel model)
        {
            model = model ?? new ChartsModel();

            var to       = model.To.Date;
            var from     = model.From.Date;
            var grouping = model.Grouping;

            var providers = GetPossibleProviders().Where(x => model.Graphs.Contains(x.Name));

            var series = new List <GraphSeries>();

            foreach (var prov in providers)
            {
                var data = MemCache.GetCached($"chart_{prov.Title}_{from}_{to}", () => GraphPoint.FillHoles(prov.GetDailyValues(from, to), from, to).ToList(), 3600 * 24);


                if (grouping > 1)
                {
                    data =
                        data.GroupBy(x => (int)x.Day.Subtract(from).TotalDays / grouping)
                        .Select(x => new GraphPoint()
                    {
                        Day = x.First().Day, Value = x.Average(y => y.Value)
                    })
                        .ToList();
                }

                series.Add(new GraphSeries()
                {
                    Title = prov.Title, Data = data
                });
            }


            if (model.Normalize)
            {
                foreach (var s in series)
                {
                    var min = s.Data.Min(x => x.Value);
                    var max = s.Data.Max(x => x.Value);

                    foreach (var d in s.Data)
                    {
                        d.Value = 100.0 * (d.Value - min) / (max - min);
                    }
                }
            }

            // TODO: convert this to System.Web.UI.DataVisualization.Charting  (which this thing is internally using)

            var chart = new Chart(1500, 700, ChartTheme.Blue);

            chart.AddTitle(string.Join(", ", providers.Select(x => x.Name)));
            chart.AddLegend("Daily values", "l");
            var graphType = "Line";

            foreach (var s in series)
            {
                chart.AddSeries(s.Title,
                                graphType,
                                xValue: s.Data.Select(x => x.Day.Date.ToString("d")).ToList(),
                                yValues: s.Data.Select(y => y.Value).ToList(),
                                legend: "l");
            }

            return(File(chart.GetBytes("png"), "image/png"));
        }
Exemple #2
0
        public ActionResult Index(ChartsModel model)
        {
            model = model ?? new ChartsModel();
            model.PossibleGraphs = GetPossibleProviders().Select(x => new ChartsModel.PossibleGraph()
            {
                Title = x.Title, Name = x.Name
            }).ToList();


            var to       = model.To.Date;
            var from     = model.From.Date;
            var grouping = model.Grouping;

            var providers = GetPossibleProviders().Where(x => model.Graphs.Contains(x.Name));

            var series = new List <GraphSeries>();

            foreach (var prov in providers)
            {
                var data = MemCache.GetCached($"chart_{prov.Title}_{from}_{to}", () => GraphPoint.FillHoles(prov.GetDailyValues(from, to), from, to).ToList(), 3600 * 24);


                if (grouping > 1)
                {
                    data =
                        data.GroupBy(x => (int)x.Day.Subtract(from).TotalDays / grouping)
                        .Select(x => new GraphPoint()
                    {
                        Day = x.First().Day, Value = x.Average(y => y.Value)
                    })
                        .ToList();
                }

                if (data.Count > 0)
                {
                    data.RemoveAt(data.Count - 1);
                }
                series.Add(new GraphSeries()
                {
                    Title = prov.Title, Data = data
                });
            }


            if (model.Normalize)
            {
                foreach (var s in series)
                {
                    var min = s.Data.Min(x => x.Value);
                    var max = s.Data.Max(x => x.Value);

                    foreach (var d in s.Data)
                    {
                        d.Value = 100.0 * (d.Value - min) / (max - min);
                    }
                }
            }

            model.GraphingData = series;
            return(View("ChartsIndex", model));
        }