forked from core-plot/core-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPTLegendEntry.m
More file actions
250 lines (198 loc) · 6.31 KB
/
CPTLegendEntry.m
File metadata and controls
250 lines (198 loc) · 6.31 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
#import "CPTLegendEntry.h"
#import "CPTPlatformSpecificCategories.h"
#import "CPTPlot.h"
#import "CPTTextStyle.h"
#import "CPTUtilities.h"
#import <tgmath.h>
/// @cond
@interface CPTLegendEntry()
@property (nonatomic, readonly, nullable) NSString *title;
@property (nonatomic, readonly, nullable) NSAttributedString *attributedTitle;
@end
/// @endcond
#pragma mark -
/**
* @brief A graph legend entry.
**/
@implementation CPTLegendEntry
/** @property nullable CPTPlot *plot
* @brief The plot associated with this legend entry.
**/
@synthesize plot;
/** @property NSUInteger index
* @brief The zero-based index of the legend entry for the given plot.
**/
@synthesize index;
/** @property NSUInteger row
* @brief The row number where this entry appears in the legend (first row is @num{0}).
**/
@synthesize row;
/** @property NSUInteger column
* @brief The column number where this entry appears in the legend (first column is @num{0}).
**/
@synthesize column;
/// @cond
/** @property nullable NSString *title
* @brief The legend entry title.
**/
@dynamic title;
/** @property nullable NSAttributedString *attributedTitle
* @brief The legend entry attributed title.
**/
@dynamic attributedTitle;
/// @endcond
/** @property nullable CPTTextStyle *textStyle
* @brief The text style used to draw the legend entry title.
**/
@synthesize textStyle;
/** @property CGSize titleSize
* @brief The size of the legend entry title when drawn using the @ref textStyle.
**/
@dynamic titleSize;
#pragma mark -
#pragma mark Init/Dealloc
/// @name Initialization
/// @{
/** @brief Initializes a newly allocated CPTLegendEntry object.
*
* The initialized object will have the following properties:
* - @ref plot = @nil
* - @link CPTLegendEntry::index index @endlink = @num{0}
* - @ref row = @num{0}
* - @ref column = @num{0}
* - @ref textStyle = @nil
*
* @return The initialized object.
**/
-(nonnull instancetype)init
{
if ( (self = [super init]) ) {
plot = nil;
index = 0;
row = 0;
column = 0;
textStyle = nil;
}
return self;
}
/// @}
#pragma mark -
#pragma mark NSCoding Methods
/// @cond
-(void)encodeWithCoder:(nonnull NSCoder *)coder
{
[coder encodeConditionalObject:self.plot forKey:@"CPTLegendEntry.plot"];
[coder encodeInteger:(NSInteger)self.index forKey:@"CPTLegendEntry.index"];
[coder encodeInteger:(NSInteger)self.row forKey:@"CPTLegendEntry.row"];
[coder encodeInteger:(NSInteger)self.column forKey:@"CPTLegendEntry.column"];
[coder encodeObject:self.textStyle forKey:@"CPTLegendEntry.textStyle"];
}
-(nullable instancetype)initWithCoder:(nonnull NSCoder *)coder
{
if ( (self = [super init]) ) {
plot = [coder decodeObjectOfClass:[CPTPlot class]
forKey:@"CPTLegendEntry.plot"];
index = (NSUInteger)[coder decodeIntegerForKey:@"CPTLegendEntry.index"];
row = (NSUInteger)[coder decodeIntegerForKey:@"CPTLegendEntry.row"];
column = (NSUInteger)[coder decodeIntegerForKey:@"CPTLegendEntry.column"];
textStyle = [coder decodeObjectOfClass:[CPTTextStyle class]
forKey:@"CPTLegendEntry.textStyle"];
}
return self;
}
/// @endcond
#pragma mark -
#pragma mark NSSecureCoding Methods
/// @cond
+(BOOL)supportsSecureCoding
{
return YES;
}
/// @endcond
#pragma mark -
#pragma mark Drawing
/** @brief Draws the legend title centered vertically in the given rectangle.
* @param rect The bounding rectangle where the title should be drawn.
* @param context The graphics context to draw into.
* @param scale The drawing scale factor. Must be greater than zero (@num{0}).
**/
-(void)drawTitleInRect:(CGRect)rect inContext:(nonnull CGContextRef)context scale:(CGFloat)scale
{
#if TARGET_OS_SIMULATOR || TARGET_OS_IPHONE
CGContextSaveGState(context);
CGContextTranslateCTM(context, CPTFloat(0.0), rect.origin.y);
CGContextScaleCTM( context, CPTFloat(1.0), CPTFloat(-1.0) );
CGContextTranslateCTM( context, CPTFloat(0.0), -CGRectGetMaxY(rect) );
#endif
// center the title vertically
CGRect textRect = rect;
CGSize theTitleSize = self.titleSize;
if ( theTitleSize.height < textRect.size.height ) {
CGFloat offset = (textRect.size.height - theTitleSize.height) / CPTFloat(2.0);
if ( scale == CPTFloat(1.0) ) {
offset = round(offset);
}
else {
offset = round(offset * scale) / scale;
}
textRect = CPTRectInset(textRect, 0.0, offset);
}
CPTAlignRectToUserSpace(context, textRect);
NSAttributedString *styledTitle = self.attributedTitle;
if ( (styledTitle.length > 0) && [styledTitle respondsToSelector:@selector(drawInRect:)] ) {
[styledTitle drawInRect:textRect
inContext:context];
}
else {
NSString *theTitle = styledTitle.string;
if ( !theTitle ) {
theTitle = self.title;
}
[theTitle drawInRect:textRect
withTextStyle:self.textStyle
inContext:context];
}
#if TARGET_OS_SIMULATOR || TARGET_OS_IPHONE
CGContextRestoreGState(context);
#endif
}
#pragma mark -
#pragma mark Accessors
/// @cond
-(nullable NSString *)title
{
CPTPlot *thePlot = self.plot;
return [thePlot titleForLegendEntryAtIndex:self.index];
}
-(nullable NSAttributedString *)attributedTitle
{
CPTPlot *thePlot = self.plot;
return [thePlot attributedTitleForLegendEntryAtIndex:self.index];
}
-(CGSize)titleSize
{
CGSize theTitleSize = CGSizeZero;
NSAttributedString *styledTitle = self.attributedTitle;
if ( (styledTitle.length > 0) && [styledTitle respondsToSelector:@selector(size)] ) {
#if TARGET_OS_SIMULATOR || TARGET_OS_IPHONE
theTitleSize = styledTitle.size;
#else
theTitleSize = NSSizeToCGSize(styledTitle.size);
#endif
}
else {
NSString *theTitle = styledTitle.string;
if ( !theTitle ) {
theTitle = self.title;
}
CPTTextStyle *theTextStyle = self.textStyle;
if ( theTitle && theTextStyle ) {
theTitleSize = [theTitle sizeWithTextStyle:theTextStyle];
}
}
theTitleSize.width = ceil(theTitleSize.width);
theTitleSize.height = ceil(theTitleSize.height);
return theTitleSize;
}
/// @endcond
@end