mattercontrol/original/MatterControlLib/CustomWidgets/DataViewGraph.cs

260 lines
5.6 KiB
C#
Raw Normal View History

2023-06-30 18:12:53 -07:00
/*
Copyright (c) 2023, Lars Brubaker
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
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;
2017-10-31 11:43:25 -07:00
private ColorF LineColor = ColorF.Black;
2017-06-30 18:00:02 -07:00
public DataViewGraph()
{
dataHistoryArray = new HistoryData(10);
DoubleBuffer = true;
}
2018-10-18 17:57:45 -07:00
public bool DynamicallyScaleRange { get; set; } = true;
private Color _goalColor = Color.Yellow;
2017-10-31 11:43:25 -07:00
public Color GoalColor
{
get
{
return _goalColor;
}
set
{
_goalColor = value;
Invalidate();
}
}
private 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)));
2017-06-30 18:00:02 -07:00
base.LocalBounds = value;
}
}
public double MaxValue { get; set; } = double.MinValue;
2017-06-30 18:00:02 -07:00
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)
2017-06-30 18:00:02 -07:00
{
2018-10-18 17:57:45 -07:00
if (DynamicallyScaleRange)
2017-06-30 18:00:02 -07:00
{
MaxValue = System.Math.Max(MaxValue, newData);
MinValue = System.Math.Min(MinValue, newData);
2017-06-30 18:00:02 -07:00
}
dataHistoryArray.Add(newData);
2015-04-08 15:20:10 -07:00
TotalAdds++;
if (this.ActuallyVisibleOnScreen())
{
Invalidate();
}
2017-06-30 18:00:02 -07:00
}
public double GetAverageValue()
{
return dataHistoryArray.GetAverageValue();
}
public override void OnDraw(Graphics2D graphics2D)
{
var linesToDrawStorage = new VertexStorage();
double range = MaxValue - MinValue;
2017-06-30 18:00:02 -07:00
if (ShowGoal)
{
var yPos = (GoalValue - MinValue) * Height / range;
graphics2D.Line(0, yPos, Width, yPos, GoalColor);
}
2017-10-31 11:43:25 -07:00
Color backgroundGridColor = Color.Gray;
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);
2017-10-31 11:43:25 -07:00
graphics2D.Line(xPos, inset, xPos, Height - inset, new Color(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);
2017-06-30 18:00:02 -07:00
}
else
{
linesToDrawStorage.LineTo(i + Width - dataHistoryArray.Count, (dataHistoryArray.GetItem(i) - MinValue) * Height / range);
2017-06-30 18:00:02 -07:00
}
}
graphics2D.Render(new Stroke(linesToDrawStorage), LineColor);
base.OnDraw(graphics2D);
}
public void Reset()
{
dataHistoryArray.Reset();
}
2015-04-08 15:20:10 -07:00
2023-06-30 18:12:53 -07:00
public void AddData(List<int> salesData)
{
foreach (var data in salesData)
{
AddData(data);
}
}
internal class HistoryData
2015-04-08 15:20:10 -07:00
{
2017-06-30 18:00:02 -07:00
internal double currentDataSum;
private readonly int capacity;
private readonly 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)
2015-04-08 15:20:10 -07:00
{
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
}
2015-04-08 15:20:10 -07:00
data.Add(value);
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)
2015-04-08 15:20:10 -07:00
{
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()
{
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;
2015-04-08 15:20:10 -07:00
}
internal double GetMinValue()
{
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;
2015-04-08 15:20:10 -07:00
}
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
}
}
}
2015-04-08 15:20:10 -07:00
}