Improve graph. Add things like scales/axis labelling.

This commit is contained in:
Scott_NZ
2012-11-27 17:11:47 +13:00
parent b6e8c9c9ea
commit e424883857
6 changed files with 109 additions and 40 deletions

View File

@@ -18,68 +18,132 @@ namespace OpenRA.Widgets
{
public class ObserverStatsGraphWidget : Widget
{
public Func<IEnumerable<Pair<Player, IEnumerable<float>>>> GetDataSource = () => null;
public Func<float> GetDataScale = () => 1.0f;
public Func<string> GetLastValueFormat = () => "{0}";
public Func<int> GetNodeCount = () => 20;
public Func<int> GetNodeStep = () => 5;
public Func<IEnumerable<Pair<Player, IEnumerable<float>>>> GetDataSource;
public Func<float> GetDataScale;
public Func<string> GetValueFormat;
public Func<string> GetXAxisValueFormat;
public Func<string> GetYAxisValueFormat;
public Func<int> GetVisibleNodeCount;
public Func<string> GetXAxisLabel;
public Func<string> GetYAxisLabel;
public Func<bool> GetDisplayYAxisZero;
public string ValueFormat = "{0}";
public string XAxisValueFormat = "{0}";
public string YAxisValueFormat = "{0}";
public int VisibleNodeCount = 10;
public string XAxisLabel = "";
public string YAxisLabel = "";
public bool DisplayYAxisZero = true;
public ObserverStatsGraphWidget() : base() { }
public ObserverStatsGraphWidget()
: base()
{
GetValueFormat = () => ValueFormat;
GetXAxisValueFormat = () => XAxisValueFormat;
GetYAxisValueFormat = () => YAxisValueFormat;
GetVisibleNodeCount = () => VisibleNodeCount;
GetXAxisLabel = () => XAxisLabel;
GetYAxisLabel = () => YAxisLabel;
GetDisplayYAxisZero = () => DisplayYAxisZero;
}
protected ObserverStatsGraphWidget(ObserverStatsGraphWidget other)
: base(other)
{
GetDataSource = other.GetDataSource;
GetValueFormat = other.GetValueFormat;
GetXAxisValueFormat = other.GetXAxisValueFormat;
GetYAxisValueFormat = other.GetYAxisValueFormat;
GetVisibleNodeCount = other.GetVisibleNodeCount;
GetXAxisLabel = other.GetXAxisLabel;
GetYAxisLabel = other.GetYAxisLabel;
GetDisplayYAxisZero = other.GetDisplayYAxisZero;
ValueFormat = other.ValueFormat;
XAxisValueFormat = other.XAxisValueFormat;
YAxisValueFormat = other.YAxisValueFormat;
VisibleNodeCount = other.VisibleNodeCount;
XAxisLabel = other.XAxisLabel;
YAxisLabel = other.YAxisLabel;
DisplayYAxisZero = other.DisplayYAxisZero;
}
public override void Draw()
{
if (GetDataSource == null || !GetDataSource().Any())
{
return;
}
var rect = RenderBounds;
var origin = new float2(rect.Left, rect.Bottom);
var basis = new float2(rect.Width / 100, rect.Height / 100);
Game.Renderer.LineRenderer.DrawLine(origin, origin + new float2(100, 0) * basis, Color.White, Color.White);
Game.Renderer.LineRenderer.DrawLine(origin, origin - new float2(0, 100) * basis, Color.White, Color.White);
Game.Renderer.LineRenderer.DrawLine(origin + new float2(100, 0) * basis, origin + new float2(100, -100) * basis, Color.White, Color.White);
var width = rect.Width;
var height = rect.Height;
Game.Renderer.LineRenderer.DrawLine(origin, origin + new float2(width, 0), Color.White, Color.White);
Game.Renderer.LineRenderer.DrawLine(origin, origin + new float2(0, -height), Color.White, Color.White);
Game.Renderer.LineRenderer.DrawLine(origin + new float2(width, 0), origin + new float2(width, -height), Color.White, Color.White);
Game.Renderer.LineRenderer.DrawLine(origin + new float2(0, -height), origin + new float2(width, -height), Color.White, Color.White);
var tinyBold = Game.Renderer.Fonts["TinyBold"];
var bold = Game.Renderer.Fonts["Bold"];
var i = 0;
foreach (var pair in GetDataSource())
var visibleNodeCount = GetVisibleNodeCount();
var visibleNodeStep = width / visibleNodeCount;
var actualNodeCount = GetDataSource().First().Second.Count();
var visibleNodeStart = Math.Max(0, actualNodeCount - visibleNodeCount);
var visibleNodeEnd = Math.Max(actualNodeCount, visibleNodeCount);
float maxValue = GetDataSource().Select(p => p.Second).SelectMany(d => d).Max();
var scale = (100 / maxValue) * 3;
//todo: make this stuff not draw outside of the RenderBounds
for (int n = visibleNodeStart, i = 0; n <= visibleNodeEnd; n++, i++)
{
var player = pair.First;
var data = pair.Second.Reverse().Take(GetNodeCount()).Reverse();
var x = i * visibleNodeStep;
Game.Renderer.LineRenderer.DrawLine(origin + new float2(x, 0), origin + new float2(x, -5), Color.White, Color.White);
tinyBold.DrawText(GetXAxisValueFormat().F(n), origin + new float2(x, 2), Color.White);
}
bold.DrawText(GetXAxisLabel(), origin + new float2(width / 2, 20), Color.White);
for (var i = (GetDisplayYAxisZero() ? 0f : height / 10); i <= height; i += height / 10)
{
var value = i / scale;
Game.Renderer.LineRenderer.DrawLine(origin + new float2(width - 5, -i), origin + new float2(width, -i), Color.White, Color.White);
tinyBold.DrawText(GetYAxisValueFormat().F(value), origin + new float2(width + 2, -i), Color.White);
}
bold.DrawText(GetYAxisLabel(), origin + new float2(width + 40, -(height / 2)), Color.White);
var playerNameOffset = 0;
foreach (var playerDataPair in GetDataSource())
{
var player = playerDataPair.First;
var data = playerDataPair.Second;
var color = player.ColorRamp.GetColor(0);
if (data.Any())
{
var scale = GetDataScale();
data = data.Reverse().Take(visibleNodeCount).Reverse();
var scaledData = data.Select(d => d * scale);
var n = 0;
var step = GetNodeStep();
var x = 0f;
scaledData.Aggregate((a, b) =>
{
Game.Renderer.LineRenderer.DrawLine(
origin + new float2(n, -a) * basis,
origin + new float2(n + step, -b) * basis,
origin + new float2(x, -a),
origin + new float2(x + visibleNodeStep, -b),
color, color);
n += step;
x += visibleNodeStep;
return b;
});
var lastValue = data.Last();
if (lastValue != 0)
var value = data.Last();
if (value != 0)
{
var scaledLastValue = lastValue * scale;
var lastValueFormat = GetLastValueFormat();
if (lastValueFormat != null)
{
tinyBold.DrawText(lastValueFormat.F(lastValue), origin + new float2(n, -scaledLastValue - 2) * basis, color);
}
var scaledValue = value * scale;
tinyBold.DrawText(GetValueFormat().F(value), origin + new float2(x, -scaledValue - 2), color);
}
}
tinyBold.DrawText(player.PlayerName, new float2(rect.Left, rect.Top) + new float2(5, 10 * i - 3), color);
i++;
tinyBold.DrawText(player.PlayerName, new float2(rect.Left, rect.Top) + new float2(5, 10 * playerNameOffset + 3), color);
playerNameOffset++;
}
}