forked from djw/core-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPTAxisLabel.m
315 lines (260 loc) · 8.11 KB
/
CPTAxisLabel.m
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
#import "CPTAxisLabel.h"
#import "CPTExceptions.h"
#import "CPTLayer.h"
#import "CPTMutableTextStyle.h"
#import "CPTTextLayer.h"
#import "CPTUtilities.h"
#import "NSCoderExtensions.h"
#import <tgmath.h>
/** @brief An axis label.
*
* The label can be text-based or can be the content of any CPTLayer provided by the user.
**/
@implementation CPTAxisLabel
/** @property contentLayer
* @brief The label content.
**/
@synthesize contentLayer;
/** @property offset
* @brief The offset distance between the axis and label.
**/
@synthesize offset;
/** @property rotation
* @brief The rotation of the label in radians.
**/
@synthesize rotation;
/** @property alignment
* @brief The alignment of the axis label with respect to the tick mark.
**/
@synthesize alignment;
/** @property tickLocation
* @brief The data coordinate of the ticklocation.
**/
@synthesize tickLocation;
#pragma mark -
#pragma mark Init/Dealloc
/** @brief Initializes a newly allocated text-based CPTAxisLabel object with the provided text and style.
*
* @param newText The label text.
* @param newStyle The text style for the label.
* @return The initialized CPTAxisLabel object.
**/
-(id)initWithText:(NSString *)newText textStyle:(CPTTextStyle *)newStyle
{
CPTTextLayer *newLayer = [[CPTTextLayer alloc] initWithText:newText style:newStyle];
self = [self initWithContentLayer:newLayer];
[newLayer release];
return self;
}
/** @brief Initializes a newly allocated CPTAxisLabel object with the provided layer. This is the designated initializer.
*
* @param layer The label content.
* @return The initialized CPTAxisLabel object.
**/
-(id)initWithContentLayer:(CPTLayer *)layer
{
if ( layer ) {
if ( (self = [super init]) ) {
contentLayer = [layer retain];
offset = 20.0;
rotation = 0.0;
alignment = CPTAlignmentCenter;
tickLocation = CPTDecimalFromInteger(0);
}
}
else {
[self release];
self = nil;
}
return self;
}
-(void)dealloc
{
[contentLayer release];
[super dealloc];
}
#pragma mark -
#pragma mark NSCoding methods
-(void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:self.contentLayer forKey:@"CPTAxisLabel.contentLayer"];
[coder encodeCGFloat:self.offset forKey:@"CPTAxisLabel.offset"];
[coder encodeCGFloat:self.rotation forKey:@"CPTAxisLabel.rotation"];
[coder encodeInteger:self.alignment forKey:@"CPTAxisLabel.alignment"];
[coder encodeDecimal:self.tickLocation forKey:@"CPTAxisLabel.tickLocation"];
}
-(id)initWithCoder:(NSCoder *)coder
{
if ( (self = [super init]) ) {
contentLayer = [[coder decodeObjectForKey:@"CPTAxisLabel.contentLayer"] retain];
offset = [coder decodeCGFloatForKey:@"CPTAxisLabel.offset"];
rotation = [coder decodeCGFloatForKey:@"CPTAxisLabel.rotation"];
alignment = [coder decodeIntegerForKey:@"CPTAxisLabel.alignment"];
tickLocation = [coder decodeDecimalForKey:@"CPTAxisLabel.tickLocation"];
}
return self;
}
#pragma mark -
#pragma mark Layout
/** @brief Positions the axis label relative to the given point.
* The algorithm for positioning is different when the rotation property is non-zero.
* When zero, the anchor point is positioned along the closest side of the label.
* When non-zero, the anchor point is left at the center. This has consequences for
* the value taken by the offset.
* @param point The view point.
* @param coordinate The coordinate in which the label is being position. Orthogonal to the axis coordinate.
* @param direction The offset direction.
**/
-(void)positionRelativeToViewPoint:(CGPoint)point forCoordinate:(CPTCoordinate)coordinate inDirection:(CPTSign)direction
{
CPTLayer *content = self.contentLayer;
if ( !content ) {
return;
}
CGPoint newPosition = point;
CGFloat *value = ( coordinate == CPTCoordinateX ? &(newPosition.x) : &(newPosition.y) );
CGFloat angle = 0.0;
CGFloat myRotation = self.rotation;
content.transform = CATransform3DMakeRotation(myRotation, 0.0, 0.0, 1.0);
CGRect contentFrame = content.frame;
// Position the anchor point along the closest edge.
switch ( direction ) {
case CPTSignNone:
case CPTSignNegative:
*value -= self.offset;
switch ( coordinate ) {
case CPTCoordinateX:
angle = M_PI;
switch ( self.alignment ) {
case CPTAlignmentBottom:
newPosition.y += contentFrame.size.height / (CGFloat)2.0;
break;
case CPTAlignmentTop:
newPosition.y -= contentFrame.size.height / (CGFloat)2.0;
break;
default: // middle
// no adjustment
break;
}
break;
case CPTCoordinateY:
angle = -M_PI_2;
switch ( self.alignment ) {
case CPTAlignmentLeft:
newPosition.x += contentFrame.size.width / (CGFloat)2.0;
break;
case CPTAlignmentRight:
newPosition.x -= contentFrame.size.width / (CGFloat)2.0;
break;
default: // center
// no adjustment
break;
}
break;
default:
[NSException raise:NSInvalidArgumentException format:@"Invalid coordinate in positionRelativeToViewPoint:forCoordinate:inDirection:"];
break;
}
break;
case CPTSignPositive:
*value += self.offset;
switch ( coordinate ) {
case CPTCoordinateX:
// angle = 0.0;
switch ( self.alignment ) {
case CPTAlignmentBottom:
newPosition.y += contentFrame.size.height / (CGFloat)2.0;
break;
case CPTAlignmentTop:
newPosition.y -= contentFrame.size.height / (CGFloat)2.0;
break;
default: // middle
// no adjustment
break;
}
break;
case CPTCoordinateY:
angle = M_PI_2;
switch ( self.alignment ) {
case CPTAlignmentLeft:
newPosition.x += contentFrame.size.width / (CGFloat)2.0;
break;
case CPTAlignmentRight:
newPosition.x -= contentFrame.size.width / (CGFloat)2.0;
break;
default: // center
// no adjustment
break;
}
break;
default:
[NSException raise:NSInvalidArgumentException format:@"Invalid coordinate in positionRelativeToViewPoint:forCoordinate:inDirection:"];
break;
}
break;
default:
[NSException raise:NSInvalidArgumentException format:@"Invalid direction in positionRelativeToViewPoint:forCoordinate:inDirection:"];
break;
}
angle += M_PI;
angle -= myRotation;
CGFloat newAnchorX = cos(angle);
CGFloat newAnchorY = sin(angle);
if ( ABS(newAnchorX) <= ABS(newAnchorY) ) {
newAnchorX /= ABS(newAnchorY);
newAnchorY = signbit(newAnchorY) ? -1.0 : 1.0;
}
else {
newAnchorY /= ABS(newAnchorX);
newAnchorX = signbit(newAnchorX) ? -1.0 : 1.0;
}
CGPoint anchor = CGPointMake( (newAnchorX + (CGFloat)1.0) / (CGFloat)2.0, (newAnchorY + (CGFloat)1.0) / (CGFloat)2.0 );
content.anchorPoint = anchor;
content.position = newPosition;
[content pixelAlign];
[content setNeedsDisplay];
}
/** @brief Positions the axis label between two given points.
* @param firstPoint The first view point.
* @param secondPoint The second view point.
* @param coordinate The axis coordinate.
* @param direction The offset direction.
**/
-(void)positionBetweenViewPoint:(CGPoint)firstPoint andViewPoint:(CGPoint)secondPoint forCoordinate:(CPTCoordinate)coordinate inDirection:(CPTSign)direction
{
[self positionRelativeToViewPoint:CGPointMake( (firstPoint.x + secondPoint.x) / (CGFloat)2.0, (firstPoint.y + secondPoint.y) / (CGFloat)2.0 )
forCoordinate:coordinate
inDirection:direction];
}
#pragma mark -
#pragma mark Description
-(NSString *)description
{
return [NSString stringWithFormat:@"<%@ {%@}>", [super description], self.contentLayer];
}
#pragma mark -
#pragma mark Label comparison
// Axis labels are equal if they have the same location
-(BOOL)isEqual:(id)object
{
if ( self == object ) {
return YES;
}
else if ( [object isKindOfClass:[self class]] ) {
return CPTDecimalEquals(self.tickLocation, ( (CPTAxisLabel *)object ).tickLocation);
}
else {
return NO;
}
}
-(NSUInteger)hash
{
NSUInteger hashValue = 0;
// Equal objects must hash the same.
double tickLocationAsDouble = CPTDecimalDoubleValue(self.tickLocation);
if ( !isnan(tickLocationAsDouble) ) {
hashValue = (NSUInteger)fmod(ABS(tickLocationAsDouble), (double)NSUIntegerMax);
}
return hashValue;
}
@end