2020-11-14 09:46:26 +08:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using JetBrains.Annotations;
|
2020-11-14 11:38:02 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using osu.Game.Graphics.Sprites;
|
|
|
|
|
using osu.Framework.Utils;
|
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Game.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
2020-11-22 07:34:29 +08:00
|
|
|
|
using osuTK;
|
2020-11-22 09:40:55 +08:00
|
|
|
|
using static osu.Game.Users.User;
|
2020-11-14 09:46:26 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Profile.Sections.Historical
|
|
|
|
|
{
|
|
|
|
|
public class ProfileLineChart : CompositeDrawable
|
|
|
|
|
{
|
|
|
|
|
private UserHistoryCount[] values;
|
|
|
|
|
|
2020-11-15 00:17:01 +08:00
|
|
|
|
[NotNull]
|
2020-11-14 09:46:26 +08:00
|
|
|
|
public UserHistoryCount[] Values
|
|
|
|
|
{
|
|
|
|
|
get => values;
|
|
|
|
|
set
|
|
|
|
|
{
|
2020-11-24 04:24:37 +08:00
|
|
|
|
if (value.Length == 0)
|
|
|
|
|
throw new ArgumentException("At least one value expected!", nameof(value));
|
|
|
|
|
|
|
|
|
|
graph.Values = values = value;
|
2020-11-14 11:38:02 +08:00
|
|
|
|
|
|
|
|
|
createRowTicks();
|
2020-11-14 12:28:01 +08:00
|
|
|
|
createColumnTicks();
|
2020-11-14 09:46:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly UserHistoryGraph graph;
|
2020-11-14 11:38:02 +08:00
|
|
|
|
private readonly Container<TickText> rowTicksContainer;
|
2020-11-14 12:28:01 +08:00
|
|
|
|
private readonly Container<TickText> columnTicksContainer;
|
2020-11-14 11:38:02 +08:00
|
|
|
|
private readonly Container<TickLine> rowLinesContainer;
|
2020-11-14 12:28:01 +08:00
|
|
|
|
private readonly Container<TickLine> columnLinesContainer;
|
2020-11-14 09:46:26 +08:00
|
|
|
|
|
|
|
|
|
public ProfileLineChart()
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X;
|
|
|
|
|
Height = 250;
|
|
|
|
|
InternalChild = new GridContainer
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
ColumnDimensions = new[]
|
|
|
|
|
{
|
|
|
|
|
new Dimension(GridSizeMode.AutoSize),
|
|
|
|
|
new Dimension()
|
|
|
|
|
},
|
|
|
|
|
RowDimensions = new[]
|
|
|
|
|
{
|
|
|
|
|
new Dimension(),
|
|
|
|
|
new Dimension(GridSizeMode.AutoSize)
|
|
|
|
|
},
|
|
|
|
|
Content = new[]
|
|
|
|
|
{
|
|
|
|
|
new Drawable[]
|
|
|
|
|
{
|
2020-11-14 11:38:02 +08:00
|
|
|
|
rowTicksContainer = new Container<TickText>
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Y,
|
|
|
|
|
AutoSizeAxes = Axes.X
|
|
|
|
|
},
|
|
|
|
|
new Container
|
2020-11-14 09:46:26 +08:00
|
|
|
|
{
|
2020-11-14 11:38:02 +08:00
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2020-11-14 12:28:01 +08:00
|
|
|
|
new Container
|
2020-11-14 11:38:02 +08:00
|
|
|
|
{
|
2020-11-14 12:28:01 +08:00
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Children = new[]
|
|
|
|
|
{
|
|
|
|
|
rowLinesContainer = new Container<TickLine>
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both
|
|
|
|
|
},
|
|
|
|
|
columnLinesContainer = new Container<TickLine>
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-14 11:38:02 +08:00
|
|
|
|
},
|
|
|
|
|
graph = new UserHistoryGraph
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-14 09:46:26 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
2020-11-15 01:17:32 +08:00
|
|
|
|
new[]
|
2020-11-14 09:46:26 +08:00
|
|
|
|
{
|
|
|
|
|
Empty(),
|
2020-11-14 12:28:01 +08:00
|
|
|
|
columnTicksContainer = new Container<TickText>
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
Padding = new MarginPadding { Top = 10 }
|
|
|
|
|
}
|
2020-11-14 09:46:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2020-11-14 11:38:02 +08:00
|
|
|
|
|
|
|
|
|
private void createRowTicks()
|
|
|
|
|
{
|
|
|
|
|
rowTicksContainer.Clear();
|
|
|
|
|
rowLinesContainer.Clear();
|
|
|
|
|
|
|
|
|
|
var min = values.Select(v => v.Count).Min();
|
|
|
|
|
var max = values.Select(v => v.Count).Max();
|
|
|
|
|
|
2020-11-23 13:52:29 +08:00
|
|
|
|
var tick = getTick(max - min, 6);
|
2020-11-14 11:38:02 +08:00
|
|
|
|
|
2020-11-23 13:52:29 +08:00
|
|
|
|
// Prevent infinite loop in case if tick is zero
|
|
|
|
|
if (tick == 0)
|
|
|
|
|
tick = 1;
|
2020-11-22 09:28:17 +08:00
|
|
|
|
|
2020-11-22 08:49:00 +08:00
|
|
|
|
double rollingRow = 0;
|
2020-11-14 11:38:02 +08:00
|
|
|
|
|
2020-11-15 01:07:52 +08:00
|
|
|
|
while (rollingRow <= max)
|
2020-11-14 11:38:02 +08:00
|
|
|
|
{
|
2020-11-22 08:49:00 +08:00
|
|
|
|
if (rollingRow >= min)
|
|
|
|
|
{
|
|
|
|
|
var y = -Interpolation.ValueAt(rollingRow, 0, 1f, min, max);
|
2020-11-23 13:52:29 +08:00
|
|
|
|
addRowTick(y, rollingRow);
|
2020-11-22 08:49:00 +08:00
|
|
|
|
}
|
2020-11-14 11:38:02 +08:00
|
|
|
|
|
2020-11-22 08:49:00 +08:00
|
|
|
|
rollingRow += tick;
|
2020-11-14 11:38:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-14 12:28:01 +08:00
|
|
|
|
private void createColumnTicks()
|
|
|
|
|
{
|
|
|
|
|
columnTicksContainer.Clear();
|
|
|
|
|
columnLinesContainer.Clear();
|
|
|
|
|
|
2020-11-22 09:28:17 +08:00
|
|
|
|
var totalMonths = values.Length;
|
2020-11-14 12:28:01 +08:00
|
|
|
|
|
2020-11-22 08:11:38 +08:00
|
|
|
|
int monthsPerTick = 1;
|
2020-11-14 12:28:01 +08:00
|
|
|
|
|
2020-11-22 08:58:56 +08:00
|
|
|
|
if (totalMonths > 80)
|
|
|
|
|
monthsPerTick = 12;
|
|
|
|
|
else if (totalMonths >= 45)
|
|
|
|
|
monthsPerTick = 3;
|
|
|
|
|
else if (totalMonths > 20)
|
|
|
|
|
monthsPerTick = 2;
|
2020-11-14 12:28:01 +08:00
|
|
|
|
|
2020-11-22 08:11:38 +08:00
|
|
|
|
for (int i = 0; i < totalMonths; i += monthsPerTick)
|
2020-11-14 12:28:01 +08:00
|
|
|
|
{
|
2020-11-22 09:28:17 +08:00
|
|
|
|
var x = (float)i / (totalMonths - 1);
|
2020-11-22 08:11:38 +08:00
|
|
|
|
addColumnTick(x, values[i].Date);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-14 12:28:01 +08:00
|
|
|
|
|
2020-11-23 13:52:29 +08:00
|
|
|
|
private void addRowTick(float y, double value)
|
2020-11-22 08:11:38 +08:00
|
|
|
|
{
|
|
|
|
|
rowTicksContainer.Add(new TickText
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.BottomRight,
|
|
|
|
|
Origin = Anchor.CentreRight,
|
|
|
|
|
RelativePositionAxes = Axes.Y,
|
|
|
|
|
Margin = new MarginPadding { Right = 3 },
|
2020-11-23 13:52:29 +08:00
|
|
|
|
Text = value.ToString("N0"),
|
2020-11-22 08:11:38 +08:00
|
|
|
|
Font = OsuFont.GetFont(size: 12),
|
|
|
|
|
Y = y
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
rowLinesContainer.Add(new TickLine
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.BottomRight,
|
|
|
|
|
Origin = Anchor.CentreRight,
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
RelativePositionAxes = Axes.Y,
|
|
|
|
|
Height = 0.1f,
|
|
|
|
|
EdgeSmoothness = Vector2.One,
|
|
|
|
|
Y = y
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-11-14 12:28:01 +08:00
|
|
|
|
|
2020-11-22 08:11:38 +08:00
|
|
|
|
private void addColumnTick(float x, DateTime value)
|
|
|
|
|
{
|
|
|
|
|
columnTicksContainer.Add(new TickText
|
|
|
|
|
{
|
|
|
|
|
Origin = Anchor.CentreLeft,
|
|
|
|
|
RelativePositionAxes = Axes.X,
|
|
|
|
|
Text = value.ToString("MMM yyyy"),
|
|
|
|
|
Font = OsuFont.GetFont(size: 12, weight: FontWeight.SemiBold),
|
|
|
|
|
Rotation = 45,
|
|
|
|
|
X = x
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
columnLinesContainer.Add(new TickLine
|
|
|
|
|
{
|
|
|
|
|
Origin = Anchor.TopCentre,
|
|
|
|
|
RelativeSizeAxes = Axes.Y,
|
|
|
|
|
RelativePositionAxes = Axes.X,
|
|
|
|
|
Width = 0.1f,
|
|
|
|
|
EdgeSmoothness = Vector2.One,
|
|
|
|
|
X = x
|
|
|
|
|
});
|
2020-11-14 12:28:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-23 13:52:29 +08:00
|
|
|
|
private long getTick(long range, int maxTicksCount)
|
2020-11-14 11:38:02 +08:00
|
|
|
|
{
|
2020-11-23 13:52:29 +08:00
|
|
|
|
var value = (float)range / (maxTicksCount - 1);
|
2020-11-22 08:49:00 +08:00
|
|
|
|
|
2020-11-14 12:28:01 +08:00
|
|
|
|
var exponent = Math.Floor(Math.Log10(value));
|
2020-11-14 11:38:02 +08:00
|
|
|
|
var fraction = value / Math.Pow(10, exponent);
|
|
|
|
|
|
|
|
|
|
double niceFraction;
|
|
|
|
|
|
2020-11-22 08:49:00 +08:00
|
|
|
|
if (fraction < 1.5)
|
|
|
|
|
niceFraction = 1.0;
|
|
|
|
|
else if (fraction < 3)
|
|
|
|
|
niceFraction = 2.0;
|
|
|
|
|
else if (fraction < 7)
|
|
|
|
|
niceFraction = 5.0;
|
2020-11-14 11:38:02 +08:00
|
|
|
|
else
|
2020-11-22 08:49:00 +08:00
|
|
|
|
niceFraction = 10.0;
|
2020-11-14 11:38:02 +08:00
|
|
|
|
|
2020-11-23 13:52:29 +08:00
|
|
|
|
return (long)(niceFraction * Math.Pow(10, exponent));
|
2020-11-14 11:38:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class TickText : OsuSpriteText
|
|
|
|
|
{
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OverlayColourProvider colourProvider)
|
|
|
|
|
{
|
|
|
|
|
Colour = colourProvider.Foreground1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class TickLine : Box
|
|
|
|
|
{
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OverlayColourProvider colourProvider)
|
|
|
|
|
{
|
|
|
|
|
Colour = colourProvider.Background6;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-14 09:46:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|