From 96b0fbd6a30569bb60b781ca65db0310df29d6a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD=20=D0=98=D0=B2=D0=B0=D1=88=D0=BA?= =?UTF-8?q?=D0=B8=D0=BD?= Date: Thu, 31 Oct 2024 13:46:50 +0500 Subject: [PATCH] Add GraphSettings --- .../Windows/MainGraph/FormulaViewModel.cs | 12 ++++- .../Windows/MainGraph/GraphViewModel.cs | 18 ++----- .../Windows/TreeGraph/TreeGraphViewModel.cs | 14 ++---- .../Interfaces/FunctionDecorator.cs | 5 +- .../Interfaces/IOneVariableFunction.cs | 4 +- .../Functions/Decorator/LimXDecorator.cs | 15 +++++- .../Functions/Decorator/LimYDecorator.cs | 29 +++++++---- .../Functions/Decorator/ScaleXDecorator.cs | 10 +++- .../Functions/Decorator/ScaleYDecorator.cs | 10 +++- .../Models/Functions/FormulaFunction.cs | 28 +++-------- .../Models/Functions/TableFunction.cs | 16 ++++-- .../Services/GraphSettings.cs | 49 +++++++++++++++++++ 12 files changed, 138 insertions(+), 72 deletions(-) create mode 100644 StructureHelperCommon/Services/GraphSettings.cs diff --git a/StructureHelper/Windows/MainGraph/FormulaViewModel.cs b/StructureHelper/Windows/MainGraph/FormulaViewModel.cs index 310ab8a..ffbff48 100644 --- a/StructureHelper/Windows/MainGraph/FormulaViewModel.cs +++ b/StructureHelper/Windows/MainGraph/FormulaViewModel.cs @@ -18,12 +18,14 @@ namespace StructureHelper.Windows.MainGraph public class FormulaViewModel : ViewModelBase { private const string ERROR_BOUNDS = "The left bound must be less than the right bound"; + private const string ERROR_STEP = "The number of steps should not be more than"; private const string DEFAULT_NAME = "Put function name here..."; private const string DEFAULT_DESCRIPTION = "Put function description here..."; private const string DEFAULT_FORMULA = "x^2"; - private const double DEFAULT_LEFT_BOUND = 0; - private const double DEFAULT_RIGHT_BOUND = 1000; + private const double DEFAULT_LEFT_BOUND = -500; + private const double DEFAULT_RIGHT_BOUND = 500; private const int DEFAULT_STEP = 100; + private const int MAX_STEP = 1000; public char GREATER { get; } = '\u2265'; public char LESS { get; } = '\u2264'; public char X { get; } = 'x'; @@ -145,6 +147,7 @@ namespace StructureHelper.Windows.MainGraph Function.Name = Name; Function.Description = Description; Function.IsUser = true; + (Function as FormulaFunction).Step = Step; (Function as FormulaFunction).Formula = Formula; var window = parameter as Window; if (LeftBound > RightBound) @@ -152,6 +155,11 @@ namespace StructureHelper.Windows.MainGraph MessageBox.Show($"{ERROR_BOUNDS}"); return; } + if (Step > MAX_STEP) + { + MessageBox.Show($"{ERROR_STEP} {MAX_STEP}"); + return; + } window.DialogResult = true; window.Close(); } diff --git a/StructureHelper/Windows/MainGraph/GraphViewModel.cs b/StructureHelper/Windows/MainGraph/GraphViewModel.cs index 61dabf3..4724035 100644 --- a/StructureHelper/Windows/MainGraph/GraphViewModel.cs +++ b/StructureHelper/Windows/MainGraph/GraphViewModel.cs @@ -213,21 +213,9 @@ namespace StructureHelper.Windows.MainGraph } private void DrawGraph() { - var labels = new List(); - var lineSeries = new LineSeries(); - var seriesCollection = new SeriesCollection(); - var chartValues = new ChartValues(); - foreach (GraphPoint graphPoint in SelectedFuntion.Table) - { - labels.Add(Math.Round(graphPoint.X, 2).ToString()); - chartValues.Add(Math.Round(graphPoint.Y)); - } - lineSeries.Values = chartValues; - lineSeries.Stroke = Brushes.Blue; - lineSeries.Fill = Brushes.Transparent; - Labels = labels; - seriesCollection.Add(lineSeries); - SeriesCollection = seriesCollection; + var graphSettings = SelectedFuntion.GetGraphSettings(); + Labels = graphSettings.GetLabels(); + SeriesCollection = graphSettings.GetSeriesCollection(); } } } diff --git a/StructureHelper/Windows/TreeGraph/TreeGraphViewModel.cs b/StructureHelper/Windows/TreeGraph/TreeGraphViewModel.cs index 08e4055..e5a23bc 100644 --- a/StructureHelper/Windows/TreeGraph/TreeGraphViewModel.cs +++ b/StructureHelper/Windows/TreeGraph/TreeGraphViewModel.cs @@ -213,17 +213,9 @@ namespace StructureHelper.Windows.TreeGraph return; } SelectedFuntion = selectedTreeViewItem.Function; - foreach (GraphPoint graphPoint in SelectedFuntion.Table) - { - labels.Add(Math.Round(graphPoint.X, 2).ToString()); - chartValues.Add(Math.Round(graphPoint.Y)); - } - lineSeries.Values = chartValues; - lineSeries.Stroke = Brushes.Blue; - lineSeries.Fill = Brushes.Transparent; - Labels = labels; - seriesCollection.Add(lineSeries); - SeriesCollection = seriesCollection; + var graphSettings = SelectedFuntion.GetGraphSettings(); + Labels = graphSettings.GetLabels(); + SeriesCollection = graphSettings.GetSeriesCollection(); } } } diff --git a/StructureHelperCommon/Infrastructures/Interfaces/FunctionDecorator.cs b/StructureHelperCommon/Infrastructures/Interfaces/FunctionDecorator.cs index 85f6f68..5f8d62d 100644 --- a/StructureHelperCommon/Infrastructures/Interfaces/FunctionDecorator.cs +++ b/StructureHelperCommon/Infrastructures/Interfaces/FunctionDecorator.cs @@ -9,6 +9,7 @@ using System.Windows.Media; using System.Linq; using System.Text; using System.Threading.Tasks; +using StructureHelperCommon.Services; namespace StructureHelperCommon.Infrastructures.Interfaces { @@ -46,9 +47,9 @@ namespace StructureHelperCommon.Infrastructures.Interfaces { return function.GetByX(xValue); } - public virtual SeriesCollection GetSeriesCollection() + public virtual GraphSettings GetGraphSettings() { - return function.GetSeriesCollection(); + return function.GetGraphSettings(); } } } diff --git a/StructureHelperCommon/Infrastructures/Interfaces/IOneVariableFunction.cs b/StructureHelperCommon/Infrastructures/Interfaces/IOneVariableFunction.cs index 62fcb4d..e83713e 100644 --- a/StructureHelperCommon/Infrastructures/Interfaces/IOneVariableFunction.cs +++ b/StructureHelperCommon/Infrastructures/Interfaces/IOneVariableFunction.cs @@ -1,6 +1,7 @@ using LiveCharts; using StructureHelperCommon.Infrastructures.Enums; using StructureHelperCommon.Models.Functions; +using StructureHelperCommon.Services; using System; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -21,13 +22,12 @@ namespace StructureHelperCommon.Infrastructures.Interfaces public FunctionType Type { get; set; } public string Name { get; set; } public string Description { get; set; } - public List Table { get; set; } public double MinArg { get; set; } public double MaxArg { get; set; } public Color Color { get; set; } public ObservableCollection Functions { get; set; } public bool Check(); public double GetByX(double xValue); - public SeriesCollection GetSeriesCollection(); + public GraphSettings GetGraphSettings(); } } diff --git a/StructureHelperCommon/Models/Functions/Decorator/LimXDecorator.cs b/StructureHelperCommon/Models/Functions/Decorator/LimXDecorator.cs index 1723aae..794e083 100644 --- a/StructureHelperCommon/Models/Functions/Decorator/LimXDecorator.cs +++ b/StructureHelperCommon/Models/Functions/Decorator/LimXDecorator.cs @@ -1,5 +1,6 @@ using LiveCharts; using StructureHelperCommon.Infrastructures.Interfaces; +using StructureHelperCommon.Services; using System; using System.Collections.Generic; using System.Linq; @@ -30,9 +31,19 @@ namespace StructureHelperCommon.Models.Functions.Decorator } return 0; } - public override SeriesCollection GetSeriesCollection() + public override GraphSettings GetGraphSettings() { - return base.GetSeriesCollection(); + var graphSettings = base.GetGraphSettings(); + var graphLimitGraphPoint = new List(); + foreach (GraphPoint point in graphSettings.GraphPoints) + { + if (point.X > leftBound && point.X < rightBound) + { + graphLimitGraphPoint.Add(point); + } + } + graphSettings.GraphPoints = graphLimitGraphPoint; + return graphSettings; } } } diff --git a/StructureHelperCommon/Models/Functions/Decorator/LimYDecorator.cs b/StructureHelperCommon/Models/Functions/Decorator/LimYDecorator.cs index 2c27901..22e15d2 100644 --- a/StructureHelperCommon/Models/Functions/Decorator/LimYDecorator.cs +++ b/StructureHelperCommon/Models/Functions/Decorator/LimYDecorator.cs @@ -1,5 +1,6 @@ using LiveCharts; using StructureHelperCommon.Infrastructures.Interfaces; +using StructureHelperCommon.Services; using System; using System.Collections.Generic; using System.Linq; @@ -10,13 +11,13 @@ namespace StructureHelperCommon.Models.Functions.Decorator { public class LimYDecorator : FunctionDecorator { - private double leftBound; - private double rightBound; - public LimYDecorator(IOneVariableFunction function, double leftBound, double rightBound) : base(function) + private double downBound; + private double upBound; + public LimYDecorator(IOneVariableFunction function, double downBound, double upBound) : base(function) { - Name = $"y\u2208[{leftBound};{rightBound}]"; - this.leftBound = leftBound; - this.rightBound = rightBound; + Name = $"y\u2208[{downBound};{upBound}]"; + this.downBound = downBound; + this.upBound = upBound; } public override bool Check() { @@ -25,15 +26,25 @@ namespace StructureHelperCommon.Models.Functions.Decorator public override double GetByX(double xValue) { var y = base.GetByX(xValue); - if (y > leftBound && y < rightBound) + if (y > downBound && y < upBound) { return y; } return 0; } - public override SeriesCollection GetSeriesCollection() + public override GraphSettings GetGraphSettings() { - return base.GetSeriesCollection(); + var graphSettings = base.GetGraphSettings(); + var graphLimitGraphPoint = new List(); + foreach (GraphPoint point in graphSettings.GraphPoints) + { + if (point.Y > downBound && point.Y < upBound) + { + graphLimitGraphPoint.Add(point); + } + } + graphSettings.GraphPoints = graphLimitGraphPoint; + return graphSettings; } } } diff --git a/StructureHelperCommon/Models/Functions/Decorator/ScaleXDecorator.cs b/StructureHelperCommon/Models/Functions/Decorator/ScaleXDecorator.cs index b422358..169001e 100644 --- a/StructureHelperCommon/Models/Functions/Decorator/ScaleXDecorator.cs +++ b/StructureHelperCommon/Models/Functions/Decorator/ScaleXDecorator.cs @@ -1,5 +1,6 @@ using LiveCharts; using StructureHelperCommon.Infrastructures.Interfaces; +using StructureHelperCommon.Services; using System; using System.Collections.Generic; using System.Linq; @@ -24,9 +25,14 @@ namespace StructureHelperCommon.Models.Functions.Decorator { return base.GetByX(factor * xValue); } - public override SeriesCollection GetSeriesCollection() + public override GraphSettings GetGraphSettings() { - return base.GetSeriesCollection(); + var graphSettings = base.GetGraphSettings(); + foreach(GraphPoint point in graphSettings.GraphPoints) + { + point.Y = GetByX(point.X); + } + return graphSettings; } } } diff --git a/StructureHelperCommon/Models/Functions/Decorator/ScaleYDecorator.cs b/StructureHelperCommon/Models/Functions/Decorator/ScaleYDecorator.cs index 7634ed9..c885997 100644 --- a/StructureHelperCommon/Models/Functions/Decorator/ScaleYDecorator.cs +++ b/StructureHelperCommon/Models/Functions/Decorator/ScaleYDecorator.cs @@ -1,5 +1,6 @@ using LiveCharts; using StructureHelperCommon.Infrastructures.Interfaces; +using StructureHelperCommon.Services; using System; using System.Collections.Generic; using System.Linq; @@ -24,9 +25,14 @@ namespace StructureHelperCommon.Models.Functions.Decorator { return factor * base.GetByX(xValue); } - public override SeriesCollection GetSeriesCollection() + public override GraphSettings GetGraphSettings() { - return base.GetSeriesCollection(); + var graphSettings = base.GetGraphSettings(); + foreach (GraphPoint point in graphSettings.GraphPoints) + { + point.Y = GetByX(point.X); + } + return graphSettings; } } } diff --git a/StructureHelperCommon/Models/Functions/FormulaFunction.cs b/StructureHelperCommon/Models/Functions/FormulaFunction.cs index 37c140d..bb08994 100644 --- a/StructureHelperCommon/Models/Functions/FormulaFunction.cs +++ b/StructureHelperCommon/Models/Functions/FormulaFunction.cs @@ -10,6 +10,8 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Documents; +using StructureHelperCommon.Services; +using LiveCharts.Wpf; namespace StructureHelperCommon.Models.Functions { @@ -24,18 +26,6 @@ namespace StructureHelperCommon.Models.Functions public string Name { get; set; } public string Description { get ; set; } public int Step { get; set; } - private List table; - public List Table - { - get - { - return CalculateTable(); - } - set - { - table = value; - } - } public string Formula { get; set; } public Guid Id => throw new NotImplementedException(); @@ -86,23 +76,19 @@ namespace StructureHelperCommon.Models.Functions public double GetByX(double xValue) { double yValue = 0; - yValue = Math.Round(Math.Pow(xValue, 2), 2); //Временное тестовой выражение квадратичной параболы, будет разбор выражения + yValue = Math.Round(Math.Pow(xValue, 2), 2); //Временно выражение квадратичной параболы, будет разбор выражения return yValue; } - private List CalculateTable() + public GraphSettings GetGraphSettings() { - var table = new List(); + var graphSettings = new GraphSettings(Color, Color); var stepLenght = Math.Abs(MaxArg - MinArg) / Step; for (double x = MinArg; x < MaxArg; x += stepLenght) { var graphPoint = new GraphPoint(x, GetByX(x)); - table.Add(graphPoint); + graphSettings.GraphPoints.Add(graphPoint); } - return table; - } - public SeriesCollection GetSeriesCollection() - { - throw new NotImplementedException(); + return graphSettings; } } } diff --git a/StructureHelperCommon/Models/Functions/TableFunction.cs b/StructureHelperCommon/Models/Functions/TableFunction.cs index 2068793..d01b10b 100644 --- a/StructureHelperCommon/Models/Functions/TableFunction.cs +++ b/StructureHelperCommon/Models/Functions/TableFunction.cs @@ -9,6 +9,7 @@ using System.Windows.Media; using System.Linq; using System.Text; using System.Threading.Tasks; +using StructureHelperCommon.Services; namespace StructureHelperCommon.Models.Functions { @@ -57,7 +58,6 @@ namespace StructureHelperCommon.Models.Functions public object Clone() { var tableFunction = new TableFunction(); - //Здесь будет стратегия tableFunction.Type = Type; tableFunction.Name = $"{Name} {COPY}"; @@ -74,12 +74,20 @@ namespace StructureHelperCommon.Models.Functions { //Реализовать взятие значения из таблицы и интерполяцию по таблице - return 0; + + + return 100; } - public SeriesCollection GetSeriesCollection() + public GraphSettings GetGraphSettings() { - throw new NotImplementedException(); + var graphSettings = new GraphSettings(Color, Color); + foreach(GraphPoint point in Table) + { + var graphPoint = new GraphPoint(point.X, GetByX(point.X)); + graphSettings.GraphPoints.Add(graphPoint); + } + return graphSettings; } } } diff --git a/StructureHelperCommon/Services/GraphSettings.cs b/StructureHelperCommon/Services/GraphSettings.cs new file mode 100644 index 0000000..abbd8fd --- /dev/null +++ b/StructureHelperCommon/Services/GraphSettings.cs @@ -0,0 +1,49 @@ +using LiveCharts; +using LiveCharts.Wpf; +using StructureHelperCommon.Models.Functions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Media; +using System.Windows.Shapes; + +namespace StructureHelperCommon.Services +{ + public class GraphSettings + { + private Color _strokeColor; + private Color _fillColor; + private List _labels = new List(); + private SeriesCollection _seriesCollection = new SeriesCollection(); + private LineSeries _lineSeries { get; set; } = new LineSeries(); + private ChartValues _chartValues { get; set; } = new ChartValues(); + public List GraphPoints { get; set; } = new List(); + public GraphSettings(Color strokeColor, Color fillColor) + { + _strokeColor = strokeColor; + _fillColor = fillColor; + } + public List GetLabels() + { + foreach (GraphPoint point in GraphPoints) + { + _labels.Add(Math.Round(point.X, 2).ToString()); + } + return _labels; + } + public SeriesCollection GetSeriesCollection() + { + foreach (GraphPoint point in GraphPoints) + { + _chartValues.Add(Math.Round(point.Y, 2)); + } + _lineSeries.Values = _chartValues; + _lineSeries.Stroke = Brushes.Blue; //Заменить на поле Color + _lineSeries.Fill = Brushes.Transparent; //Заменить на поле Color + _seriesCollection.Add(_lineSeries); + return _seriesCollection; + } + } +}