1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Windows.Markup;
|
---|
6 | using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
|
---|
7 |
|
---|
8 | namespace Microsoft.Research.DynamicDataDisplay.Charts.Axes.Numeric
|
---|
9 | {
|
---|
10 | /// <summary>
|
---|
11 | /// Represents a ticks provider for logarithmically transfomed axis - returns ticks which are a power of specified logarithm base.
|
---|
12 | /// </summary>
|
---|
13 | public class LogarithmNumericTicksProvider : ITicksProvider<double>
|
---|
14 | {
|
---|
15 | /// <summary>
|
---|
16 | /// Initializes a new instance of the <see cref="LogarithmNumericTicksProvider"/> class.
|
---|
17 | /// </summary>
|
---|
18 | public LogarithmNumericTicksProvider()
|
---|
19 | {
|
---|
20 | minorProvider = new MinorNumericTicksProvider(this);
|
---|
21 | minorProvider.Changed += ticksProvider_Changed;
|
---|
22 | }
|
---|
23 |
|
---|
24 | /// <summary>
|
---|
25 | /// Initializes a new instance of the <see cref="LogarithmNumericTicksProvider"/> class.
|
---|
26 | /// </summary>
|
---|
27 | /// <param name="logarithmBase">The logarithm base.</param>
|
---|
28 | public LogarithmNumericTicksProvider(double logarithmBase)
|
---|
29 | : this()
|
---|
30 | {
|
---|
31 | LogarithmBase = logarithmBase;
|
---|
32 | }
|
---|
33 |
|
---|
34 | private void ticksProvider_Changed(object sender, EventArgs e)
|
---|
35 | {
|
---|
36 | Changed.Raise(this);
|
---|
37 | }
|
---|
38 |
|
---|
39 | private double logarithmBase = 10;
|
---|
40 | public double LogarithmBase
|
---|
41 | {
|
---|
42 | get { return logarithmBase; }
|
---|
43 | set
|
---|
44 | {
|
---|
45 | if (value <= 0)
|
---|
46 | throw new ArgumentOutOfRangeException(Strings.Exceptions.LogarithmBaseShouldBePositive);
|
---|
47 |
|
---|
48 | logarithmBase = value;
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | private double LogByBase(double d)
|
---|
53 | {
|
---|
54 | return Math.Log10(d) / Math.Log10(logarithmBase);
|
---|
55 | }
|
---|
56 |
|
---|
57 | #region ITicksProvider<double> Members
|
---|
58 |
|
---|
59 | private double[] ticks;
|
---|
60 | public ITicksInfo<double> GetTicks(Range<double> range, int ticksCount)
|
---|
61 | {
|
---|
62 | double min = LogByBase(range.Min);
|
---|
63 | double max = LogByBase(range.Max);
|
---|
64 |
|
---|
65 | double minDown = Math.Floor(min);
|
---|
66 | double maxUp = Math.Ceiling(max);
|
---|
67 |
|
---|
68 | double logLength = LogByBase(range.GetLength());
|
---|
69 |
|
---|
70 | ticks = CreateTicks(range);
|
---|
71 |
|
---|
72 | int log = RoundingHelper.GetDifferenceLog(range.Min, range.Max);
|
---|
73 | TicksInfo<double> result = new TicksInfo<double> { Ticks = ticks, TickSizes = ArrayExtensions.CreateArray(ticks.Length, 1.0), Info = log };
|
---|
74 | return result;
|
---|
75 | }
|
---|
76 |
|
---|
77 | private double[] CreateTicks(Range<double> range)
|
---|
78 | {
|
---|
79 | double min = LogByBase(range.Min);
|
---|
80 | double max = LogByBase(range.Max);
|
---|
81 |
|
---|
82 | double minDown = Math.Floor(min);
|
---|
83 | double maxUp = Math.Ceiling(max);
|
---|
84 |
|
---|
85 | int intStart = (int)Math.Floor(minDown);
|
---|
86 | int count = (int)(maxUp - minDown + 1);
|
---|
87 |
|
---|
88 | var ticks = new double[count];
|
---|
89 | for (int i = 0; i < count; i++)
|
---|
90 | {
|
---|
91 | ticks[i] = intStart + i;
|
---|
92 | }
|
---|
93 |
|
---|
94 | for (int i = 0; i < ticks.Length; i++)
|
---|
95 | {
|
---|
96 | ticks[i] = Math.Pow(logarithmBase, ticks[i]);
|
---|
97 | }
|
---|
98 |
|
---|
99 | return ticks;
|
---|
100 | }
|
---|
101 |
|
---|
102 | public int DecreaseTickCount(int ticksCount)
|
---|
103 | {
|
---|
104 | return ticksCount;
|
---|
105 | }
|
---|
106 |
|
---|
107 | public int IncreaseTickCount(int ticksCount)
|
---|
108 | {
|
---|
109 | return ticksCount;
|
---|
110 | }
|
---|
111 |
|
---|
112 | private MinorNumericTicksProvider minorProvider;
|
---|
113 | public ITicksProvider<double> MinorProvider
|
---|
114 | {
|
---|
115 | get
|
---|
116 | {
|
---|
117 | minorProvider.SetRanges(ArrayExtensions.GetPairs(ticks));
|
---|
118 | return minorProvider;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | public ITicksProvider<double> MajorProvider
|
---|
123 | {
|
---|
124 | get { return null; }
|
---|
125 | }
|
---|
126 |
|
---|
127 | public event EventHandler Changed;
|
---|
128 |
|
---|
129 | #endregion
|
---|
130 | }
|
---|
131 | }
|
---|