forked from djw/core-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPTMutablePlotRange.m
More file actions
160 lines (132 loc) · 4.67 KB
/
CPTMutablePlotRange.m
File metadata and controls
160 lines (132 loc) · 4.67 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
#import "CPTMutablePlotRange.h"
#import "CPTUtilities.h"
/** @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 location
* @brief The starting value of the range.
**/
@dynamic location;
/** @property length
* @brief The length of the range.
**/
@dynamic length;
#pragma mark -
#pragma mark Combining ranges
/** @brief Extends the range to include another range. The sign of <code>length</code> is unchanged.
* @param other The other plot range.
**/
-(void)unionPlotRange:(CPTPlotRange *)other
{
if ( !other ) {
return;
}
NSDecimal min1 = self.minLimit;
NSDecimal min2 = other.minLimit;
NSDecimal minimum = CPTDecimalLessThan(min1, min2) ? min1 : min2;
NSDecimal max1 = self.maxLimit;
NSDecimal max2 = other.maxLimit;
NSDecimal maximum = CPTDecimalGreaterThan(max1, max2) ? max1 : max2;
NSDecimal newLocation, newLength;
if ( CPTDecimalGreaterThanOrEqualTo( self.length, CPTDecimalFromInteger(0) ) ) {
newLocation = minimum;
newLength = CPTDecimalSubtract(maximum, minimum);
}
else {
newLocation = maximum;
newLength = CPTDecimalSubtract(minimum, maximum);
}
self.location = newLocation;
self.length = newLength;
}
/** @brief Sets the messaged object to the intersection with another range. The sign of <code>length</code> is unchanged.
* @param other The other plot range.
**/
-(void)intersectionPlotRange:(CPTPlotRange *)other
{
if ( !other ) {
return;
}
NSDecimal min1 = self.minLimit;
NSDecimal min2 = other.minLimit;
NSDecimal minimum = CPTDecimalGreaterThan(min1, min2) ? min1 : min2;
NSDecimal max1 = self.maxLimit;
NSDecimal max2 = other.maxLimit;
NSDecimal maximum = CPTDecimalLessThan(max1, max2) ? max1 : max2;
if ( CPTDecimalGreaterThanOrEqualTo(maximum, minimum) ) {
NSDecimal newLocation, newLength;
if ( CPTDecimalGreaterThanOrEqualTo( self.length, CPTDecimalFromInteger(0) ) ) {
newLocation = minimum;
newLength = CPTDecimalSubtract(maximum, minimum);
}
else {
newLocation = maximum;
newLength = CPTDecimalSubtract(minimum, maximum);
}
self.location = newLocation;
self.length = newLength;
}
else {
self.length = CPTDecimalFromInteger(0);
}
}
#pragma mark -
#pragma mark Expanding/Contracting ranges
/** @brief Extends/contracts the range by a factor.
* @param factor Factor used. A value of 1.0 gives no change.
* Less than 1.0 is a contraction, and greater than 1.0 is expansion.
**/
-(void)expandRangeByFactor:(NSDecimal)factor
{
NSDecimal oldLength = self.length;
NSDecimal newLength = CPTDecimalMultiply(oldLength, factor);
NSDecimal locationOffset = CPTDecimalDivide( CPTDecimalSubtract(oldLength, newLength), CPTDecimalFromInteger(2) );
NSDecimal newLocation = CPTDecimalAdd(self.location, locationOffset);
self.location = newLocation;
self.length = newLength;
}
#pragma mark -
#pragma mark Shifting Range
/** @brief Moves the whole range so that the location fits in other range.
* @param otherRange Other range.
* The minimum possible shift is made. The range length is unchanged.
**/
-(void)shiftLocationToFitInRange:(CPTPlotRange *)otherRange
{
NSParameterAssert(otherRange);
switch ( [otherRange compareToNumber:[NSDecimalNumber decimalNumberWithDecimal:self.location]] ) {
case CPTPlotRangeComparisonResultNumberBelowRange:
self.location = otherRange.minLimit;
break;
case CPTPlotRangeComparisonResultNumberAboveRange:
self.location = otherRange.maxLimit;
break;
default:
// in range--do nothing
break;
}
}
/** @brief Moves the whole range so that the end point fits in other range.
* @param otherRange Other range.
* The minimum possible shift is made. The range length is unchanged.
**/
-(void)shiftEndToFitInRange:(CPTPlotRange *)otherRange
{
NSParameterAssert(otherRange);
switch ( [otherRange compareToNumber:[NSDecimalNumber decimalNumberWithDecimal:self.end]] ) {
case CPTPlotRangeComparisonResultNumberBelowRange:
self.location = CPTDecimalSubtract(otherRange.minLimit, self.length);
break;
case CPTPlotRangeComparisonResultNumberAboveRange:
self.location = CPTDecimalSubtract(otherRange.maxLimit, self.length);
break;
default:
// in range--do nothing
break;
}
}
@end