forked from Live-Charts/Live-Charts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindowAxisCore.cs
210 lines (169 loc) · 7.82 KB
/
WindowAxisCore.cs
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
198
199
200
201
202
203
204
205
206
207
208
209
210
//The MIT License(MIT)
//Copyright(c) 2016 Alberto Rodriguez & LiveCharts Contributors
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
using System;
using System.Collections.Generic;
using System.Linq;
using LiveCharts.Charts;
using LiveCharts.Definitions.Charts;
using LiveCharts.Dtos;
namespace LiveCharts
{
/// <summary>
/// Provides an axis that displays separators based upon configured windows
/// </summary>
public class WindowAxisCore : AxisCore
{
private IAxisWindow _selectedWindow;
private IAxisWindow SelectedWindow
{
get { return _selectedWindow; }
set
{
if (Equals(_selectedWindow, value)) return;
_selectedWindow = value;
((IWindowAxisView)View).SetSelectedWindow(value);
}
}
/// <summary>
///
/// </summary>
public List<AxisWindow> Windows { get; set; }
/// <summary>
///
/// </summary>
/// <param name="view"></param>
public WindowAxisCore(IAxisView view) : base(view)
{
}
/// <summary>
///
/// </summary>
internal override CoreMargin PrepareChart(AxisOrientation source, ChartCore chart)
{
if (!(Math.Abs(TopLimit - BotLimit) > S * .01) || !ShowLabels) return new CoreMargin();
var currentMargin = new CoreMargin();
var tolerance = S / 10;
InitializeGarbageCollector();
// Determine which magnitude and unit to use
var m = (!double.IsNaN(View.Unit) ? View.Unit : Magnitude);
var u = (!double.IsNaN(View.Unit) ? View.Unit : 1);
// Calculate the separator indices
var indices = CalculateSeparatorIndices(chart, source, u);
// Draw the separators
foreach (var index in indices)
{
DrawSeparator(index, tolerance, currentMargin, source);
}
return currentMargin;
}
internal IEnumerable<double> CalculateSeparatorIndices(ChartCore chart, AxisOrientation source, double unit)
{
if (!double.IsNaN(Separator.Step)) throw new Exception("Step should be NaN for WindowAxis separators");
if (Windows == null) return Enumerable.Empty<double>();
// Holder for the calculated separator indices and the proposed window
var supportedSeparatorCount = 0;
var separatorIndices = new List<double>();
IAxisWindow proposedWindow = AxisWindows.EmptyWindow;
// Build a range of possible separator indices
var start = (long) Math.Floor(BotLimit);
var count = (long) Math.Floor(TopLimit - (EvaluatesUnitWidth ? unit : 0.0) - BotLimit);
var end = start + count - 1;
var rangeIndices = LongRange(start, end).Select(i => (double)i);
// Make sure we have at least 2 separators to show
if (Windows != null && count > 1)
{
foreach (var window in Windows)
{
IEnumerable<double> proposedSeparatorIndices;
// Calculate the number of supported separators.
supportedSeparatorCount = (int)Math.Floor(chart.ControlSize.Width / (window.MinimumSeparatorWidth * CleanFactor));
// Try go get separators. Continue if the window invalidated.
if (!window.TryGetSeparatorIndices(rangeIndices, supportedSeparatorCount, out proposedSeparatorIndices)) continue;
// Double check whether the window exceeded the maximum separator count.
// It might be it does not respect the supportedSeparatorCount parameter.
separatorIndices = proposedSeparatorIndices.ToList();
if (supportedSeparatorCount < separatorIndices.Count) continue;
// Pick this window. It is the first who passed both validations and our best candidate
proposedWindow = window;
break;
}
}
if (proposedWindow == null)
{
// All variables are still set to defaults
}
// Force the step of 1, as our prepare chart will filter the X axis for valid separators, and will skip a few
S = 1;
Magnitude = Math.Pow(10, Math.Floor(Math.Log(supportedSeparatorCount) / Math.Log(10)));
SelectedWindow = proposedWindow;
return separatorIndices;
}
private IEnumerable<long> LongRange(long start, long end)
{
for (long i = start; i <= end; i++)
{
yield return i;
}
}
private void DrawSeparator(double x, double tolerance, CoreMargin currentMargin, AxisOrientation source)
{
SeparatorElementCore elementCore;
var key = Math.Round(x / tolerance) * tolerance;
if (!Cache.TryGetValue(key, out elementCore))
{
elementCore = new DateSeparatorElementCore { IsNew = true };
Cache[key] = elementCore;
}
else
{
elementCore.IsNew = false;
}
// Determine whether this separator is a header now
((DateSeparatorElementCore)elementCore).IsHeader = SelectedWindow.IsHeader(x);
View.RenderSeparator(elementCore, Chart);
elementCore.Key = key;
elementCore.Value = x;
elementCore.GarbageCollectorIndex = GarbageCollectorIndex;
var labelsMargin = elementCore.View.UpdateLabel(SelectedWindow.FormatAxisLabel(x), this, source);
currentMargin.Width = labelsMargin.TakenWidth > currentMargin.Width
? labelsMargin.TakenWidth
: currentMargin.Width;
currentMargin.Height = labelsMargin.TakenHeight > currentMargin.Height
? labelsMargin.TakenHeight
: currentMargin.Height;
currentMargin.Left = labelsMargin.Left > currentMargin.Left
? labelsMargin.Left
: currentMargin.Left;
currentMargin.Right = labelsMargin.Right > currentMargin.Right
? labelsMargin.Right
: currentMargin.Right;
currentMargin.Top = labelsMargin.Top > currentMargin.Top
? labelsMargin.Top
: currentMargin.Top;
currentMargin.Bottom = labelsMargin.Bottom > currentMargin.Bottom
? labelsMargin.Bottom
: currentMargin.Bottom;
if (LastAxisMax == null)
{
elementCore.State = SeparationState.InitialAdd;
return;
}
elementCore.State = SeparationState.Keep;
}
}
}