Add GraphSettings

This commit is contained in:
Иван Ивашкин
2024-10-31 13:46:50 +05:00
parent 94387d0d0b
commit 96b0fbd6a3
12 changed files with 138 additions and 72 deletions

View File

@@ -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();
}

View File

@@ -213,21 +213,9 @@ namespace StructureHelper.Windows.MainGraph
}
private void DrawGraph()
{
var labels = new List<string>();
var lineSeries = new LineSeries();
var seriesCollection = new SeriesCollection();
var chartValues = new ChartValues<double>();
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();
}
}
}

View File

@@ -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();
}
}
}

View File

@@ -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();
}
}
}

View File

@@ -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<GraphPoint> Table { get; set; }
public double MinArg { get; set; }
public double MaxArg { get; set; }
public Color Color { get; set; }
public ObservableCollection<IOneVariableFunction> Functions { get; set; }
public bool Check();
public double GetByX(double xValue);
public SeriesCollection GetSeriesCollection();
public GraphSettings GetGraphSettings();
}
}

View File

@@ -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<GraphPoint>();
foreach (GraphPoint point in graphSettings.GraphPoints)
{
if (point.X > leftBound && point.X < rightBound)
{
graphLimitGraphPoint.Add(point);
}
}
graphSettings.GraphPoints = graphLimitGraphPoint;
return graphSettings;
}
}
}

View File

@@ -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<GraphPoint>();
foreach (GraphPoint point in graphSettings.GraphPoints)
{
if (point.Y > downBound && point.Y < upBound)
{
graphLimitGraphPoint.Add(point);
}
}
graphSettings.GraphPoints = graphLimitGraphPoint;
return graphSettings;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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<GraphPoint> table;
public List<GraphPoint> 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<GraphPoint> CalculateTable()
public GraphSettings GetGraphSettings()
{
var table = new List<GraphPoint>();
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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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<string> _labels = new List<string>();
private SeriesCollection _seriesCollection = new SeriesCollection();
private LineSeries _lineSeries { get; set; } = new LineSeries();
private ChartValues<double> _chartValues { get; set; } = new ChartValues<double>();
public List<GraphPoint> GraphPoints { get; set; } = new List<GraphPoint>();
public GraphSettings(Color strokeColor, Color fillColor)
{
_strokeColor = strokeColor;
_fillColor = fillColor;
}
public List<string> 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;
}
}
}