ChartWindow: Use BillingData and Curve

This commit is contained in:
2024-01-17 14:33:21 +01:00
parent b52c09a176
commit b6fd62f8ca
3 changed files with 159 additions and 136 deletions

View File

@ -7,18 +7,18 @@ using System.Text.Json.Nodes;
namespace Elwig.Helpers.Billing {
public class Graph : ICloneable {
public string Type { get; set; }
public int Num { get; set; }
public BillingData.CurveMode Mode { get; set; }
public int Id { get; set; }
private int MinX { get; set; }
private int MaxX { get; set; }
public string Contracts { get; set; }
public List<Tuple<string, string>> Contracts { get; set; }
public double[] DataX { get; set; }
public double[] DataY { get; set; }
public Graph(int num, int minX, int maxX) {
Type = "oe";
Num = num;
Contracts = "";
public Graph(int id, BillingData.CurveMode mode, int minX, int maxX) {
Id = id;
Mode = mode;
Contracts = new List<Tuple<string, string>>();
MinX = minX;
MaxX = maxX;
@ -26,21 +26,21 @@ namespace Elwig.Helpers.Billing {
DataY = DataGen.Zeros(MaxX - MinX + 1);
}
public Graph(string type, int num, JsonObject graphData, string contracts, int minX, int maxX) {
Type = type;
Num = num;
Contracts = contracts;
public Graph(int id, BillingData.CurveMode mode, Dictionary<double, decimal> data, int minX, int maxX) {
Id = id;
Mode = mode;
Contracts = new List<Tuple<string, string>>();
MinX = minX;
MaxX = maxX;
DataX = DataGen.Range(MinX, MaxX + 1);
DataY = DataGen.Zeros(MaxX - MinX + 1);
ParseGraphData(graphData);
ParseGraphData(data);
}
public Graph(string type, int num, int minX, int maxX, string contracts, double[] dataX, double[] dataY) {
Type = type;
Num = num;
public Graph(int id, BillingData.CurveMode mode, int minX, int maxX, List<Tuple<string, string>> contracts, double[] dataX, double[] dataY) {
Id = id;
Mode = mode;
MinX = minX;
MaxX = maxX;
Contracts = contracts;
@ -48,72 +48,78 @@ namespace Elwig.Helpers.Billing {
DataY = dataY;
}
private void ParseGraphData(JsonObject graphData) {
var GraphPoints = graphData.ToDictionary(p => int.Parse(p.Key[..^2]), p => (double)p.Value?.AsValue());
if (GraphPoints.Keys.Count < 1) {
private void ParseGraphData(Dictionary<double, decimal> graphPoints) {
if (graphPoints.Keys.Count < 1) {
return;
}
var minKey = GraphPoints.Keys.Order().First();
var maxKey = GraphPoints.Keys.OrderDescending().First();
var minKey = graphPoints.Keys.Order().First();
var maxKey = graphPoints.Keys.OrderDescending().First();
if (!GraphPoints.ContainsKey(MinX)) {
GraphPoints.Add(MinX, GraphPoints.GetValueOrDefault(minKey));
if (!graphPoints.ContainsKey(MinX)) {
graphPoints.Add(MinX, graphPoints.GetValueOrDefault(minKey));
}
if (!GraphPoints.ContainsKey(MaxX)) {
GraphPoints.Add(MaxX, GraphPoints.GetValueOrDefault(maxKey));
if (!graphPoints.ContainsKey(MaxX)) {
graphPoints.Add(MaxX, graphPoints.GetValueOrDefault(maxKey));
}
var keys = GraphPoints.Keys.Order().ToArray();
var keys = graphPoints.Keys.Order().ToArray();
for (int i = 0; i < keys.Length; i++) {
double point1Value = GraphPoints[keys[i]];
decimal point1Value = graphPoints[keys[i]];
if (i + 1 < keys.Length) {
double point2Value = GraphPoints[keys[i + 1]];
decimal point2Value = graphPoints[keys[i + 1]];
if (point1Value == point2Value) {
for (int j = keys[i] - MinX; j < keys[i + 1] - MinX; j++) {
DataY[j] = point1Value;
for (int j = (int)(keys[i] - MinX); j < keys[i + 1] - MinX; j++) {
DataY[j] = (double)point1Value;
}
} else {
int steps = Math.Abs(keys[i + 1] - keys[i]);
double step = (point2Value - point1Value) / steps;
int steps = (int)Math.Abs(keys[i + 1] - keys[i]);
decimal step = (point2Value - point1Value) / steps;
DataY[keys[i] - MinX] = point1Value;
DataY[keys[i + 1] - MinX] = point2Value;
DataY[(int)(keys[i] - MinX)] = (double)point1Value;
DataY[(int)(keys[i + 1] - MinX)] = (double)point2Value;
for (int j = keys[i] - MinX; j < keys[i + 1] - MinX - 1; j++) {
DataY[j + 1] = Math.Round(DataY[j] + step, 4); // TODO richtig runden
for (int j = (int)(keys[i] - MinX); j < keys[i + 1] - MinX - 1; j++) {
DataY[j + 1] = Math.Round(DataY[j] + (double)step, 4); // TODO richtig runden
}
}
}
else {
for (int j = keys[i] - MinX; j < DataX.Length; j++) {
DataY[j] = point1Value;
for (int j = (int)(keys[i] - MinX); j < DataX.Length; j++) {
DataY[j] = (double)point1Value;
}
}
}
}
public JsonObject ToJson() {
JsonObject graph = new();
var graph = new JsonObject {
["id"] = Id,
["mode"] = Mode.ToString().ToLower(),
};
var data = new JsonObject();
if (DataY[0] != DataY[1]) {
graph.Add(new KeyValuePair<string, JsonNode?>(DataX[0] + Type.ToLower(), Math.Round(DataY[0], 4)));
data.Add(new KeyValuePair<string, JsonNode?>(DataX[0] + Mode.ToString().ToLower(), Math.Round(DataY[0], 4)));
}
for (int i = 1; i < DataX.Length - 1; i++) {
if (Math.Round(DataY[i] - DataY[i - 1], 4) != Math.Round(DataY[i + 1] - DataY[i], 4)) {
graph.Add(new KeyValuePair<string, JsonNode?>(DataX[i] + Type.ToLower(), Math.Round(DataY[i], 4)));
if (Math.Round(DataY[i] - DataY[i - 1], 10) != Math.Round(DataY[i + 1] - DataY[i], 10)) {
data.Add(new KeyValuePair<string, JsonNode?>(DataX[i] + Mode.ToString().ToLower(), Math.Round(DataY[i], 4)));
}
}
if (DataY[^1] != DataY[^2]) {
graph.Add(new KeyValuePair<string, JsonNode?>(DataX[^1] + Type.ToLower(), Math.Round(DataY[^1], 4)));
data.Add(new KeyValuePair<string, JsonNode?>(DataX[^1] + Mode.ToString().ToLower(), Math.Round(DataY[^1], 4)));
}
graph["data"] = data;
return graph;
}
public object Clone() {
return new Graph(Type, Num, MinX, MaxX, Contracts, (double[])DataX.Clone(), (double[])DataY.Clone());
return new Graph(Id, Mode, MinX, MaxX, Contracts.ConvertAll(item => new Tuple<string, string>(item.Item1, item.Item2)), (double[])DataX.Clone(), (double[])DataY.Clone());
}
}
}