1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
|
---|
6 |
|
---|
7 | namespace Microsoft.Research.DynamicDataDisplay.Charts
|
---|
8 | {
|
---|
9 | internal abstract class MinorTimeProviderBase<T> : ITicksProvider<T>
|
---|
10 | {
|
---|
11 | public event EventHandler Changed;
|
---|
12 | protected void RaiseChanged()
|
---|
13 | {
|
---|
14 | if (Changed != null)
|
---|
15 | {
|
---|
16 | Changed(this, EventArgs.Empty);
|
---|
17 | }
|
---|
18 | }
|
---|
19 |
|
---|
20 | private readonly ITicksProvider<T> provider;
|
---|
21 | public MinorTimeProviderBase(ITicksProvider<T> provider)
|
---|
22 | {
|
---|
23 | this.provider = provider;
|
---|
24 | }
|
---|
25 |
|
---|
26 | private T[] majorTicks = new T[] { };
|
---|
27 | internal void SetTicks(T[] ticks)
|
---|
28 | {
|
---|
29 | this.majorTicks = ticks;
|
---|
30 | }
|
---|
31 |
|
---|
32 | private double ticksSize = 0.5;
|
---|
33 | public ITicksInfo<T> GetTicks(Range<T> range, int ticksCount)
|
---|
34 | {
|
---|
35 | if (majorTicks.Length == 0)
|
---|
36 | return new TicksInfo<T>();
|
---|
37 |
|
---|
38 | ticksCount /= majorTicks.Length;
|
---|
39 | if (ticksCount == 0)
|
---|
40 | ticksCount = 2;
|
---|
41 |
|
---|
42 | var ticks = majorTicks.GetPairs().Select(r => Clip(provider.GetTicks(r, ticksCount), r)).
|
---|
43 | SelectMany(t => t.Ticks).ToArray();
|
---|
44 |
|
---|
45 | var res = new TicksInfo<T>
|
---|
46 | {
|
---|
47 | Ticks = ticks,
|
---|
48 | TickSizes = ArrayExtensions.CreateArray(ticks.Length, ticksSize)
|
---|
49 | };
|
---|
50 | return res;
|
---|
51 | }
|
---|
52 |
|
---|
53 | private ITicksInfo<T> Clip(ITicksInfo<T> ticks, Range<T> range)
|
---|
54 | {
|
---|
55 | var newTicks = new List<T>(ticks.Ticks.Length);
|
---|
56 | var newSizes = new List<double>(ticks.TickSizes.Length);
|
---|
57 |
|
---|
58 | for (int i = 0; i < ticks.Ticks.Length; i++)
|
---|
59 | {
|
---|
60 | T tick = ticks.Ticks[i];
|
---|
61 | if (IsInside(tick, range))
|
---|
62 | {
|
---|
63 | newTicks.Add(tick);
|
---|
64 | newSizes.Add(ticks.TickSizes[i]);
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | return new TicksInfo<T>
|
---|
69 | {
|
---|
70 | Ticks = newTicks.ToArray(),
|
---|
71 | TickSizes = newSizes.ToArray(),
|
---|
72 | Info = ticks.Info
|
---|
73 | };
|
---|
74 | }
|
---|
75 |
|
---|
76 | protected abstract bool IsInside(T value, Range<T> range);
|
---|
77 |
|
---|
78 | public int DecreaseTickCount(int ticksCount)
|
---|
79 | {
|
---|
80 | if (majorTicks.Length > 0)
|
---|
81 | ticksCount /= majorTicks.Length;
|
---|
82 |
|
---|
83 | int minorTicksCount = provider.DecreaseTickCount(ticksCount);
|
---|
84 |
|
---|
85 | if (majorTicks.Length > 0)
|
---|
86 | minorTicksCount *= majorTicks.Length;
|
---|
87 |
|
---|
88 | return minorTicksCount;
|
---|
89 | }
|
---|
90 |
|
---|
91 | public int IncreaseTickCount(int ticksCount)
|
---|
92 | {
|
---|
93 | if (majorTicks.Length > 0)
|
---|
94 | ticksCount /= majorTicks.Length;
|
---|
95 |
|
---|
96 | int minorTicksCount = provider.IncreaseTickCount(ticksCount);
|
---|
97 |
|
---|
98 | if (majorTicks.Length > 0)
|
---|
99 | minorTicksCount *= majorTicks.Length;
|
---|
100 |
|
---|
101 | return minorTicksCount;
|
---|
102 | }
|
---|
103 |
|
---|
104 | public ITicksProvider<T> MinorProvider
|
---|
105 | {
|
---|
106 | get { return null; }
|
---|
107 | }
|
---|
108 |
|
---|
109 | public ITicksProvider<T> MajorProvider
|
---|
110 | {
|
---|
111 | get { return null; }
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|