public async Task PlayerChart(string name) { using (var c = new BarcabotDatabaseConnection()) { var playerObject = c.GetPlayerByName(name); if (playerObject == null) { await Context.Channel.SendMessageAsync( $":warning: Error: Could not find player `{name}`. Are you sure they exist and are a FCB player?\nIf you think there is a player missing from the database please report it to the creator of BarcaBot `Trace#8994`."); } else { var convertedName = NameConverter.ConvertName(playerObject.Name); var stats = playerObject.Per90Stats; var x = new ArrayList { "Shots", "Shots on Target", "Key Passes", "Tackles", "Blocks", "Interceptions", "Duels Won", "Dribbles Attempted", "Dribbles Won", "Fouls Drawn", "Fouls Committed" }; var y = new ArrayList { stats.Shots.Total, stats.Shots.OnTarget, stats.Passes.KeyPasses, stats.Tackles.TotalTackles, stats.Tackles.Blocks, stats.Tackles.Interceptions, stats.Duels.Won, stats.Dribbles.Attempted, stats.Dribbles.Won, stats.Fouls.Drawn, stats.Fouls.Committed }; var chart = new PlotlyChart { Figure = new Figure { Data = new ArrayList { new BarTrace { X = x, Y = y, Name = convertedName } }, Layout = GetLayout($"{convertedName} Per 90 Stats", false) }, Height = 500, Width = 1000 }; var chartAsBytes = await _plotlyClient.GetChartAsByteArray(chart); var stream = new MemoryStream(chartAsBytes); await Context.Channel.SendFileAsync(stream, "chart.png"); } } }
public async Task <byte[]> GetStatsBarChart(IList <Player> players) { var chart = new PlotlyChart { Figure = new Figure { Data = new ArrayList() }, Height = 500, Width = 1000 }; var chartLayout = GetDefaultLayout(); var x = new ArrayList { "Shots", "Shots on Target", "Key Passes", "Tackles", "Blocks", "Interceptions", "Duels Won", "Dribbles Attempted", "Dribbles Won", "Fouls Drawn", "Fouls Committed" }; foreach (var player in players) { var stats = player.Statistics; var converter = new ToPer90Converter(stats.Games.MinutesPlayed); var y = new ArrayList { converter.ToPer90(stats.Shots.Total), converter.ToPer90(stats.Shots.OnTarget), converter.ToPer90(stats.Passes.Key), converter.ToPer90(stats.Tackles.Total), converter.ToPer90(stats.Tackles.Blocks), converter.ToPer90(stats.Tackles.Interceptions), converter.ToPer90(stats.Duels.Won), converter.ToPer90(stats.Dribbles.Attempts), converter.ToPer90(stats.Dribbles.Success), converter.ToPer90(stats.Fouls.Drawn), converter.ToPer90(stats.Fouls.Committed) }; var trace = new BarTrace { X = x, Y = y, Name = player.Name }; chart.Figure.Data.Add(trace); } if (players.Count == 1) { chartLayout.Title.Text = $"{players[0].Name} per 90 statistics"; chartLayout.ShowLegend = false; } else { chartLayout.Title.Text = "Per 90 statistics comparison"; chartLayout.ShowLegend = true; } chart.Figure.Layout = chartLayout; return(await _plotlyClient.GetChartAsByteArray(chart)); }
static async Task Main(string[] args) { /* * Example 1 * * Example of how to generate a chart using the built-in PlotlyChart type * and save it to a PNG file */ // very basic example, hardcoding credentials is HIGHLY not recommended PlotlyCredentials Func() => new PlotlyCredentials { Username = "******", Token = "plotly_token" }; // create a plotly client that will communicate with Plotly API PlotlyClient plotlyClient = new PlotlyClient(Client, Func); // create a chart PlotlyChart myChart = new PlotlyChart { Figure = new Figure { Data = new ArrayList { new BarTrace { X = new ArrayList { 1, 2, 3 }, Y = new ArrayList { 1, 2, 3 }, }, new BarTrace { X = new ArrayList { 1, 2, 3 }, Y = new ArrayList { 1, 2, 3 }, } }, }, Height = 1000, Width = 1000 }; // send the chart to Plotly to compute an image and receive it as a PNG file as byte array byte[] myImg = await plotlyClient.GetChartAsByteArray(myChart); // save it to a file var imgStream = new MemoryStream(myImg); using (var fileStream = File.Create("example1.png")) { imgStream.CopyTo(fileStream); } Thread.Sleep(2000); /* * Example 2 * * Example of how to generate a chart using a JSON object string * and save it to a PNG file */ // must be escaped var escapedJsonObjectString = "{\"figure\": {\"data\": [{\"y\": [10, 10, 2, 20]}], \"layout\": {\"width\": 700}}, \"width\": 1000, \"height\": 500, \"format\": \"png\", \"encoded\": false}"; byte[] myImg2 = await plotlyClient.GetChartAsByteArray(escapedJsonObjectString); // save it to a file MemoryStream imgStream2 = new MemoryStream(myImg2); using (FileStream fileStream = File.Create("example2.png")) { imgStream2.CopyTo(fileStream); } }