1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
|
---|
6 | using System.Collections.ObjectModel;
|
---|
7 |
|
---|
8 | namespace Microsoft.Research.DynamicDataDisplay.Charts.NewAxis
|
---|
9 | {
|
---|
10 | public sealed class DefaultDoubleTicksProvider : ITicksProvider<double>
|
---|
11 | {
|
---|
12 | public double[] GetTicks(Range<double> range, int preferredTicksCount)
|
---|
13 | {
|
---|
14 | double start = range.Min;
|
---|
15 | double finish = range.Max;
|
---|
16 |
|
---|
17 | double delta = finish - start;
|
---|
18 |
|
---|
19 | int log = (int)Math.Round(Math.Log10(delta));
|
---|
20 |
|
---|
21 | double newStart = Round(start, log);
|
---|
22 | double newFinish = Round(finish, log);
|
---|
23 | if (newStart == newFinish)
|
---|
24 | {
|
---|
25 | log--;
|
---|
26 | newStart = Round(start, log);
|
---|
27 | newFinish = Round(finish, log);
|
---|
28 | }
|
---|
29 |
|
---|
30 | double step = (newFinish - newStart) / preferredTicksCount;
|
---|
31 |
|
---|
32 | //double[] ticks = CreateTicks(newStart, newFinish, preferredTicksCount);
|
---|
33 | double[] ticks = CreateTicks(start, finish, step);
|
---|
34 | return ticks;
|
---|
35 | }
|
---|
36 |
|
---|
37 | protected static double[] CreateTicks(double start, double finish, double step)
|
---|
38 | {
|
---|
39 | double x = step * (Math.Floor(start / step) + 1);
|
---|
40 | List<double> res = new List<double>();
|
---|
41 | while (x <= finish)
|
---|
42 | {
|
---|
43 | res.Add(x);
|
---|
44 | x += step;
|
---|
45 | }
|
---|
46 | return res.ToArray();
|
---|
47 | }
|
---|
48 |
|
---|
49 | //private static double[] CreateTicks(double start, double finish, int tickCount)
|
---|
50 | //{
|
---|
51 | // double[] ticks = new double[tickCount];
|
---|
52 | // if (tickCount == 0)
|
---|
53 | // return ticks;
|
---|
54 |
|
---|
55 | // DebugVerify.Is(tickCount > 0);
|
---|
56 |
|
---|
57 | // double delta = (finish - start) / (tickCount - 1);
|
---|
58 |
|
---|
59 | // for (int i = 0; i < tickCount; i++)
|
---|
60 | // {
|
---|
61 | // ticks[i] = start + i * delta;
|
---|
62 | // }
|
---|
63 |
|
---|
64 | // return ticks;
|
---|
65 | //}
|
---|
66 |
|
---|
67 | private static double Round(double number, int rem)
|
---|
68 | {
|
---|
69 | if (rem <= 0)
|
---|
70 | {
|
---|
71 | return Math.Round(number, -rem);
|
---|
72 | }
|
---|
73 | else
|
---|
74 | {
|
---|
75 | double pow = Math.Pow(10, rem - 1);
|
---|
76 | double val = pow * Math.Round(number / Math.Pow(10, rem - 1));
|
---|
77 | return val;
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | private static ReadOnlyCollection<int> TickCount =
|
---|
82 | new ReadOnlyCollection<int>(new int[] { 20, 10, 5, 4, 2, 1 });
|
---|
83 |
|
---|
84 | public const int DefaultPreferredTicksCount = 10;
|
---|
85 |
|
---|
86 | public int DecreaseTickCount(int tickCount)
|
---|
87 | {
|
---|
88 | return TickCount.FirstOrDefault(tick => tick < tickCount);
|
---|
89 | }
|
---|
90 |
|
---|
91 | public int IncreaseTickCount(int tickCount) {
|
---|
92 | int newTickCount = TickCount.Reverse().FirstOrDefault(tick => tick > tickCount);
|
---|
93 | if (newTickCount == 0)
|
---|
94 | newTickCount = TickCount[0];
|
---|
95 |
|
---|
96 | return newTickCount;
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|