aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Visuals/YAxisTicks/YAxisTicks.xaml.cs
blob: 6aa713d167f9701a24b5a863b02eba911e02ff8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Tango.Visuals
{
    /// <summary>
    /// Interaction logic for TicksAxis.xaml
    /// </summary>
    public partial class YAxisTicks : UserControl
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="YAxisTicks"/> class.
        /// </summary>
        public YAxisTicks()
        {
            InitializeComponent();
            this.FocusVisualStyle = null;
            this.Focusable = false;
            this.Loaded += TicksAxis_Loaded;
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Handles the Loaded event of the TicksAxis control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void TicksAxis_Loaded(object sender, RoutedEventArgs e)
        {
            DrawTicks();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the ticks.
        /// </summary>
        public int Ticks
        {
            get { return (int)GetValue(TicksProperty); }
            set { SetValue(TicksProperty, value); }
        }
        public static readonly DependencyProperty TicksProperty =
            DependencyProperty.Register("Ticks", typeof(int), typeof(YAxisTicks), new PropertyMetadata(11, (d, e) => (d as YAxisTicks).DrawTicks()));

        /// <summary>
        /// Gets or sets the tick template.
        /// </summary>
        public DataTemplate TickTemplate
        {
            get { return (DataTemplate)GetValue(TickTemplateProperty); }
            set { SetValue(TickTemplateProperty, value); }
        }
        public static readonly DependencyProperty TickTemplateProperty =
            DependencyProperty.Register("TickTemplate", typeof(DataTemplate), typeof(YAxisTicks), new PropertyMetadata(null, (d, e) => (d as YAxisTicks).DrawTicks()));

        /// <summary>
        /// Gets or sets the big tick interval.
        /// </summary>
        public int? BigTickInterval
        {
            get { return (int?)GetValue(BigTickIntervalProperty); }
            set { SetValue(BigTickIntervalProperty, value); }
        }
        public static readonly DependencyProperty BigTickIntervalProperty =
            DependencyProperty.Register("BigTickInterval", typeof(int?), typeof(YAxisTicks), new PropertyMetadata(null, (d, e) => (d as YAxisTicks).DrawTicks()));

        /// <summary>
        /// Gets or sets the big tick template.
        /// </summary>
        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

        #region Methods

        /// <summary>
        /// Draws the ticks.
        /// </summary>
        private void DrawTicks()
        {
            grid.Children.Clear();
            grid.RowDefinitions.Clear();

            for (int i = 0; i < Ticks; i++)
            {
                if (i == Ticks - 1)
                {
                    ContentControl tick = null;

                    if (BigTickInterval != null && BigTickTemplate != null)
                    {
                        tick = AddBigTick(i, i);
                    }
                    else
                    {
                        tick = AddTick(i, i);
                    }
                    tick.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
                    grid.Children.Add(tick);
                }
                else
                {
                    ContentControl tick = null;

                    RowDefinition row = new RowDefinition();
                    row.Height = new GridLength(1, GridUnitType.Star);
                    grid.RowDefinitions.Add(row);


                    if (BigTickInterval != null && (double)i % (double)BigTickInterval == 0 && BigTickTemplate != null)
                    {
                        tick = AddBigTick(i, i);
                    }
                    else
                    {
                        tick = AddTick(i, i);
                    }

                    grid.Children.Add(tick);
                }
            }
        }

        /// <summary>
        /// Adds the tick.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="index">The index.</param>
        /// <returns></returns>
        private ContentControl AddTick(double value, int index)
        {
            ContentControl tick = new ContentControl();
            tick.Focusable = false;
            tick.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            tick.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            Grid.SetRow(tick, index);

            if (TickTemplate != null)
            {
                tick.ContentTemplate = TickTemplate;
            }

            return tick;
        }

        /// <summary>
        /// Adds big tick.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="index">The index.</param>
        /// <returns></returns>
        private ContentControl AddBigTick(double value, int index)
        {
            ContentControl tick = new ContentControl();
            tick.Focusable = false;
            tick.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            tick.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            Grid.SetRow(tick, index);

            if (BigTickTemplate != null)
            {
                tick.ContentTemplate = BigTickTemplate;
            }

            return tick;
        }

        #endregion

    }
}