forked from Live-Charts/Live-Charts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAxisCore.cs
540 lines (467 loc) · 18 KB
/
AxisCore.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
//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.Globalization;
using System.Linq;
using LiveCharts.Charts;
using LiveCharts.Definitions.Charts;
using LiveCharts.Dtos;
using LiveCharts.Helpers;
namespace LiveCharts
{
/// <summary>
///
/// </summary>
public class AxisCore
{
private double _topLimit;
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="AxisCore"/> class.
/// </summary>
/// <param name="view">The view.</param>
public AxisCore(IAxisView view)
{
View = view;
CleanFactor = 3;
Cache = new Dictionary<double, SeparatorElementCore>();
LastAxisMax = null;
LastAxisMin = null;
}
#endregion
#region Public Properties
/// <summary>
/// Gets or sets the chart.
/// </summary>
/// <value>
/// The chart.
/// </value>
public ChartCore Chart { get; set; }
/// <summary>
/// Gets or sets the view.
/// </summary>
/// <value>
/// The view.
/// </value>
public IAxisView View { get; set; }
/// <summary>
/// Gets or sets the labels.
/// </summary>
/// <value>
/// The labels.
/// </value>
public IList<string> Labels { get; set; }
/// <summary>
/// Gets or sets the sections.
/// </summary>
/// <value>
/// The sections.
/// </value>
public List<AxisSectionCore> Sections { get; set; }
/// <summary>
/// Gets or sets the label formatter.
/// </summary>
/// <value>
/// The label formatter.
/// </value>
public Func<double, string> LabelFormatter { get; set; }
/// <summary>
/// Gets or sets the stroke thickness.
/// </summary>
/// <value>
/// The stroke thickness.
/// </value>
public double StrokeThickness { get; set; }
/// <summary>
/// Gets or sets a value indicating whether [show labels].
/// </summary>
/// <value>
/// <c>true</c> if [show labels]; otherwise, <c>false</c>.
/// </value>
public bool ShowLabels { get; set; }
/// <summary>
/// Gets or sets the maximum value.
/// </summary>
/// <value>
/// The maximum value.
/// </value>
public double MaxValue { get; set; }
/// <summary>
/// Gets or sets the minimum value.
/// </summary>
/// <value>
/// The minimum value.
/// </value>
public double MinValue { get; set; }
/// <summary>
/// Gets or sets the title.
/// </summary>
/// <value>
/// The title.
/// </value>
public string Title { get; set; }
/// <summary>
/// Gets or sets a value indicating whether [disable animations].
/// </summary>
/// <value>
/// <c>true</c> if [disable animations]; otherwise, <c>false</c>.
/// </value>
public bool DisableAnimations { get; set; }
/// <summary>
/// Gets or sets the position.
/// </summary>
/// <value>
/// The position.
/// </value>
public AxisPosition Position { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this instance is merged.
/// </summary>
/// <value>
/// <c>true</c> if this instance is merged; otherwise, <c>false</c>.
/// </value>
public bool IsMerged { get; set; }
/// <summary>
/// Gets a value indicating whether [evaluates unit width].
/// </summary>
/// <value>
/// <c>true</c> if [evaluates unit width]; otherwise, <c>false</c>.
/// </value>
public bool EvaluatesUnitWidth { get; internal set; }
/// <summary>
/// Gets the current separators.
/// </summary>
/// <value>
/// The current separators.
/// </value>
public IEnumerable<SeparatorElementCore> CurrentSeparators
{
get { return Cache.Values; }
}
/// <summary>
/// Gets or sets the separator.
/// </summary>
/// <value>
/// The separator.
/// </value>
public SeparatorConfigurationCore Separator { get; set; }
/// <summary>
/// Gets the s.
/// </summary>
/// <value>
/// The s.
/// </value>
public double S { get; internal set; }
#endregion
#region Internal Properties
internal double Tab { get; set; }
internal double TopLimit
{
get { return _topLimit; }
set
{
_topLimit = value;
}
}
internal double BotLimit { get; set; }
internal double TopSeriesLimit { get; set; }
internal double BotSeriesLimit { get; set; }
internal double MaxPointRadius { get; set; }
internal double Magnitude { get; set; }
internal double CleanFactor { get; set; }
internal Dictionary<double, SeparatorElementCore> Cache { get; set; }
internal double? LastAxisMax { get; set; }
internal double? LastAxisMin { get; set; }
internal CoreRectangle LastPlotArea { get; set; }
internal int GarbageCollectorIndex { get; set; }
internal double PreviousTop { get; set; }
internal double PreviousBot { get; set; }
protected internal double FirstSeparator { get; protected set; }
protected internal double LastSeparator { get; protected set; }
#endregion
#region Public Methods
/// <summary>
/// Gets the formatter.
/// </summary>
/// <returns></returns>
public virtual Func<double, string> GetFormatter()
{
return Formatter;
}
/// <summary>
/// Clears the separators.
/// </summary>
public void ClearSeparators()
{
foreach (var separator in Cache)
{
separator.Value.View.Clear(Chart.View);
}
Cache = new Dictionary<double, SeparatorElementCore>();
}
#endregion
#region Internal Methods
internal virtual void CalculateSeparator(ChartCore chart, AxisOrientation source)
{
var range = TopLimit - BotLimit;
range = range <= 0 ? 1 : range;
//ToDO: Improve this according to current labels!
var separations = source == AxisOrientation.Y
? Math.Round(chart.ControlSize.Height / ((12) * CleanFactor), 0) // at least 3 font 12 labels per separator.
: Math.Round(chart.ControlSize.Width / (50 * CleanFactor), 0); // at least 150 pixels per separator.
separations = separations < 2 ? 2 : separations;
var minimum = range / separations;
Magnitude = Math.Pow(10, Math.Floor(Math.Log(minimum) / Math.Log(10)));
if (!double.IsNaN(Separator.Step))
{
S = Separator.Step;
return;
}
var residual = minimum / Magnitude;
double tick;
if (residual > 5)
tick = 10 * Magnitude;
else if (residual > 2)
tick = 5 * Magnitude;
else if (residual > 1)
tick = 2 * Magnitude;
else
tick = Magnitude;
S = tick;
if (Labels != null) S = S < 1 ? 1 : S;
}
internal virtual CoreMargin PrepareChart(AxisOrientation source, ChartCore chart)
{
if (!(Math.Abs(TopLimit - BotLimit) > S * .01) || !ShowLabels) return new CoreMargin();
CalculateSeparator(chart, source);
var f = GetFormatter();
var currentMargin = new CoreMargin();
var tolerance = S / 10;
InitializeGarbageCollector();
var m = !double.IsNaN(View.BarUnit)
? View.BarUnit
: (!double.IsNaN(View.Unit)
? View.Unit
: Magnitude);
var u = !double.IsNaN(View.BarUnit)
? View.BarUnit
: (!double.IsNaN(View.Unit)
? View.Unit
: 1);
if (TopLimit <= 0 && BotLimit < 0)
{
var l = TopLimit - (EvaluatesUnitWidth ? u : 0);
LastSeparator = l;
for (var i = l; i >= Math.Truncate(BotLimit / m) * m; i -= S)
{
FirstSeparator = i;
DrawSeparator(i, tolerance, currentMargin, f, source);
}
}
else
{
var l = Math.Truncate(BotLimit / m) * m;
FirstSeparator = l;
for (var i = l; i <= TopLimit - (EvaluatesUnitWidth ? u : 0); i += S)
{
LastSeparator = i;
DrawSeparator(i, tolerance, currentMargin, f, source);
}
}
return currentMargin;
}
internal void UpdateSeparators(AxisOrientation source, ChartCore chart, int axisIndex)
{
foreach (var element in Cache.Values.ToArray())
{
if (element.GarbageCollectorIndex < GarbageCollectorIndex)
{
element.State = SeparationState.Remove;
Cache.Remove(element.Key);
}
var toLine = ChartFunctions.ToPlotArea(element.Value, source, chart, axisIndex);
var direction = source == AxisOrientation.X ? 1 : -1;
toLine += EvaluatesUnitWidth ? direction * ChartFunctions.GetUnitWidth(source, chart, this) / 2 : 0;
var toLabel = toLine + element.View.LabelModel.GetOffsetBySource(source);
if (IsMerged)
{
const double padding = 4;
if (source == AxisOrientation.Y)
{
if (toLabel + element.View.LabelModel.ActualHeight >
chart.DrawMargin.Top + chart.DrawMargin.Height)
toLabel -= element.View.LabelModel.ActualHeight + padding;
}
else
{
if (toLabel + element.View.LabelModel.ActualWidth >
chart.DrawMargin.Left + chart.DrawMargin.Width)
toLabel -= element.View.LabelModel.ActualWidth + padding;
}
}
var labelTab = Tab;
labelTab += element.View.LabelModel.GetOffsetBySource(source.Invert());
switch (element.State)
{
case SeparationState.Remove:
if (!chart.View.DisableAnimations && !View.DisableAnimations)
{
element.View.Move(chart, this, source, axisIndex, toLabel, toLine, labelTab);
element.View.FadeOutAndRemove(chart);
}
else
{
element.View.Remove(chart);
}
break;
case SeparationState.Keep:
if (!chart.View.DisableAnimations && !View.DisableAnimations)
{
if (element.IsNew)
{
var toLinePrevious = FromPreviousState(element.Value, source, chart);
toLinePrevious += EvaluatesUnitWidth ? ChartFunctions.GetUnitWidth(source, chart, this) / 2 : 0;
var toLabelPrevious = toLinePrevious + element.View.LabelModel.GetOffsetBySource(source);
element.View.Place(chart, this, source, axisIndex, toLabelPrevious,
toLinePrevious, labelTab);
element.View.FadeIn(this, chart);
}
element.View.Move(chart, this, source, axisIndex, toLabel, toLine, labelTab);
}
else
{
element.View.Place(chart, this, source, axisIndex, toLabel, toLine, labelTab);
}
break;
case SeparationState.InitialAdd:
element.View.Place(chart, this, source, axisIndex, toLabel, toLine, labelTab);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
LastAxisMax = TopLimit;
LastAxisMin = BotLimit;
LastPlotArea = new CoreRectangle(chart.DrawMargin.Left, chart.DrawMargin.Top,
chart.DrawMargin.Width, chart.DrawMargin.Height);
}
internal double FromPreviousState(double value, AxisOrientation source, ChartCore chart)
{
if (LastAxisMax == null) return 0;
var p1 = new CorePoint();
var p2 = new CorePoint();
if (source == AxisOrientation.Y)
{
p1.X = LastAxisMax ?? 0;
p1.Y = LastPlotArea.Top;
p2.X = LastAxisMin ?? 0;
p2.Y = LastPlotArea.Top + LastPlotArea.Height;
}
else
{
p1.X = LastAxisMax ?? 0;
p1.Y = LastPlotArea.Width + LastPlotArea.Left;
p2.X = LastAxisMin ?? 0;
p2.Y = LastPlotArea.Left;
}
var deltaX = p2.X - p1.X;
// ReSharper disable once CompareOfFloatsByEqualityOperator
var m = (p2.Y - p1.Y) / (deltaX == 0 ? double.MinValue : deltaX);
var d = m * (value - p1.X) + p1.Y;
return d;
}
#endregion
#region Protected Methods
/// <summary>
/// Initializes the garbage collector.
/// </summary>
protected void InitializeGarbageCollector()
{
if (GarbageCollectorIndex == int.MaxValue)
{
foreach (var value in Cache.Values)
{
value.GarbageCollectorIndex = 0;
}
GarbageCollectorIndex = 0;
}
GarbageCollectorIndex++;
}
#endregion
#region Private Methods
private void DrawSeparator(double i, double tolerance, CoreMargin currentMargin, Func<double, string> f, AxisOrientation source)
{
if (i < BotLimit) return;
SeparatorElementCore asc;
var key = Math.Round(i / tolerance) * tolerance;
if (!Cache.TryGetValue(key, out asc))
{
asc = new SeparatorElementCore { IsNew = true };
Cache[key] = asc;
}
else
{
asc.IsNew = false;
}
View.RenderSeparator(asc, Chart);
asc.Key = key;
asc.Value = i;
asc.GarbageCollectorIndex = GarbageCollectorIndex;
var labelsMargin = asc.View.UpdateLabel(f(i), 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)
{
asc.State = SeparationState.InitialAdd;
return;
}
asc.State = SeparationState.Keep;
}
private string Formatter(double x)
{
if (Labels == null)
{
return LabelFormatter == null
? x.ToString(CultureInfo.InvariantCulture)
: LabelFormatter(x);
}
if (x < 0) x *= -1;
return Labels.Count > x && x >= 0
? Labels[(int)x]
: "";
}
#endregion
}
}