Add tree graph commands

This commit is contained in:
Иван Ивашкин
2024-10-19 21:08:13 +05:00
parent 88ac95af2f
commit 11ac7c7c48
17 changed files with 366 additions and 112 deletions

View File

@@ -1,4 +1,5 @@
<Window x:Class="StructureHelper.Windows.MainGraph.FormulaView"
x:Name="FormulaFunction_win"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
@@ -6,9 +7,7 @@
xmlns:local="clr-namespace:StructureHelper.Windows.MainGraph"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance local:FormulaViewModel}"
Title="TableFunction" Height="300" Width="400"
MaxWidth="400"
MinWidth="400">
Title="FormulaFunction" Height="320" Width="400">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
@@ -31,25 +30,21 @@
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="y(x)="
FontStyle="Italic"
<TextBlock Grid.Column="0" Text="y(x) ="
Margin="5"
VerticalAlignment="Center"
HorizontalAlignment="Right"/>
<TextBox Grid.Column="1" Text="{Binding Description}"
<TextBox Grid.Column="1" Text="{Binding Formula, UpdateSourceTrigger=PropertyChanged}"
Margin="5"/>
</Grid>
</Grid>
<TextBlock Grid.Row="1"
Margin="5"
Background="LightYellow"
Text="y(x) = формула"
Text="{Binding FormulaText, UpdateSourceTrigger=PropertyChanged}"
TextAlignment="Center"
FontSize="20"
TextWrapping="Wrap"
FontStyle="Italic">
TextWrapping="Wrap">
</TextBlock>
<Grid Grid.Row="2">
<Grid.RowDefinitions>
@@ -75,7 +70,7 @@
</Grid>
<Button Grid.Row="4" Margin="5" Content="Save"
Command="{Binding DrawGraphCommand}"
CommandParameter="{Binding ElementName=TableFunction_win}">
CommandParameter="{Binding ElementName=FormulaFunction_win}">
</Button>
</Grid>
</Window>

View File

@@ -1,15 +1,52 @@
using StructureHelperCommon.Infrastructures.Interfaces;
using StructureHelper.Infrastructure;
using StructureHelperCommon.Infrastructures.Interfaces;
using StructureHelperCommon.Models.Functions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
namespace StructureHelper.Windows.MainGraph
{
public class FormulaViewModel
public class FormulaViewModel : ViewModelBase
{
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 int DEFAULT_STEP = 100;
private RelayCommand drawGraphCommand;
public ICommand DrawGraphCommand
{
get => drawGraphCommand ??= new RelayCommand(o => Save(o));
}
private string formula;
public string Formula
{
get => formula;
set
{
formula = value;
}
}
private string formulaText = "y(x)=";
public string FormulaText
{
get => formulaText;
set
{
formulaText = $"y(x)={Formula}";
OnPropertyChanged(nameof(Formula));
}
}
private IOneVariableFunction function;
public IOneVariableFunction Function
{
@@ -19,13 +56,49 @@ namespace StructureHelper.Windows.MainGraph
function = value;
}
}
public FormulaViewModel()
private string name;
public string Name
{
get => name;
set
{
name = value;
}
}
public FormulaViewModel(FormulaFunction function)
private string description;
public string Description
{
get => description;
set
{
description = value;
}
}
public FormulaViewModel()
{
Name = DEFAULT_NAME;
Description = DEFAULT_DESCRIPTION;
}
public FormulaViewModel(FormulaFunction formulaFunction)
{
Function = formulaFunction;
Name = Function.Name;
Description = Function.Description;
}
private void Save(object parameter)
{
if (Function is null)
{
Function = new FormulaFunction();
}
Function.Name = Name;
Function.Description = Description;
Function.IsUser = true;
(Function as FormulaFunction).Formula = Formula;
var window = parameter as Window;
window.DialogResult = true;
window.Close();
}
}
}

View File

@@ -18,15 +18,44 @@
<RowDefinition Height="*"/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<ListBox Grid.Row="0" Grid.Column="0" Margin="5"
<ListView Grid.Row="0" Grid.Column="0" Margin="5"
ItemsSource="{Binding Functions, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedFuntion, UpdateSourceTrigger=PropertyChanged}">
<ListBox.ItemTemplate>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander IsExpanded="True">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" />
<TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Green" FontWeight="Bold" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" />
<TextBlock Text=" item(s)" FontSize="22" Foreground="Silver" FontStyle="Italic" VerticalAlignment="Bottom" />
</StackPanel>
</Expander.Header>
<ItemsPresenter/>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ListView.ItemTemplate>
</ListView>
<Grid Grid.Row="1" Grid.Column="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>

View File

