コード例 #1
0
ファイル: Twitter.cshtml.cs プロジェクト: KodjoB/Valkyrie
        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")));
        }
コード例 #2
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);
        }