PageRenderTime 15ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

/Main/src/DynamicDataDisplay/Charts/Axes/Numeric/CustomBaseNumericLabelProvider.cs

#
C# | 92 lines | 64 code | 9 blank | 19 comment | 9 complexity | 470eec4d1c267cbdfbfc36a433df6e9a MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5using System.Windows;
 6using Microsoft.Research.DynamicDataDisplay;
 7
 8namespace Microsoft.Research.DynamicDataDisplay.Charts.Axes.Numeric
 9{
10	public class CustomBaseNumericLabelProvider : LabelProvider<double>
11	{
12		private double customBase = 2;
13		/// <summary>
14		/// Gets or sets the custom base.
15		/// </summary>
16		/// <value>The custom base.</value>
17		public double CustomBase
18		{
19			get { return customBase; }
20			set
21			{
22				if (Double.IsNaN(value))
23					throw new ArgumentException(Strings.Exceptions.CustomBaseTicksProviderBaseIsNaN);
24				if (value <= 0)
25					throw new ArgumentOutOfRangeException(Strings.Exceptions.CustomBaseTicksProviderBaseIsTooSmall);
26
27				customBase = value;
28			}
29		}
30
31		/// <summary>
32		/// Initializes a new instance of the <see cref="CustomBaseNumericLabelProvider"/> class.
33		/// </summary>
34		public CustomBaseNumericLabelProvider() { }
35
36		/// <summary>
37		/// Initializes a new instance of the <see cref="CustomBaseNumericLabelProvider"/> class.
38		/// </summary>
39		public CustomBaseNumericLabelProvider(double customBase)
40			: this()
41		{
42			CustomBase = customBase;
43		}
44
45		/// <summary>
46		/// Initializes a new instance of the <see cref="CustomBaseNumericLabelProvider"/> class.
47		/// </summary>
48		/// <param name="customBase">The custom base.</param>
49		/// <param name="customBaseString">The custom base string.</param>
50		public CustomBaseNumericLabelProvider(double customBase, string customBaseString)
51			: this(customBase)
52		{
53			CustomBaseString = customBaseString;
54		}
55
56		private string customBaseString = null;
57		/// <summary>
58		/// Gets or sets the custom base string.
59		/// </summary>
60		/// <value>The custom base string.</value>
61		public string CustomBaseString
62		{
63			get { return customBaseString; }
64			set
65			{
66				if (customBaseString != value)
67				{
68					customBaseString = value;
69					RaiseChanged();
70				}
71			}
72		}
73
74		protected override string GetStringCore(LabelTickInfo<double> tickInfo)
75		{
76			double value = tickInfo.Tick / customBase;
77
78			string customBaseStr = customBaseString ?? customBase.ToString();
79			string result;
80			if (value == 1)
81				result = customBaseStr;
82			else if (value == -1)
83			{
84				result = "-" + customBaseStr;
85			}
86			else
87				result = value.ToString() + customBaseStr;
88
89			return result;
90		}
91	}
92}