forked from core-plot/core-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPTMutablePlotRange.m
More file actions
226 lines (179 loc) · 6.19 KB
/
CPTMutablePlotRange.m
File metadata and controls
226 lines (179 loc) · 6.19 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
#import "CPTMutablePlotRange.h"
#import "CPTUtilities.h"
/// @cond
@interface CPTMutablePlotRange()
@property (nonatomic, readwrite) BOOL inValueUpdate;
@end
/// @endcond
#pragma mark -
/** @brief Defines a mutable range of plot data.
*
* If you need to change the plot range, you should use this class rather than the
* immutable super class.
*
**/
@implementation CPTMutablePlotRange
/** @property NSNumber *location
* @brief The starting value of the range.
* @see @ref locationDecimal, @ref locationDouble
**/
@dynamic location;
/** @property NSNumber *length
* @brief The length of the range.
* @see @ref lengthDecimal, @ref lengthDouble
**/
@dynamic length;
/** @property NSDecimal locationDecimal
* @brief The starting value of the range.
* @see @ref location, @ref locationDouble
**/
@dynamic locationDecimal;
/** @property NSDecimal lengthDecimal
* @brief The length of the range.
* @see @ref length, @ref lengthDouble
**/
@dynamic lengthDecimal;
/** @property double locationDouble
* @brief The starting value of the range as a @double.
* @see @ref location, @ref locationDecimal
**/
@dynamic locationDouble;
/** @property double lengthDouble
* @brief The length of the range as a @double.
* @see @ref length, @ref lengthDecimal
**/
@dynamic lengthDouble;
@dynamic inValueUpdate;
#pragma mark -
#pragma mark Combining ranges
/** @brief Extends the range to include another range. The sign of @ref length is unchanged.
* @param other The other plot range.
**/
-(void)unionPlotRange:(CPTPlotRange *)other
{
if ( !other ) {
return;
}
NSDecimal min1 = self.minLimitDecimal;
NSDecimal min2 = other.minLimitDecimal;
NSDecimal minimum = CPTDecimalLessThan(min1, min2) ? min1 : min2;
NSDecimal max1 = self.maxLimitDecimal;
NSDecimal max2 = other.maxLimitDecimal;
NSDecimal maximum = CPTDecimalGreaterThan(max1, max2) ? max1 : max2;
NSDecimal newLocation, newLength;
if ( CPTDecimalGreaterThanOrEqualTo( self.lengthDecimal, CPTDecimalFromInteger(0) ) ) {
newLocation = minimum;
newLength = CPTDecimalSubtract(maximum, minimum);
}
else {
newLocation = maximum;
newLength = CPTDecimalSubtract(minimum, maximum);
}
self.locationDecimal = newLocation;
self.lengthDecimal = newLength;
}
/** @brief Sets the messaged object to the intersection with another range. The sign of @ref length is unchanged.
* @param other The other plot range.
**/
-(void)intersectionPlotRange:(CPTPlotRange *)other
{
if ( !other ) {
return;
}
NSDecimal min1 = self.minLimitDecimal;
NSDecimal min2 = other.minLimitDecimal;
NSDecimal minimum = CPTDecimalGreaterThan(min1, min2) ? min1 : min2;
NSDecimal max1 = self.maxLimitDecimal;
NSDecimal max2 = other.maxLimitDecimal;
NSDecimal maximum = CPTDecimalLessThan(max1, max2) ? max1 : max2;
if ( CPTDecimalGreaterThanOrEqualTo(maximum, minimum) ) {
NSDecimal newLocation, newLength;
if ( CPTDecimalGreaterThanOrEqualTo( self.lengthDecimal, CPTDecimalFromInteger(0) ) ) {
newLocation = minimum;
newLength = CPTDecimalSubtract(maximum, minimum);
}
else {
newLocation = maximum;
newLength = CPTDecimalSubtract(minimum, maximum);
}
self.locationDecimal = newLocation;
self.lengthDecimal = newLength;
}
else {
self.lengthDecimal = CPTDecimalFromInteger(0);
}
}
#pragma mark -
#pragma mark Expanding/Contracting ranges
/** @brief Extends/contracts the range by a given factor.
* @param factor Factor used. A value of @num{1.0} gives no change.
* Less than @num{1.0} is a contraction, and greater than @num{1.0} is expansion.
**/
-(void)expandRangeByFactor:(NSNumber *)factor
{
NSDecimal oldLength = self.lengthDecimal;
NSDecimal newLength = CPTDecimalMultiply(oldLength, factor.decimalValue);
NSDecimal locationOffset = CPTDecimalDivide( CPTDecimalSubtract(oldLength, newLength), CPTDecimalFromInteger(2) );
NSDecimal newLocation = CPTDecimalAdd(self.locationDecimal, locationOffset);
self.locationDecimal = newLocation;
self.lengthDecimal = newLength;
}
#pragma mark -
#pragma mark Shifting Range
/** @brief Moves the whole range so that the @ref location fits in other range.
* @param otherRange Other range.
* The minimum possible shift is made. The range @ref length is unchanged.
**/
-(void)shiftLocationToFitInRange:(CPTPlotRange *)otherRange
{
NSParameterAssert(otherRange);
switch ( [otherRange compareToDecimal:self.locationDecimal] ) {
case CPTPlotRangeComparisonResultNumberBelowRange:
self.locationDecimal = otherRange.minLimitDecimal;
break;
case CPTPlotRangeComparisonResultNumberAboveRange:
self.locationDecimal = otherRange.maxLimitDecimal;
break;
default:
// in range--do nothing
break;
}
}
/** @brief Moves the whole range so that the @ref end point fits in other range.
* @param otherRange Other range.
* The minimum possible shift is made. The range @ref length is unchanged.
**/
-(void)shiftEndToFitInRange:(CPTPlotRange *)otherRange
{
NSParameterAssert(otherRange);
switch ( [otherRange compareToDecimal:self.endDecimal] ) {
case CPTPlotRangeComparisonResultNumberBelowRange:
self.locationDecimal = CPTDecimalSubtract(otherRange.minLimitDecimal, self.lengthDecimal);
break;
case CPTPlotRangeComparisonResultNumberAboveRange:
self.locationDecimal = CPTDecimalSubtract(otherRange.maxLimitDecimal, self.lengthDecimal);
break;
default:
// in range--do nothing
break;
}
}
#pragma mark -
#pragma mark Accessors
/// @cond
-(void)setLocation:(NSNumber *)newLocation
{
self.inValueUpdate = YES;
self.locationDecimal = newLocation.decimalValue;
self.locationDouble = newLocation.doubleValue;
self.inValueUpdate = NO;
}
-(void)setLength:(NSNumber *)newLength
{
self.inValueUpdate = YES;
self.lengthDecimal = newLength.decimalValue;
self.lengthDouble = newLength.doubleValue;
self.inValueUpdate = NO;
}
/// @endcond
@end