1 using Microsoft.Xna.Framework;
2 using Microsoft.Xna.Framework.Graphics;
10 private float[] values;
12 public Graph(
int arraySize = 100)
14 values =
new float[arraySize];
19 float maxValue = 0.0f;
20 for (
int i = 0; i < values.Length; i++)
22 if (values[i] > maxValue) maxValue = values[i];
29 return values.Length == 0 ? 0.0f : values.Average();
34 for (
int i = values.Length - 1; i > 0; i--)
36 values[i] = values[i - 1];
41 public delegate
void GraphDelegate(SpriteBatch spriteBatch,
float value,
int order, Vector2 position);
43 public void Draw(SpriteBatch spriteBatch, Rectangle rect,
float? maxValue =
null,
float xOffset = 0, Color? color =
null,
GraphDelegate doForEachValue =
null)
45 color ??= Color.White;
46 float graphMaxVal = 1.0f;
51 else if (maxValue > 0.0f)
53 graphMaxVal = (float)maxValue;
56 GUI.DrawRectangle(spriteBatch, rect, Color.White);
58 if (values.Length == 0) {
return; }
60 float lineWidth = rect.Width / (float)(values.Length - 2);
61 float yScale = rect.Height / graphMaxVal;
63 Vector2 prevPoint =
new Vector2(rect.Right, rect.Bottom - (values[1] + (values[0] - values[1]) * xOffset) * yScale);
64 float currX = rect.Right - ((xOffset - 1.0f) * lineWidth);
65 for (
int i = 1; i < values.Length - 1; i++)
67 float value = values[i];
69 Vector2 newPoint =
new Vector2(currX, rect.Bottom - value * yScale);
70 GUI.DrawLine(spriteBatch, prevPoint, newPoint -
new Vector2(1.0f, 0), color.Value);
72 doForEachValue?.Invoke(spriteBatch, value, i, newPoint);
74 int lastIndex = values.Length - 1;
75 float lastValue = values[lastIndex];
76 Vector2 lastPoint =
new Vector2(rect.X, rect.Bottom - (lastValue + (values[values.Length - 2] - lastValue) * xOffset) * yScale);
77 GUI.DrawLine(spriteBatch, prevPoint, lastPoint, color.Value);
78 doForEachValue?.Invoke(spriteBatch, lastValue, lastIndex, lastPoint);
delegate void GraphDelegate(SpriteBatch spriteBatch, float value, int order, Vector2 position)
void Draw(SpriteBatch spriteBatch, Rectangle rect, float? maxValue=null, float xOffset=0, Color? color=null, GraphDelegate doForEachValue=null)
void Update(float newValue)