-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAxisBinder.cs
More file actions
376 lines (311 loc) · 12.6 KB
/
Copy pathAxisBinder.cs
File metadata and controls
376 lines (311 loc) · 12.6 KB
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
using System;
using System.Drawing;
using System.Linq;
using DevExpress.Utils;
using DevExpress.XtraCharts;
using OSPSuite.Core.Chart;
using OSPSuite.Core.Chart.Mappers;
using OSPSuite.Core.Domain;
using OSPSuite.Core.Domain.UnitSystem;
using OSPSuite.Presentation.Presenters.Charts;
using OSPSuite.UI.Controls;
using OSPSuite.UI.Extensions;
using OSPSuite.Utility;
using OSPSuite.Utility.Extensions;
using OSPSuite.Utility.Format;
using DevExpressAxis = DevExpress.XtraCharts.Axis;
using OSPAxis = OSPSuite.Core.Chart.Axis;
using Range = DevExpress.XtraCharts.Range;
namespace OSPSuite.UI.Binders
{
public class AxisBinder : IAxisBinder
{
private const int DEVEXPRESS_DEFAULT_Y_MINOR_TICKS = 4;
private const int DEVEXPRESS_DEFAULT_X_MINOR_TICKS = 1;
private const int MINOR_COUNT_IN_LOG_MODE = 8;
private readonly DevExpressAxis _axisView;
private readonly INumericFormatterOptions _numericFormatterOptions;
private readonly int _defaultMinorTickCount;
private bool _explicitRange;
private readonly UnitToMinorIntervalMapper _unitToMinorIntervalMapper;
private readonly UxChartControl _chartControl;
public AxisBinder(OSPAxis axis, UxChartControl chartControl, INumericFormatterOptions numericFormatterOptions)
{
Axis = axis;
_chartControl = chartControl;
_unitToMinorIntervalMapper = new UnitToMinorIntervalMapper();
_defaultMinorTickCount = AxisType == AxisTypes.X ? DEVEXPRESS_DEFAULT_X_MINOR_TICKS : DEVEXPRESS_DEFAULT_Y_MINOR_TICKS;
_axisView = retrieveAxisView();
_axisView.VisualRange.Auto = false;
_numericFormatterOptions = numericFormatterOptions;
}
private DevExpressAxis retrieveAxisView()
{
if (AxisType == AxisTypes.X)
return xyDiagram.AxisX;
if (AxisType == AxisTypes.Y)
return xyDiagram.AxisY;
int axisTypeIndex = (int)AxisType;
const int secondaryAxisOffset = (int)AxisTypes.Y2;
// create yN-Axis, if necessary, and also the preceding yN-Axes
for (int i = xyDiagram.SecondaryAxesY.Count; i <= axisTypeIndex - secondaryAxisOffset; i++)
{
var typeOfAxisView = EnumHelper.AllValuesFor<AxisTypes>().ElementAt(i + secondaryAxisOffset);
var secondaryAxisY = new SecondaryAxisY(typeOfAxisView.ToString());
xyDiagram.SecondaryAxesY.Add(secondaryAxisY);
}
return xyDiagram.SecondaryAxesY[axisTypeIndex - secondaryAxisOffset];
}
public AxisTypes AxisType => Axis.AxisType;
private XYDiagram xyDiagram => _chartControl.XYDiagram;
public OSPAxis Axis { get; }
public bool Visible
{
get => _axisView.Visibility == DefaultBoolean.True;
set
{
var reallyVisible = value && Axis.Visible;
_axisView.Visibility = reallyVisible ? DefaultBoolean.True : DefaultBoolean.False;
_axisView.GridLines.Visible = reallyVisible && Axis.GridLines;
}
}
public void Dispose()
{
if (AxisType < AxisTypes.Y2 || xyDiagram == null)
return;
var secondaryAxisY = _axisView.DowncastTo<SecondaryAxisY>();
xyDiagram.SecondaryAxesY.Remove(secondaryAxisY);
}
public object AxisView => _axisView;
public void RefreshRange(bool sideMarginsEnabled, Size diagramSize)
{
setAxisRange(sideMarginsEnabled, diagramSize);
}
public void Refresh()
{
if (Axis.Dimension == null || Axis.UnitName == null)
return;
_axisView.GridLines.Visible = Axis.GridLines;
if (!Axis.Visible)
Visible = false;
setAxisTitle();
setLabels();
}
/// <summary>
/// Cut the min value for logarithmic axis by the smallest positive value possible.
/// </summary>
private void adjustMinForLogScale(Range range)
{
if (range.MinValue == null)
return;
var doubleValue = Convert.ToDouble(range.MinValue);
if (!(doubleValue < double.Epsilon))
return;
range.Auto = false;
range.MinValue = double.Epsilon;
}
private bool isAuto => Axis.NumberMode == NumberModes.Relative || noLimitsSet;
private bool noLimitsSet => !Axis.Min.HasValue && !Axis.Max.HasValue;
private bool allLimitsSet => Axis.Min.HasValue && Axis.Max.HasValue;
private void adjustAxisMinMax()
{
//for log scaling adjust the min value if neccessary to minimum positive value
if (Axis.Scaling == Scalings.Log)
{
if (Axis.Min.HasValue && Axis.Min < float.Epsilon)
{
Axis.Min = float.Epsilon;
}
}
//both limits are set
if (allLimitsSet)
{
if (Axis.Min >= Axis.Max)
{
Axis.ResetRange();
}
return;
}
//no limits are set
if (noLimitsSet)
return;
if (_explicitRange) // do we come from explicit range? Somebody deleted a limit...
{
Axis.ResetRange();
}
else // somebody inserted a limit...
{
if (!Axis.Min.HasValue)
{
var rangeMin = Convert.ToSingle(_axisView.WholeRange.MinValue);
Axis.Min = Axis.Max.HasValue ? Math.Min(Axis.Max.Value, rangeMin) : rangeMin;
}
if (!Axis.Max.HasValue)
Axis.Max = Math.Max(Axis.Min.Value, Convert.ToSingle(_axisView.WholeRange.MaxValue));
}
_explicitRange = !_explicitRange;
}
private void setAxisRange(bool sideMarginsEnabled, Size diagramSize)
{
var axisWidthInPixel = Axis.AxisType == AxisTypes.X ? diagramSize.Width : diagramSize.Height;
_axisView.Logarithmic = Axis.Scaling == Scalings.Log;
_axisView.WholeRange.AlwaysShowZeroLevel = !_axisView.Logarithmic;
adjustAxisMinMax();
setRange(isAuto, sideMarginsEnabled);
configureAxisScale(axisWidthInPixel);
// logarithmic scale depending settings
if (!_axisView.Logarithmic) return;
//8 minor counts in log mode
_axisView.MinorCount = MINOR_COUNT_IN_LOG_MODE;
adjustMinForLogScale();
}
private bool shouldApplyPreferredMinorTicks()
{
if (Axis.Dimension == null)
return false;
return Axis.Dimension.IsTime()
&& _unitToMinorIntervalMapper.HasPreferredMinorIntervalsFor(Axis.Unit);
}
private void automaticallyConfigureAxisScale()
{
_axisView.MinorCount = _defaultMinorTickCount;
_axisView.NumericScaleOptions.AutoGrid = true;
}
// The manual tick override is only applied for linearly scaled axes. Logarithmic axes keep their
// decade gridlines and the fixed minor count handled in setAxisRange.
private bool hasManualTickSettings => Axis.Scaling != Scalings.Log && (hasValidMajorInterval || Axis.MinorTicks.HasValue);
private bool hasValidMajorInterval => Axis.MajorInterval.HasValue && Axis.MajorInterval.Value > 0;
// The charting component throws if the minor tick count is outside [MIN_MINOR_TICKS, MAX_MINOR_TICKS]. An out-of-range
// value is flagged to the user by the axis validation rules; here we simply fall back to the automatic count.
private bool hasValidMinorTicks =>
Axis.MinorTicks.HasValue && Axis.MinorTicks.Value >= OSPAxis.MIN_MINOR_TICKS && Axis.MinorTicks.Value <= OSPAxis.MAX_MINOR_TICKS;
private void applyManualTickSettings()
{
if (hasValidMajorInterval)
{
_axisView.NumericScaleOptions.AutoGrid = false;
_axisView.NumericScaleOptions.GridSpacing = Axis.MajorInterval.Value;
_axisView.NumericScaleOptions.GridAlignment = NumericGridAlignment.Ones;
_axisView.NumericScaleOptions.MeasureUnit = NumericMeasureUnit.Ones;
}
else
{
_axisView.NumericScaleOptions.AutoGrid = true;
}
if (hasValidMinorTicks)
{
_axisView.MinorCount = Axis.MinorTicks.Value;
_axisView.Tickmarks.MinorVisible = true;
}
else
{
_axisView.MinorCount = _defaultMinorTickCount;
}
}
private void configureAxisScale(int axisWidthInPixel)
{
if (hasManualTickSettings)
{
applyManualTickSettings();
return;
}
var ticksConfig = new TicksConfig { AutoScale = true };
if (shouldApplyPreferredMinorTicks())
ticksConfig = calculateMinorIntervalsPerMajorInterval(axisWidthInPixel);
if (ticksConfig.AutoScale)
{
automaticallyConfigureAxisScale();
}
else
{
_axisView.MinorCount = ticksConfig.MinorTicks;
_axisView.Tickmarks.MinorVisible = true;
_axisView.NumericScaleOptions.AutoGrid = false;
_axisView.NumericScaleOptions.GridSpacing = ticksConfig.GridSpacing;
_axisView.NumericScaleOptions.GridAlignment = NumericGridAlignment.Ones;
_axisView.NumericScaleOptions.MeasureUnit = NumericMeasureUnit.Ones;
}
}
private TicksConfig calculateMinorIntervalsPerMajorInterval(int axisWidthInPixel)
{
var axisWidthInUnit = Convert.ToSingle(_axisView.VisualRange.MaxValue) - Convert.ToSingle(_axisView.VisualRange.MinValue);
return _unitToMinorIntervalMapper.MapFrom(Axis.Unit, axisWidthInUnit, axisWidthInPixel);
}
private void adjustMinForLogScale()
{
adjustMinForLogScale(_axisView.WholeRange);
adjustMinForLogScale(_axisView.VisualRange);
}
private void setRange(bool autoRange, bool sideMarginsEnabled)
{
// For DevExpress, setting the sideMargins affects the auto range calculation property.
// We want to decouple that relationship so set side margins first
// then we set the auto range.
_axisView.SetSideMarginsEnabled(sideMarginsEnabled);
if (autoRange)
enableAutoRange(true);
else
setExplicitRange();
}
private void setExplicitRange()
{
if (!allLimitsSet)
return;
enableAutoRange(false);
var currentMin = Convert.ToDouble(_axisView.WholeRange.MinValue);
var currentMax = Convert.ToDouble(_axisView.WholeRange.MaxValue);
var settingsMin = Convert.ToDouble(Axis.Min);
var settingsMax = Convert.ToDouble(Axis.Max);
//first we might have to enlarge the whole range
if (settingsMin < currentMin || settingsMax > currentMax)
_axisView.WholeRange.SetMinMaxValues(settingsMin, settingsMax);
//if limits are already set we avoid a new setting
if (Convert.ToDouble(_axisView.VisualRange.MinValue).Equals(settingsMin) &&
Convert.ToDouble(_axisView.VisualRange.MaxValue).Equals(settingsMax))
return;
_axisView.VisualRange.SetMinMaxValues(settingsMin, settingsMax);
}
private void enableAutoRange(bool autoRangeEnabled)
{
// modify the Auto property of WholeRange
_axisView.WholeRange.Auto = autoRangeEnabled;
if (!autoRangeEnabled)
return;
// we only reset the VisualRange to WholeRange if enabling autoRange
// do not auto range the VisualRange because it has unwanted side effects
_axisView.ResetVisualRange();
}
private void setLabels()
{
string pattern;
switch (Axis.NumberMode)
{
case NumberModes.Scientific:
pattern = $"V:E{_numericFormatterOptions.DecimalPlace}";
break;
case NumberModes.Normal:
pattern = "V:G15";
break;
case NumberModes.Relative:
pattern = "V:P";
break;
default:
return;
}
updateLabelTextPattern(pattern);
}
private void updateLabelTextPattern(string pattern)
{
pattern = $"{{{pattern}}}";
if (string.Equals(_axisView.Label.TextPattern, pattern))
return;
_axisView.Label.TextPattern = pattern;
}
private void setAxisTitle()
{
_axisView.Title.Visibility = DefaultBoolean.True;
_axisView.Title.Alignment = StringAlignment.Center;
_axisView.Title.Text = Constants.NameWithUnitFor(!string.IsNullOrEmpty(Axis.Caption) ? Axis.Caption : Axis.Dimension.DisplayName, Axis.UnitName);
}
}
}