mattercontrol/CustomWidgets/DataViewGraph.cs

209 lines
4 KiB
C#
Raw Normal View History

2017-06-30 18:00:02 -07:00
using System;
using System.Collections.Generic;
using MatterHackers.Agg;
using MatterHackers.Agg.UI;
using MatterHackers.Agg.VertexSource;
2017-06-30 18:00:02 -07:00
namespace MatterHackers.MatterControl.CustomWidgets
{
2017-06-30 18:00:02 -07:00
public class DataViewGraph : GuiWidget
2015-04-08 15:20:10 -07:00
{
2017-06-30 18:00:02 -07:00
private HistoryData dataHistoryArray;
private RGBA_Floats LineColor = RGBA_Floats.Black;
public DataViewGraph()
{
dataHistoryArray = new HistoryData(10);
DoubleBuffer = true;
}
public bool DynamiclyScaleRange { get; set; } = true;
RGBA_Bytes _goalColor = RGBA_Bytes.Yellow;
public RGBA_Bytes GoalColor
{
get { return _goalColor; }
set
{
_goalColor = value;
Invalidate();
}
}
double _goalValue;
public double GoalValue
{
get { return _goalValue; }
set
{
_goalValue = value;
Invalidate();
}
}
2017-06-30 18:00:02 -07:00
public override RectangleDouble LocalBounds
{
get => base.LocalBounds; set
{
dataHistoryArray = new HistoryData(Math.Min(1000, Math.Max(1, (int)(value.Width))));
base.LocalBounds = value;
}
}
public double MaxValue { get; set; } = double.MinValue;
public double MinValue { get; set; } = double.MaxValue;
public bool ShowGoal { get; set; }
public int TotalAdds { get; private set; }
2017-06-30 18:00:02 -07:00
public void AddData(double NewData)
{
if (DynamiclyScaleRange)
{
MaxValue = System.Math.Max(MaxValue, NewData);
MinValue = System.Math.Min(MinValue, NewData);
}
dataHistoryArray.Add(NewData);
2015-04-08 15:20:10 -07:00
TotalAdds++;
2017-06-30 18:00:02 -07:00
Invalidate();
}
public double GetAverageValue()
{
return dataHistoryArray.GetAverageValue();
}
public override void OnDraw(Graphics2D graphics2D)
{
var linesToDrawStorage = new PathStorage();
double Range = (MaxValue - MinValue);
if (ShowGoal)
{
var yPos = (GoalValue - MinValue) * Height / Range;
graphics2D.Line(0, yPos, Width, yPos, GoalColor);
}
RGBA_Bytes backgroundGridColor = RGBA_Bytes.Gray;
graphics2D.Line(0, 0, Width, 0, backgroundGridColor);
graphics2D.Line(0, Height, Width, Height, backgroundGridColor);
double pixelSkip = Height;
for (int i = 0; i < Width / pixelSkip; i++)
{
double xPos = Width - ((i * pixelSkip + TotalAdds) % Width);
2017-09-08 13:46:06 -07:00
int inset = (int)((i % 2) == 0 ? Height / 6 : Height / 3);
graphics2D.Line(xPos, inset, xPos, Height - inset, new RGBA_Bytes(backgroundGridColor, 120));
}
2017-06-30 18:00:02 -07:00
for (int i = 0; i < Width - 1; i++)
{
if (i == 0)
{
linesToDrawStorage.MoveTo(i + Width - dataHistoryArray.Count, ((dataHistoryArray.GetItem(i) - MinValue) * Height / Range));
}
else
{
linesToDrawStorage.LineTo(i + Width - dataHistoryArray.Count, ((dataHistoryArray.GetItem(i) - MinValue) * Height / Range));
}
}
graphics2D.Render(new Stroke(linesToDrawStorage), LineColor);
base.OnDraw(graphics2D);
}
public void Reset()
{
dataHistoryArray.Reset();
}
2015-04-08 15:20:10 -07:00
internal class HistoryData
{
2017-06-30 18:00:02 -07:00
internal double currentDataSum;
private int capacity;
private List<double> data;
2015-04-08 15:20:10 -07:00
2017-06-30 18:00:02 -07:00
internal HistoryData(int capacity)
2015-04-08 15:20:10 -07:00
{
this.capacity = capacity;
data = new List<double>();
2015-04-08 15:20:10 -07:00
Reset();
}
public int Count
{
get
{
return data.Count;
2015-04-08 15:20:10 -07:00
}
}
internal void Add(double Value)
{
if (data.Count == capacity)
2015-04-08 15:20:10 -07:00
{
2017-06-30 18:00:02 -07:00
currentDataSum -= data[0];
data.RemoveAt(0);
2017-06-30 18:00:02 -07:00
}
data.Add(Value);
2015-04-08 15:20:10 -07:00
currentDataSum += Value;
2015-04-08 15:20:10 -07:00
}
2017-06-30 18:00:02 -07:00
internal double GetAverageValue()
2015-04-08 15:20:10 -07:00
{
2017-06-30 18:00:02 -07:00
return currentDataSum / data.Count;
2015-04-08 15:20:10 -07:00
}
internal double GetItem(int ItemIndex)
{
if (ItemIndex < data.Count)
2015-04-08 15:20:10 -07:00
{
return data[ItemIndex];
2015-04-08 15:20:10 -07:00
}
else
{
return 0;
}
}
internal double GetMaxValue()
{
2017-06-30 18:00:02 -07:00
double Max = -double.MinValue;
for (int i = 0; i < data.Count; i++)
2015-04-08 15:20:10 -07:00
{
if (data[i] > Max)
2015-04-08 15:20:10 -07:00
{
Max = data[i];
2015-04-08 15:20:10 -07:00
}
}
return Max;
}
internal double GetMinValue()
{
2017-06-30 18:00:02 -07:00
double Min = double.MaxValue;
for (int i = 0; i < data.Count; i++)
2015-04-08 15:20:10 -07:00
{
if (data[i] < Min)
2015-04-08 15:20:10 -07:00
{
Min = data[i];
2015-04-08 15:20:10 -07:00
}
}
return Min;
}
2017-06-30 18:00:02 -07:00
internal void Reset()
2015-04-08 15:20:10 -07:00
{
2017-06-30 18:00:02 -07:00
currentDataSum = 0;
data.Clear();
2015-04-08 15:20:10 -07:00
}
};
};
}