Пример #1
0
        private static Chart GenerateLineScatterChart()
        {
            Chart chart = new Chart();

            chart.Type = Enums.ChartType.Line;

            Data data = new Data();

            data.Labels = new List <string>()
            {
                "January", "February", "March", "April", "May", "June", "July"
            };



            data.Datasets = new List <Dataset>();


            Options options = new Options()
            {
                Scales = new Scales()
            };

            Scales scales = new Scales()
            {
                YAxes = new List <Scale>()
                {
                    new CartesianScale()
                }
            };

            CartesianScale yAxes = new CartesianScale()
            {
                Ticks = new Tick()
            };

            Tick tick = new Tick()
            {
                Callback = "function(value, index, values) {return '$' + value;}"
            };

            yAxes.Ticks  = tick;
            scales.YAxes = new List <Scale>()
            {
                yAxes
            };
            options.Scales = scales;
            chart.Options  = options;

            chart.Data = data;

            return(chart);
        }
Пример #2
0
        public ContentResult OnGetChartData(string hashtags)
        {
            List <string> colors = new List <string>()
            {
                "#FF6384", "#4BC0C0", "#FFCE56", "#E7E9ED", "#36A2EB"
            };

            List <TweetsData> tweets = GetTweets(hashtags);

            var   dates = tweets.GroupBy(d => d.Date).Select(k => k.Key).ToList();
            Chart chart = new Chart
            {
                Type = Enums.ChartType.PolarArea
            };

            Data data = new Data();

            data.Labels = tweets.GroupBy(k => k.Keyword).Select(k => k.Key).ToList();
            PolarDataset dataset = new PolarDataset()
            {
                Label           = "Keywords dataset",
                BackgroundColor = new List <ChartColor>(),
                Data            = new List <double?>()
            };
            Random rnd = new Random();
            int    i   = 0;

            foreach (string label in data.Labels)
            {
                dataset.Data.Add(tweets.Where(l => l.Keyword == label).GroupBy(k => k.Count).Select(c => c.Key).Sum());
                //ChartColor.FromHexString(string.Format("#{0:X6}", rnd.Next(0x1000000))) < random colors
                dataset.BackgroundColor.Add(ChartColor.FromHexString(colors[i]));
                i++;
            }
            data.Datasets = new List <Dataset>
            {
                dataset
            };
            chart.Data = data;
            return(Content(chart.CreateChartCode("lineChart")));
        }
