using System;
using Systeusing RealTimeGraphEx.Components.ComponentsItems;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace RealTimeGraphEx.Components
{
public class YAxisTicks : ComponentBase
{
private Grid innerGrid;
#region Constructors
public YAxisTicks()
{
Width = 4;
Location = ComponentLocationEnum.Left;
this.SizeChanged += (x, y) => { Render(Graph); };
}
#endregion
#region Properties
public int SmallTicks
{
get { return (int)GetValue(SmallTicksProperty); }
set { SetValue(SmallTicksProperty, value); }
}
public static readonly DependencyProperty SmallTicksProperty =
DependencyProperty.Register("SmallTicks", typeof(int), typeof(YAxisTicks), new PropertyMetadata(20));
public int BigTicks
{
get { return (int)GetValue(BigTicksProperty); }
set { SetValue(BigTicksProperty, value); }
}
public static readonly DependencyProperty BigTicksProperty =
DependencyProperty.Register("BigTicks", typeof(int), typeof(YAxisTicks), new PropertyMetadata(2));
public DataTemplate SmallTickTemplate
{
get { return (DataTemplate)GetValue(SmallTickTemplateProperty); }
set { SetValue(SmallTickTemplateProperty, value); }
}
public static readonly DependencyProperty SmallTickTemplateProperty =
DependencyProperty.Register("SmallTickTemplate", typeof(DataTemplate), typeof(YAxisTicks), new PropertyMetadata(null));
public DataTemplate BigTickTemplate
{
get { return (DataTemplate)GetValue(BigTickTemplateProperty); }
set { SetValue(BigTickTemplateProperty, value); }
}
public static readonly DependencyProperty BigTickTemplateProperty =
DependencyProperty.Register("BigTickTemplate", typeof(DataTemplate), typeof(YAxisTicks), new PropertyMetadata(null));
#endregion
public override void Render(RealTimeGraphExBase graph, bool animate = false)
{
if (graph == null) return;
if (SmallTicks < 2) return;
if (innerGrid == null)
{
innerGrid = new Grid();
}
double animationTime = animate ? 0.2 : 0;
Grid grid = innerGrid;
DoubleAnimation heightAnimation = new DoubleAnimation() { Duration = new Duration(TimeSpan.FromSeconds(animationTime)) };
heightAnimation.To = double.IsNaN(Graph.gridLinesAndImageWrapperGrid.Height) ? Graph.gridLinesAndImageWrapperGrid.ActualHeight : Graph.gridLinesAndImageWrapperGrid.Height;
if (!double.IsNaN(grid.Height))
{
grid.BeginAnimation(Grid.HeightProperty, heightAnimation);
}
else
{
grid.Height = Graph.gridLinesAndImageWrapperGrid.ActualHeight;
}
grid.VerticalAlignment = System.Windows.VerticalAlignment.Top;
ThicknessAnimation marginAni = new ThicknessAnimation() { Duration = new Duration(TimeSpan.FromSeconds(animationTime)) };
marginAni.To = new Thickness(0, Graph.gridLinesAndImageWrapperGrid.Margin.Top, 0, 0);
if (!double.IsNaN(grid.Height))
{
grid.BeginAnimation(MarginProperty, marginAni);
}
else
{
grid.Margin = new Thickness(0, Graph.gridLinesAndImageWrapperGrid.Margin.Top, 0, 0);
}
double height = double.IsNaN(Graph.gridLinesAndImageWrapperGrid.Height) ? Graph.gridLinesAndImageWrapperGrid.ActualHeight : Graph.gridLinesAndImageWrapperGrid.Height;
int smallTicks = (int)Math.Round((SmallTicks * (((height * 100) / Graph.gridMain.ActualHeight) / 100)), MidpointRounding.ToEven);
this.Content = grid;
this.ClipToBounds = true;
grid.RowDefinitions.Clear();
grid.Children.Clear();
int bigTickCounter = BigTicks;
for (int i = 0; i < smallTicks; i++)
{
RowDefinition rowTick = new RowDefinition();
rowTick.Height = new GridLength(1, GridUnitType.Star);
grid.RowDefinitions.Add(rowTick);
if (i == bigTickCounter)
{
YAxisBigTick bigTick = new YAxisBigTick();
bigTick.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
bigTick.VerticalAlignment = System.Windows.VerticalAlignment.Top;
if (BigTickTemplate != null)
{
bigTick.ContentTemplate = BigTickTemplate;
}
bigTickCounter += BigTicks;
grid.Children.Add(bigTick);
Grid.SetRow(bigTick, i);
}
else
{
YAxisSmallTick smallTick = new YAxisSmallTick();
smallTick.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
smallTick.VerticalAlignment = System.Windows.VerticalAlignment.Top;
if (SmallTickTemplate != null)
{
smallTick.ContentTemplate = SmallTickTemplate;
}
grid.Children.Add(smallTick);
Grid.SetRow(smallTick, i);
}
}
//Add last tick
YAxisSmallTick lastTick = new YAxisSmallTick();
lastTick.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
lastTick.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
if (SmallTickTemplate != null)
{
lastTick.ContentTemplate = SmallTickTemplate;
}
grid.Children.Add(lastTick);
Grid.SetRow(lastTick, smallTicks - 1);
}
protected override void OnGraphZoomComplete(Point transformOrigin, double scaleX, double scaleY)
{
Render(Graph, true);
}
protected override void OnGraphPanningComplete(Point translate)
{
if (innerGrid != null)
{
var renderBounds = Graph.GetGraphRenderBounds();
innerGrid.BeginAnimation(MarginProperty, null);
innerGrid.Margin = new Thickness(0, Graph.gridLinesAndImageWrapperGrid.Margin.Top, 0, 0);
}
}
}
}