Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions ChessStats/ChessStats/Helpers/StatsGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ internal class StatsGraph
public double GraphWidth { get; private set; }
public float TextSize { get; private set; }
public float TextSizeMsg { get; private set; }
public double GameTypeDivisor { get; private set; }

internal StatsGraph(double width = 3840, float textSize = 140, float textSizeMsg = 100)
internal StatsGraph(double width = 3840, float textSize = 140, float textSizeMsg = 100, int gameModesPlayed = 0)
{
TextSize = textSize;
TextSizeMsg = textSizeMsg;
GameTypeDivisor = (4 - (gameModesPlayed == 0 ? 3 : gameModesPlayed));
TextSize = textSize/(float)GameTypeDivisor;
TextSizeMsg = textSizeMsg / (float)GameTypeDivisor;
GraphWidth = width;


font = new(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.TimesRoman), TextSize);
fontMessage = new(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.TimesItalic), TextSizeMsg);
Expand All @@ -52,7 +55,7 @@ private Document CreateDocument(double height)
{
Document doc = new();
doc.Pages.Add(new(GraphWidth, height));

LinearGradientBrush bkgBrush = new(new Point(0, 0),
new Point(GraphWidth, height),
new GradientStop(COL_BKG_GRAD_START, 0),
Expand Down Expand Up @@ -105,6 +108,8 @@ internal async Task<string> RenderCapsGraph(List<CapsRecord> capsScoresWhite, Li
{
return await Task<string>.Run(() =>
{
height /= GameTypeDivisor;

//Make sure the data is sorted correctly
capsScoresWhite = capsScoresWhite.OrderBy(item => item.GameDate).TakeLast((int)maxCapsGames).ToList();
capsScoresBlack = capsScoresBlack.OrderBy(item => item.GameDate).TakeLast((int)maxCapsGames).ToList();
Expand Down Expand Up @@ -162,12 +167,12 @@ internal async Task<string> RenderCapsGraph(List<CapsRecord> capsScoresWhite, Li

GraphicsPath gpWhiteSmooth = new();
_ = gpWhiteSmooth.AddSmoothSpline(gpWhitePoints.ToArray());
gpr.StrokePath(IS_SMOOTH_CAPS ? gpWhiteSmooth : gpWhite, COL_CAPS_WHITE, lineWidth: GRAPH_LINE_WIDTH);
gpr.StrokePath(IS_SMOOTH_CAPS ? gpWhiteSmooth : gpWhite, COL_CAPS_WHITE, lineWidth: GRAPH_LINE_WIDTH/GameTypeDivisor);


GraphicsPath gpBlackSmooth = new();
_ = gpBlackSmooth.AddSmoothSpline(gpBlackPoints.ToArray());
gpr.StrokePath(IS_SMOOTH_CAPS ? gpBlackSmooth : gpBlack, COL_CAPS_BLACK, lineWidth: GRAPH_LINE_WIDTH);
gpr.StrokePath(IS_SMOOTH_CAPS ? gpBlackSmooth : gpBlack, COL_CAPS_BLACK, lineWidth: GRAPH_LINE_WIDTH/GameTypeDivisor);

WriteMessage(gpr, height, $"* Based on the last {whiteMovingAv.Length}/{blackMovingAv.Length} games with available CAPs scores");
}
Expand All @@ -181,6 +186,7 @@ internal async Task<string> RenderAllCapsGraph(List<CapsRecord> capsScores, doub
{
return await Task<string>.Run(() =>
{
height /= GameTypeDivisor;
Document doc = CreateDocument(height);
VectSharp.Graphics gpr = doc.Pages[0].Graphics;

Expand Down Expand Up @@ -229,11 +235,12 @@ internal async Task<string> RenderAllCapsGraph(List<CapsRecord> capsScores, doub
}).ConfigureAwait(false);
}

internal async Task<string> RenderRatingGraph(List<(DateTime gameDate, int rating,
string gameType)> ratingsPostGame, double height = 1920)
internal async Task<string> RenderRatingGraph(List<(DateTime gameDate, int rating, string gameType)> ratingsPostGame,
double height = 1920)
{
return await Task<string>.Run(() =>
{
height /= GameTypeDivisor;
Document doc = CreateDocument(height);
VectSharp.Graphics gpr = doc.Pages[0].Graphics;

Expand Down Expand Up @@ -283,8 +290,8 @@ internal async Task<string> RenderRatingGraph(List<(DateTime gameDate, int ratin

WriteRangeMessage(gpr, height, $"{graphMin}", $"{graphMax}");

gpr.FillRectangle(0, ((graphMax - ratingsPostGameOrdered[^1].rating) * RatingStepY) - (CUR_RATING_BAR_HEIGHT / 2),
GraphWidth, CUR_RATING_BAR_HEIGHT,
gpr.FillRectangle(0, ((graphMax - ratingsPostGameOrdered[^1].rating) * RatingStepY) - ((CUR_RATING_BAR_HEIGHT / GameTypeDivisor) / 2),
GraphWidth, CUR_RATING_BAR_HEIGHT / GameTypeDivisor,
COL_RATING);
}

Expand All @@ -293,11 +300,13 @@ internal async Task<string> RenderRatingGraph(List<(DateTime gameDate, int ratin
}).ConfigureAwait(false);
}

internal async Task<string> RenderAverageStatsGraph(List<(string TimeControl, int VsMin, int Worst, int LossAv, int DrawAv,
int WinAv, int Best, int VsMax)> graphData, double height = 1280)
internal async Task<string> RenderAverageStatsGraph(List<(string TimeControl, int VsMin, int Worst, int LossAv, int DrawAv, int WinAv, int Best, int VsMax)> graphData,
double height = 1280)
{
return await Task<string>.Run(() =>
{
height /= ((double)GameTypeDivisor);

int graphMinCalc = graphData.Where(x => x.WinAv != 0 && x.LossAv != 0).Select(x => x.WinAv).DefaultIfEmpty(0).Min();
int graphMaxCalc = graphData.Where(x => x.WinAv != 0 && x.LossAv != 0).Select(x => x.LossAv).DefaultIfEmpty(0).Max();

Expand Down
15 changes: 8 additions & 7 deletions ChessStats/ChessStats/Helpers/StatsHtml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Text;

namespace ChessStats.Helpers
Expand All @@ -12,7 +13,7 @@ public static string GetHtmlTail(Uri chessdotcomUrl, string versionNumber, strin
: $"<div class='footer'><br/><hr/><i>Generated by ChessStats (for <a href='{chessdotcomUrl.OriginalString}'>Chess.com</a>)&nbsp;ver. {versionNumber}<br/><a href='{projectLink}'>{projectLink}</a></i><br/><br/><br/></div>";
}

public static string GetHtmlTop(string pageTitle, string backgroundImage, string favIconImage, string font700Fragment, string font800Fragment)
public static string GetHtmlTop(string pageTitle, string backgroundImage, string favIconImage, string font700Fragment, string font800Fragment, int controlsPlayed=3)
{
StringBuilder htmlReport = new();
_ = htmlReport.AppendLine("<!DOCTYPE html>")
Expand Down Expand Up @@ -53,12 +54,15 @@ public static string GetHtmlTop(string pageTitle, string backgroundImage, string
.AppendLine(" .headRow {display: grid; grid-template-columns: 200px auto; grid-gap: 0px; border:0px; height: auto; padding: 0px; }")
.AppendLine(" .headRow > div {padding: 0px; }")
.AppendLine(" .headBox img {vertical-align: middle}")
.AppendLine(" .ratingRow {display: grid;grid-template-columns: auto auto auto;grid-gap: 20px;padding: 10px;}")
.AppendLine($" .ratingRow {{display: grid;grid-template-columns: {string.Join(" ","auto auto auto".Split(" ").Take(controlsPlayed))} ;grid-gap: 20px;padding: 10px;}}")
.AppendLine(" .ratingRow > div {font-family: Montserrat; font-weight: 700; text-align: center; padding: 0px; color: whitesmoke; font-size: 15px; font-weight: bold;}")
.AppendLine(" .ratingBox {cursor: pointer;}")
.AppendLine(" .graphRow {display: grid;grid-template-columns: auto auto auto;grid-gap: 10px;padding: 5px;}")
.AppendLine($" .graphRow {{display: grid;grid-template-columns: {string.Join(" ", "auto auto auto".Split(" ").Take(controlsPlayed))} ;grid-gap: 10px;padding: 5px;}}")
.AppendLine(" .graphRow > div {font-family: Montserrat; font-weight: 700; text-align: center; padding: 0px; color: whitesmoke; font-size: 15px; font-weight: bold;}")
.AppendLine(" .graphBox img { max-width:100%; height:auto; }")
.AppendLine(" .graphBox img { width:100%; height:auto; object-fit: cover; }")
.AppendLine(" .graphCapsRow {display: grid;grid-template-columns: 60% auto;grid-gap: 10px;padding: 5px;}")
.AppendLine(" .graphCapsRow>div {font-family: Montserrat;font-weight: 700;text-align: center;padding: 0px;color: whitesmoke;font-size: 15px;font-weight: bold;}")
.AppendLine(" .graphCapsBox img {max-width: 100%; width: auto;height: auto;}")
.AppendLine(" .yearSplit {border-top: thin dotted; border-color: #1583b7;}")
.AppendLine(" .higher {background-color: hsla(120, 100%, 50%, 0.25);}")
.AppendLine(" .lower {background-color: hsla(0, 100%, 70%, 0.4);}")
Expand All @@ -70,9 +74,6 @@ public static string GetHtmlTop(string pageTitle, string backgroundImage, string
.AppendLine(" .capsRollingTable tbody td:nth-child(1) {font-size: 14px;font-weight: bold;}")
.AppendLine(" .playingStatsTable tbody td:nth-child(1) {font-size: 14px;font-weight: bold;}")
.AppendLine(" .playingStatsMonthTable tbody td:nth-child(1) {font-size: 14px;font-weight: bold;}")
.AppendLine(" .graphCapsRow {display: grid;grid-template-columns: 60% auto;grid-gap: 10px;padding: 5px;}")
.AppendLine(" .graphCapsRow>div {font-family: Montserrat;font-weight: 700;text-align: center;padding: 0px;color: whitesmoke;font-size: 15px;font-weight: bold;}")
.AppendLine(" .graphCapsBox img {max-width: 100%; width: auto;height: auto;}")
.AppendLine(" .playingStatsTable tbody td:nth-child(5) {border-right: thin solid; border-color: #1583b7;}")
.AppendLine(" .playingStatsTable tbody td:nth-child(8) {border-left: thin dotted; border-color: #1583b7;}")
.AppendLine(" .playingStatsTable tbody td:nth-child(11) {border-left: thin dotted; border-color: #1583b7;}")
Expand Down
Loading