diff --git a/Elwig/Helpers/Billing/Graph.cs b/Elwig/Helpers/Billing/Graph.cs index 66095cf..da4f76b 100644 --- a/Elwig/Helpers/Billing/Graph.cs +++ b/Elwig/Helpers/Billing/Graph.cs @@ -3,52 +3,31 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text.Json.Nodes; +using System.Windows.Documents; namespace Elwig.Helpers.Billing { public class Graph : ICloneable { - public BillingData.CurveMode Mode { get; set; } - public int Id { get; set; } - private int MinX { get; set; } - private int MaxX { get; set; } - public List> Contracts { get; set; } public double[] DataX { get; set; } public double[] DataY { get; set; } - public Graph(int id, BillingData.CurveMode mode, int minX, int maxX) { - Id = id; - Mode = mode; - Contracts = new List>(); - MinX = minX; - MaxX = maxX; - - DataX = DataGen.Range(MinX, MaxX + 1); - DataY = DataGen.Zeros(MaxX - MinX + 1); + public Graph(int minX, int maxX) { + DataX = DataGen.Range(minX, maxX + 1); + DataY = DataGen.Zeros(maxX - minX + 1); } - public Graph(int id, BillingData.CurveMode mode, Dictionary data, int minX, int maxX) { - Id = id; - Mode = mode; - Contracts = new List>(); - MinX = minX; - MaxX = maxX; - - DataX = DataGen.Range(MinX, MaxX + 1); - DataY = DataGen.Zeros(MaxX - MinX + 1); - ParseGraphData(data); + public Graph(Dictionary data, int minX, int maxX) { + DataX = DataGen.Range(minX, maxX + 1); + DataY = DataGen.Zeros(maxX - minX + 1); + ParseGraphData(data, minX, maxX); } - public Graph(int id, BillingData.CurveMode mode, int minX, int maxX, List> contracts, double[] dataX, double[] dataY) { - Id = id; - Mode = mode; - MinX = minX; - MaxX = maxX; - Contracts = contracts; + public Graph(double[] dataX, double[] dataY) { DataX = dataX; DataY = dataY; } - private void ParseGraphData(Dictionary graphPoints) { + private void ParseGraphData(Dictionary graphPoints, int minX, int maxX) { if (graphPoints.Keys.Count < 1) { return; } @@ -56,11 +35,11 @@ namespace Elwig.Helpers.Billing { 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(); @@ -70,56 +49,60 @@ namespace Elwig.Helpers.Billing { if (i + 1 < keys.Length) { decimal point2Value = graphPoints[keys[i + 1]]; if (point1Value == point2Value) { - for (int j = (int)(keys[i] - MinX); j < keys[i + 1] - MinX; j++) { + for (int j = (int)(keys[i] - minX); j < keys[i + 1] - minX; j++) { DataY[j] = (double)point1Value; } } else { int steps = (int)Math.Abs(keys[i + 1] - keys[i]); decimal step = (point2Value - point1Value) / steps; - DataY[(int)(keys[i] - MinX)] = (double)point1Value; - DataY[(int)(keys[i + 1] - MinX)] = (double)point2Value; + DataY[(int)(keys[i] - minX)] = (double)point1Value; + DataY[(int)(keys[i + 1] - minX)] = (double)point2Value; - for (int j = (int)(keys[i] - MinX); j < keys[i + 1] - MinX - 1; j++) { + 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 = (int)(keys[i] - MinX); j < DataX.Length; j++) { + for (int j = (int)(keys[i] - minX); j < DataX.Length; j++) { DataY[j] = (double)point1Value; } } } } - public JsonObject ToJson() { - var graph = new JsonObject { - ["id"] = Id, - ["mode"] = Mode.ToString().ToLower(), - }; + public void FlattenGraph(int begin, int end, double value) { + for (int i = begin; i <= end; i++) { + DataY[i] = value; + } + } + public void LinearIncreaseGraph(int begin, int end, double inc) { + for (int i = begin; i < end; i++) { + DataY[i + 1] = DataY[i] + inc; + } + } + + public JsonObject ToJson(string mode) { var data = new JsonObject(); if (DataY[0] != DataY[1]) { - data.Add(new KeyValuePair(DataX[0] + Mode.ToString().ToLower(), Math.Round(DataY[0], 4))); + data.Add(new KeyValuePair(DataX[0] + mode, Math.Round(DataY[0], 4))); } for (int i = 1; i < DataX.Length - 1; i++) { if (Math.Round(DataY[i] - DataY[i - 1], 10) != Math.Round(DataY[i + 1] - DataY[i], 10)) { - data.Add(new KeyValuePair(DataX[i] + Mode.ToString().ToLower(), Math.Round(DataY[i], 4))); + data.Add(new KeyValuePair(DataX[i] + mode, Math.Round(DataY[i], 4))); } } if (DataY[^1] != DataY[^2]) { - data.Add(new KeyValuePair(DataX[^1] + Mode.ToString().ToLower(), Math.Round(DataY[^1], 4))); + data.Add(new KeyValuePair(DataX[^1] + mode, Math.Round(DataY[^1], 4))); } - - graph["data"] = data; - - return graph; + return data; } public object Clone() { - return new Graph(Id, Mode, MinX, MaxX, Contracts.ConvertAll(item => new Tuple(item.Item1, item.Item2)), (double[])DataX.Clone(), (double[])DataY.Clone()); + return new Graph((double[])DataX.Clone(), (double[])DataY.Clone()); } } } diff --git a/Elwig/Helpers/Billing/GraphEntry.cs b/Elwig/Helpers/Billing/GraphEntry.cs new file mode 100644 index 0000000..d9ff337 --- /dev/null +++ b/Elwig/Helpers/Billing/GraphEntry.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Nodes; +using System.Threading.Tasks; + +namespace Elwig.Helpers.Billing { + public class GraphEntry { + + public int Id { get; set; } + public BillingData.CurveMode Mode { get; set; } + public Graph DataGraph { get; set; } + public Graph? GebundenGraph { get; set; } + public decimal? GebundenFlatPrice { get; set; } + public List Contracts { get; set; } + private int MinX { get; set; } + private int MaxX { get; set; } + + public GraphEntry(int id, BillingData.CurveMode mode, int minX, int maxX) { + Id = id; + Mode = mode; + MinX = minX; + MaxX = maxX; + DataGraph = new Graph(minX, maxX); + Contracts = []; + } + + public GraphEntry(int id, BillingData.CurveMode mode, Dictionary data, int minX, int maxX) : this(id, mode, minX, maxX) { + DataGraph = new Graph(data, minX, maxX); + } + + private GraphEntry(int id, BillingData.CurveMode mode, Graph dataGraph, Graph? gebundenGraph, + decimal? gebundenFlatPrice, List contracts, int minX, int maxX) { + Id = id; + Mode = mode; + MinX = minX; + MaxX = maxX; + DataGraph = dataGraph; + GebundenGraph = gebundenGraph; + GebundenFlatPrice = gebundenFlatPrice; + Contracts = contracts; + } + + public JsonObject ToJson() { + var curve = new JsonObject { + ["id"] = Id, + ["mode"] = Mode.ToString().ToLower(), + }; + + curve["data"] = DataGraph.ToJson(Mode.ToString().ToLower()); + + if (GebundenFlatPrice != null) { + curve["geb"] = GebundenFlatPrice.ToString(); + } else if (GebundenGraph != null) { + curve["geb"] = GebundenGraph.ToJson(Mode.ToString().ToLower()); + } + + return curve; + } + + public GraphEntry Copy(int id) { + return new GraphEntry(id, Mode, (Graph)DataGraph.Clone(), (Graph?)GebundenGraph?.Clone(), GebundenFlatPrice, Contracts, MinX, MaxX); + } + } +} diff --git a/Elwig/Windows/ChartWindow.xaml b/Elwig/Windows/ChartWindow.xaml index 707fa29..a5eb296 100644 --- a/Elwig/Windows/ChartWindow.xaml +++ b/Elwig/Windows/ChartWindow.xaml @@ -1,4 +1,4 @@ - @@ -43,94 +43,77 @@ - + - + - - - - - + - - - + + - - - - - - - - - - - - -