153 lines
5.4 KiB
C#
153 lines
5.4 KiB
C#
using ScottPlot;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text.Json.Nodes;
|
|
|
|
namespace Elwig.Helpers.Billing {
|
|
public class Graph : ICloneable {
|
|
|
|
public double[] DataX { get; set; }
|
|
public double[] DataY { get; set; }
|
|
|
|
public Graph(int minX, int maxX) {
|
|
DataX = DataGen.Range(minX, maxX + 1);
|
|
DataY = DataGen.Zeros(maxX - minX + 1);
|
|
}
|
|
|
|
public Graph(Dictionary<double, decimal> data, int minX, int maxX) {
|
|
DataX = DataGen.Range(minX, maxX + 1);
|
|
DataY = DataGen.Zeros(maxX - minX + 1);
|
|
ParseGraphData(data, minX, maxX);
|
|
}
|
|
|
|
public Graph(double[] dataX, double[] dataY) {
|
|
DataX = dataX;
|
|
DataY = dataY;
|
|
}
|
|
|
|
public double GetOechsleAt(int index) {
|
|
return DataX[index];
|
|
}
|
|
|
|
public void SetOechsleAt(int index, double oechsle) {
|
|
DataX[index] = oechsle;
|
|
}
|
|
|
|
public void SetPriceAt(int index, double price) {
|
|
DataY[index] = price;
|
|
}
|
|
|
|
public double GetPriceAt(int index) {
|
|
return DataY[index];
|
|
}
|
|
|
|
private void ParseGraphData(Dictionary<double, decimal> graphPoints, int minX, int maxX) {
|
|
if (graphPoints.Keys.Count < 1) {
|
|
return;
|
|
}
|
|
|
|
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(maxX)) {
|
|
graphPoints.Add(maxX, graphPoints.GetValueOrDefault(maxKey));
|
|
}
|
|
|
|
var keys = graphPoints.Keys.Order().ToArray();
|
|
|
|
for (int i = 0; i < keys.Length; i++) {
|
|
decimal point1Value = graphPoints[keys[i]];
|
|
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++) {
|
|
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;
|
|
|
|
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++) {
|
|
DataY[j] = (double)point1Value;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void FlattenGraph(int begin, int end, double value) {
|
|
for (int i = begin; i <= end; i++) {
|
|
DataY[i] = value;
|
|
}
|
|
}
|
|
|
|
public void FlattenGraphLeft(int pointIndex) {
|
|
FlattenGraph(0, pointIndex, DataY[pointIndex]);
|
|
}
|
|
|
|
public void FlattenGraphRight(int pointIndex) {
|
|
FlattenGraph(pointIndex, DataY.Length - 1, DataY[pointIndex]);
|
|
}
|
|
|
|
private void LinearIncreaseGraph(int begin, int end, double inc) {
|
|
for (int i = begin; i < end; i++) {
|
|
DataY[i + 1] = Math.Round(DataY[i] + inc, 4); //TODO richtig runden
|
|
}
|
|
}
|
|
|
|
public void LinearIncreaseGraphToEnd(int begin, double inc) {
|
|
LinearIncreaseGraph(begin, DataY.Length - 1, inc);
|
|
}
|
|
|
|
public void InterpolateGraph(int firstPoint, int secondPoint) {
|
|
int steps = Math.Abs(firstPoint - secondPoint);
|
|
if (firstPoint == -1 || secondPoint == -1 || steps < 2) {
|
|
return;
|
|
}
|
|
var (lowIndex, highIndex) = firstPoint < secondPoint ? (firstPoint, secondPoint) : (secondPoint, firstPoint);
|
|
double step = (DataY[highIndex] - DataY[lowIndex]) / steps;
|
|
|
|
for (int i = lowIndex; i < highIndex - 1; i++) {
|
|
DataY[i + 1] = Math.Round(DataY[i] + step, 4); // TODO richtig runden
|
|
}
|
|
}
|
|
|
|
public JsonNode ToJson(string mode) {
|
|
if (DataY.Distinct().Count() == 1) {
|
|
return JsonValue.Create(DataY[0]);
|
|
}
|
|
|
|
var data = new JsonObject();
|
|
|
|
if (DataY[0] != DataY[1]) {
|
|
data.Add(new KeyValuePair<string, JsonNode?>(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<string, JsonNode?>(DataX[i] + mode, Math.Round(DataY[i], 4)));
|
|
}
|
|
}
|
|
if (DataY[^1] != DataY[^2]) {
|
|
data.Add(new KeyValuePair<string, JsonNode?>(DataX[^1] + mode, Math.Round(DataY[^1], 4)));
|
|
}
|
|
return data;
|
|
}
|
|
|
|
public object Clone() {
|
|
return new Graph((double[])DataX.Clone(), (double[])DataY.Clone());
|
|
}
|
|
}
|
|
}
|