Пример #3
0
        public async Task <JsonResult> OnPostAsync(string UserInput)
        {
            if (!string.IsNullOrEmpty(UserInput))
            {
                List <BinResult> binResults = new List <BinResult>();
                string[]         keywords   = UserInput.Split(" ").Take(5).ToArray();
                List <string>    jReturns   = new List <string>();
                foreach (string keyword in keywords)
                {
                    //    data.Labels.Add(keyword);
                    //todo add link to pastbin raw data based on binID
                    try
                    {
                        HttpAsync httpAsync = new HttpAsync();
                        string    response  = await httpAsync.Get(PsDumpUrl.Replace("#keyword#", keyword));

                        if (!string.IsNullOrEmpty(response))
                        {
                            try
                            {
                                binResults.Add(JsonConvert.DeserializeObject <BinResult>(response));
                                jReturns.Add(response);
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
                //Create chart
                Chart chart = new Chart();
                chart.Type = Enums.ChartType.Line;
                LineDataset lineDataset = new LineDataset();
                Data        data        = new Data();
                data.Labels   = new List <string>();
                data.Datasets = new List <Dataset>();
                foreach (BinResult binResult in binResults)
                {
                    LineDataset    dataset = new LineDataset();
                    List <double?> c       = new List <double?>();
                    foreach (var line in binResult.data.GroupBy(f => DateTime.Parse(f.Time).ToUniversalTime()
                                                                .ToString("Y", System.Globalization.CultureInfo.CreateSpecificCulture("en-US")))
                             .Select(group => new
                    {
                        xAxis = group.Key,
                        yAxis = group.Count()
                    })
                             .OrderBy(x => x.xAxis))
                    {
                        Debug.WriteLine(line.xAxis);
                        if (!data.Labels.Contains(line.xAxis))
                        {
                            data.Labels.Add(line.xAxis);
                        }

                        if (c.Count() < data.Labels.IndexOf(line.xAxis))
                        {
                            for (int i = c.Count(); i < data.Labels.IndexOf(line.xAxis); i++)
                            {
                                c.Add(0);
                            }
                        }
                        c.Insert(data.Labels.IndexOf(line.xAxis), (line.yAxis));
                        Debug.WriteLine("{0} {1} {2}", binResult.Search, line.xAxis, line.yAxis);
                    }
                    lineDataset = CreateChartDataSet(binResult.Search, c);
                    data.Datasets.Add(lineDataset);
                }
                Options options = new Options()
                {
                    Scales              = new Scales(),
                    Responsive          = true,
                    MaintainAspectRatio = true
                };

                Scales scales = new Scales()
                {
                    YAxes = new List <Scale>()
                    {
                        new CartesianScale()
                    },
                    XAxes = new List <Scale>()
                    {
                        new CartesianScale()
                        {
                            Type     = "category",
                            Position = "bottom",
                            Ticks    = new CartesianLinearTick()
                            {
                                BeginAtZero = false
                            }
                        }
                    }
                };

                CartesianScale yAxes = new CartesianScale()
                {
                    Ticks = new Tick()
                };

                Tick tick = new Tick()
                {
                    Callback = "function(value, index, values) {return '#of bins' + value;}"
                };

                yAxes.Ticks  = tick;
                scales.YAxes = new List <Scale>()
                {
                    yAxes
                };
                options.Scales = scales;
                chart.Options  = options;
                chart.Data     = data;
                return(new JsonResult(new { table = jReturns, chart = chart.CreateChartCode("lineChart") }));
            }
            return(null);
        }
Пример #4
0
        public async Task <ChartJSCore.Models.Chart> GetGraphData()
        {
            Random random = new Random();

            ChartJSCore.Models.Chart chart = new ChartJSCore.Models.Chart();

            chart.Type = "line";
            ChartJSCore.Models.Data data = new ChartJSCore.Models.Data();
            data.Labels = (from contextShereRatese in _context.ShereRateses
                           select contextShereRatese.DateTimeRates.ToLongTimeString()).Distinct().ToList();
            data.Datasets = new List <Dataset>();
            foreach (var companyCode in _context.Companies.Select(w => w.Code))
            {
                string coloRround = "rbga(" + random.Next(0, 255) + "," + random.Next(0, 255) + "," + random.Next(0, 255);

                List <decimal> list = new List <decimal>();
                foreach (decimal @decimal in (_context.ShereRateses.Where(w => w.Code == companyCode).Select(
                                                  contextShereRatese => contextShereRatese.Rate)))
                {
                    list.Add(@decimal);
                }
                LineDataset dataset = new LineDataset()
                {
                    Label            = companyCode,
                    Data             = list as IList <double>,
                    Fill             = "false",
                    LineTension      = 0.1,
                    BackgroundColor  = coloRround + ",0.4)",
                    BorderColor      = coloRround + ",1)",
                    BorderCapStyle   = "butt",
                    BorderDash       = new List <int> {
                    },
                    BorderDashOffset = 0.0,
                    BorderJoinStyle  = "miter",
                    PointBorderColor = new List <string>()
                    {
                        coloRround + ",1)"
                    },
                    PointBackgroundColor = new List <string>()
                    {
                        "#fff"
                    },
                    PointBorderWidth = new List <int> {
                        1
                    },
                    PointHoverRadius = new List <int> {
                        5
                    },
                    PointHoverBackgroundColor = new List <string>()
                    {
                        coloRround + ",1)"
                    },
                    PointHoverBorderColor = new List <string>()
                    {
                        "rgba(220,220,220,1)"
                    },
                    PointHoverBorderWidth = new List <int> {
                        2
                    },
                    PointRadius = new List <int> {
                        1
                    },
                    PointHitRadius = new List <int> {
                        10
                    },
                    SpanGaps = false
                };
                data.Datasets.Add(dataset);
            }
            chart.Data = data;
            return(chart);
        }
Пример #5
0
        private List <ChartJSCore.Models.Chart> CreateCharts(Device device)
        {
            int binNum = 6;

            IEnumerable <Code> codes = _repository.GetAll(device);
            var freq = codes.GroupBy(x => x.Action).ToDictionary(x => x.Key, x => x.Count());
            IEnumerable <double> compl = codes.Where(x => x.DateArrived != DateTime.MinValue && x.DateCompleted != DateTime.MinValue &&
                                                     x.DateExecuted != DateTime.MinValue).Select(x => (x.DateCompleted - x.DateArrived).TotalSeconds);

            ChartJSCore.Models.Chart frequency      = new ChartJSCore.Models.Chart();
            ChartJSCore.Models.Chart timeToComplete = new ChartJSCore.Models.Chart();

            frequency.Type      = "bar";
            timeToComplete.Type = "bar";


            ChartJSCore.Models.Data freqData = new ChartJSCore.Models.Data();
            freqData.Labels = freq.Keys.Select(x => x.ToString()).ToList();

            double lowerCompl;
            double upperCompl;

            if (compl.Any())
            {
                lowerCompl = compl.Min();
                upperCompl = compl.Max();
            }
            else
            {
                lowerCompl = 0;
                upperCompl = 0;
            }

            double complRange = (upperCompl - lowerCompl) / binNum;

            List <string> complBins = new List <string>();

            for (int i = 0; i < binNum; i++)
            {
                complBins.Add($"{lowerCompl + i * complRange:f1}-{lowerCompl + (i + 1) * complRange:f1}");
            }

            int[] complPoints = new int[binNum];
            foreach (var value in compl)
            {
                int bucketIndex = 0;
                if (complRange > 0.0)
                {
                    bucketIndex = (int)((value - lowerCompl) / complRange);
                    if (bucketIndex == binNum)
                    {
                        bucketIndex--;
                    }
                }
                complPoints[bucketIndex]++;
            }


            ChartJSCore.Models.Data completeData = new ChartJSCore.Models.Data();
            completeData.Labels = complBins;

            BarDataset freqDataset = new BarDataset()
            {
                Label           = "Frequency of Actions",
                Data            = freq.Values.Select(x => (double)x).ToList(),
                BackgroundColor = new List <string>(),
                BorderWidth     = new List <int>()
                {
                    1
                }
            };

            freqData.Datasets = new List <Dataset>();
            freqData.Datasets.Add(freqDataset);

            BarDataset complDateDataset = new BarDataset()
            {
                Label           = "Complete time in seconds",
                Data            = complPoints.Select(x => (double)x).ToList(),
                BackgroundColor = new List <string>(),
                BorderWidth     = new List <int>()
                {
                    1
                }
            };

            completeData.Datasets = new List <Dataset>();
            completeData.Datasets.Add(complDateDataset);

            frequency.Data      = freqData;
            timeToComplete.Data = completeData;

            BarOptions options = new BarOptions()
            {
                Scales        = new Scales(),
                BarPercentage = 0.7
            };

            Scales scales = new Scales()
            {
                YAxes = new List <Scale>()
                {
                    new CartesianScale()
                    {
                        Ticks = new CartesianLinearTick()
                        {
                            BeginAtZero = true
                        }
                    }
                }
            };

            options.Scales = scales;

            frequency.Options      = options;
            timeToComplete.Options = options;

            return(new List <ChartJSCore.Models.Chart>
            {
                frequency,
                timeToComplete
            });
        }