@@ -6,6 +6,7 @@ using StructureHelperCommon.Infrastructures.Enums;
using StructureHelperCommon.Infrastructures.Interfaces;
using StructureHelperCommon.Models.Functions;
using StructureHelperLogics.Models.Graphs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Input;
@@ -47,6 +48,7 @@ namespace StructureHelper.Windows.MainGraph
private RelayCommand deleteCommand;
private RelayCommand copyCommand;
private RelayCommand treeCommand;
private RelayCommand drawGraphCommand;
public ICommand AddTableCommand
{
get => addTableCommand ??= new RelayCommand(o => AddTable());
@@ -71,47 +73,47 @@ namespace StructureHelper.Windows.MainGraph
{
get => treeCommand ??= new RelayCommand(o => Tree());
}
public ICommand DrawGraphCommand
{
get => drawGraphCommand ??= new RelayCommand(o => DrawGraph());
}
public GraphViewModel()
{
Functions = new ObservableCollection<IOneVariableFunction>();
var f1 = new TableFunction();
f1.Name = "Пробная табличная системная функция 1";
f1.Table = new List<GraphPoint>();
f1.Name = "Табличная системная функция";
f1.Table = new List<GraphPoint>()
{
new GraphPoint(1, 0),
new GraphPoint(0, 1),
};
f1.IsUser = false;
f1.Description = "Описание 1";
var f2 = new TableFunction();
f1.Description = "Описание табличной системной функции";
var f4 = new TableFunction();
f4.Name = "Табличная системная функция";
f4.Table = new List<GraphPoint>()
{
new GraphPoint(1, 0),
new GraphPoint(0, 1),
};
f4.IsUser = false;
f4.Description = "Описание табличной системной функции";
/*var f2 = new TableFunction();
f2.Name = "Пробная табличная пользовательская функция 2";
f2.Table = new List<GraphPoint>();
f2.IsUser = true;
f2.Description = "Описание 2";
f2.Description = "Описание 2";*/
var f3 = new FormulaFunction();
f3.Name = "Пробная формульная системная функция 3";
f3.Name = "Формульная системная функция";
f3.Formula = "x^2";
f3.IsUser = false;
f3.Description = "Описание 3";
f3.Description = "Описание формульной системной функции";
Functions.Add(f1);
Functions.Add(f2);
//Functions.Add(f2);
Functions.Add(f3);
Labels = new List<string>();
Labels.Add("1");
Labels.Add("2");
Labels.Add("2");
Labels.Add("3");
var chartValues = new ChartValues<double>();
chartValues.Add(1);
chartValues.Add(10);
chartValues.Add(100);
chartValues.Add(25);
chartValues.Add(150);
chartValues.Add(100);
chartValues.Add(200);
chartValues.Add(50);
var lineSeries = new LineSeries();
lineSeries.Values = chartValues;
SeriesCollection = new SeriesCollection();
SeriesCollection.Add(lineSeries);
Functions.Add(f4);
}
/*public GraphViewModel(IGraph graph)
{
@@ -148,8 +150,6 @@ namespace StructureHelper.Windows.MainGraph
var tableView = new TableView();
tableView.DataContext = tableViewModel;
tableView.ShowDialog();
//SelectedFunction.Name = tableViewModel.Function.Name; //!!!!!!!!!!
//SelectedFunction.Description = tableViewModel.Function.Description; //!!!!!!!!!!
}
else if (SelectedFuntion.Type == FunctionType.FormulaFunction)
{
@@ -157,13 +157,23 @@ namespace StructureHelper.Windows.MainGraph
var formulaView = new FormulaView();
formulaView.DataContext = formulaViewModel;
formulaView.ShowDialog();
//SelectedFunction.Name = formulaViewModel.Function.Name; //!!!!!!!!!!
//SelectedFunction.Description = formulaViewModel.Function.Description; //!!!!!!!!!!
}
}
private void Delete()
{
Functions.Remove(SelectedFuntion);
if (SelectedFuntion is null)
{
var lastFunction = Functions[Functions.Count - 1];
if (lastFunction.IsUser)
{
Functions.Remove(lastFunction);
}
}
else
{
Functions.Remove(SelectedFuntion);
}
}
private void Copy()
{
@@ -187,5 +197,20 @@ namespace StructureHelper.Windows.MainGraph
treeGraph.DataContext = treeGraphVM;
treeGraph.ShowDialog();
}
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;
Labels = labels;
SeriesCollection = seriesCollection;
}
}
}

View File

@@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
@@ -22,7 +23,7 @@ namespace StructureHelper.Windows.MainGraph
private RelayCommand deletePointCommand;
public ICommand DrawGraphCommand
{
get => drawGraphCommand ??= new RelayCommand(o => DrawGraph(o));
get => drawGraphCommand ??= new RelayCommand(o => Save(o));
}
public ICommand AddPointCommand
{
@@ -84,7 +85,8 @@ namespace StructureHelper.Windows.MainGraph
{
Table = new ObservableCollection<GraphPoint>()
{
new GraphPoint(),
new GraphPoint(0, 0),
new GraphPoint(0, 0),
};
Name = DEFAULT_NAME;
Description = DEFAULT_DESCRIPTION;
@@ -96,7 +98,7 @@ namespace StructureHelper.Windows.MainGraph
Name = Function.Name;
Description = Function.Description;
}
private void DrawGraph(object parameter)
private void Save(object parameter)
{
if (Function is null)
{
@@ -112,12 +114,31 @@ namespace StructureHelper.Windows.MainGraph
}
private void AddPoint()
{
var point = new GraphPoint();
Table.Add(point);
var point = new GraphPoint(0, 0);
if (SelectedPoint is null)
{
Table.Add(point);
}
else
{
var selectedPointIndex = Table.IndexOf(SelectedPoint);
Table.Insert(selectedPointIndex + 1, point);
}
}
private void DeletePoint()
{
Table.Remove(SelectedPoint);
if (Table.Count < 3)
{
return;
}
if (SelectedPoint is null)
{
Table.RemoveAt(Table.Count - 1);
}
else
{
Table.Remove(SelectedPoint);
}
}
}
}