-
Notifications
You must be signed in to change notification settings - Fork 598
Expand file tree
/
Copy pathCPTGraph.m
More file actions
1389 lines (1180 loc) · 43.5 KB
/
CPTGraph.m
File metadata and controls
1389 lines (1180 loc) · 43.5 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import "CPTGraph.h"
#import "CPTAxis.h"
#import "CPTAxisSet.h"
#import "CPTExceptions.h"
#import "CPTGraphHostingView.h"
#import "CPTLayerAnnotation.h"
#import "CPTLegend.h"
#import "CPTMutableTextStyle.h"
#import "CPTPlotArea.h"
#import "CPTPlotAreaFrame.h"
#import "CPTPlotGroup.h"
#import "CPTTextLayer.h"
#import "CPTTheme.h"
#import "NSCoderExtensions.h"
/** @defgroup graphAnimation Graphs
* @brief Graph properties that can be animated using Core Animation.
* @if MacOnly
* @since Custom layer property animation is supported on macOS 10.6 and later.
* @endif
* @ingroup animation
**/
CPTGraphNotification const CPTGraphNeedsRedrawNotification = @"CPTGraphNeedsRedrawNotification";
CPTGraphNotification const CPTGraphDidAddPlotSpaceNotification = @"CPTGraphDidAddPlotSpaceNotification";
CPTGraphNotification const CPTGraphDidRemovePlotSpaceNotification = @"CPTGraphDidRemovePlotSpaceNotification";
CPTGraphPlotSpaceKey const CPTGraphPlotSpaceNotificationKey = @"CPTGraphPlotSpaceNotificationKey";
/// @cond
@interface CPTGraph()
@property (nonatomic, readwrite, strong, nonnull) CPTMutablePlotArray *plots;
@property (nonatomic, readwrite, strong, nonnull) CPTMutablePlotSpaceArray *plotSpaces;
@property (nonatomic, readwrite, strong, nullable) CPTLayerAnnotation *titleAnnotation;
@property (nonatomic, readwrite, strong, nullable) CPTLayerAnnotation *legendAnnotation;
@property (nonatomic, readwrite, assign) BOOL inTitleUpdate;
-(void)plotSpaceMappingDidChange:(nonnull NSNotification *)notif;
-(CGPoint)contentAnchorForRectAnchor:(CPTRectAnchor)anchor;
@end
/// @endcond
#pragma mark -
/** @brief An abstract graph class.
*
* The graph is the top-level view container for a Core Plot graph. It should be hosted
* inside a CPTGraphHostingView.
*
* A graph contains a single plot area frame (CPTPlotAreaFrame), which is the container for all of the
* graph elements. Use the padding properties on the graph to position the plot area frame
* inside the graph. All graph elements, except for titles, legends, and other annotations
* attached directly to the graph itself are clipped to the plot area frame.
*
* The plot area (CPTPlotArea) sits inside the plot area frame. Use the padding properties on the plot
* area frame to position the plot area inside the plot area frame.
* All plots are drawn inside this area while axes, titles, and borders may fall outside.
* Use a plot space (CPTPlotSpace) to control the mapping between data values and
* plotting coordinates in the plot area. One plot space is automatically provided with
* a new graph; more may be added as needed.
*
* @see See @ref graphAnimation "Graphs" for a list of animatable properties.
**/
@implementation CPTGraph
/** @property nullable CPTGraphHostingView *hostingView
* @brief The hosting view that contains the graph.
**/
@synthesize hostingView;
/** @property nullable CPTAxisSet *axisSet
* @brief The axis set.
**/
@dynamic axisSet;
/** @property nullable CPTPlotAreaFrame *plotAreaFrame
* @brief The plot area frame.
**/
@synthesize plotAreaFrame;
/// @cond
/** @property nonnull CPTMutablePlotArray *plots
* @brief An array of all plots associated with the graph.
**/
@synthesize plots;
/** @property nonnull CPTMutablePlotSpaceArray *plotSpaces
* @brief An array of all plot spaces associated with the graph.
**/
@synthesize plotSpaces;
/// @endcond
/** @property nullable CPTPlotSpace *defaultPlotSpace
* @brief The default plot space.
**/
@dynamic defaultPlotSpace;
/** @property nullable CPTNumberArray *topDownLayerOrder
* @brief An array of graph layers to be drawn in an order other than the default.
* @see CPTPlotArea @link CPTPlotArea::topDownLayerOrder topDownLayerOrder @endlink property.
**/
@dynamic topDownLayerOrder;
/** @property nullable NSString *title
* @brief The title string.
*
* Assigning a new value to this property also sets the value of the @ref attributedTitle property to @nil.
**/
@synthesize title;
/** @property nullable NSAttributedString *attributedTitle
* @brief The styled title string.
*
* Assigning a new value to this property also sets the value of the @ref title property to the
* same string without formatting information. It also replaces the @ref titleTextStyle with
* a style matching the first position (location @num{0}) of the styled title.
* Default is @nil.
**/
@synthesize attributedTitle;
/** @property nullable CPTTextStyle *titleTextStyle
* @brief The text style of the title.
*
* Assigning a new value to this property also sets the value of the @ref attributedTitle property to @nil.
**/
@synthesize titleTextStyle;
/** @property CPTRectAnchor titlePlotAreaFrameAnchor
* @brief The location of the title with respect to the plot area frame.
* Default is #CPTRectAnchorTop.
**/
@synthesize titlePlotAreaFrameAnchor;
/** @property CGPoint titleDisplacement
* @brief A vector giving the displacement of the title from the edge location.
* @ingroup graphAnimation
**/
@synthesize titleDisplacement;
/** @property nullable CPTLegend *legend
* @brief The graph legend.
* Setting this property will automatically anchor the legend to the graph and position it
* using the @ref legendAnchor and @ref legendDisplacement properties. This is a convenience property
* only—the legend may be inserted in the layer tree and positioned like any other CPTLayer
* if more flexibility is needed.
**/
@synthesize legend;
/** @property CPTRectAnchor legendAnchor
* @brief The location of the legend with respect to the graph frame.
* Default is #CPTRectAnchorBottom.
**/
@synthesize legendAnchor;
/** @property CGPoint legendDisplacement
* @brief A vector giving the displacement of the legend from the edge location.
* @ingroup graphAnimation
**/
@synthesize legendDisplacement;
@synthesize titleAnnotation;
@synthesize legendAnnotation;
@synthesize inTitleUpdate;
#pragma mark -
#pragma mark Init/Dealloc
/// @name Initialization
/// @{
/** @brief Initializes a newly allocated CPTGraph object with the provided frame rectangle.
*
* This is the designated initializer. The initialized layer will have the following properties:
* - @ref hostingView = @nil
* - @ref title = @nil
* - @ref attributedTitle = @nil
* - @ref titlePlotAreaFrameAnchor = #CPTRectAnchorTop
* - @ref titleTextStyle = default text style
* - @ref titleDisplacement = (@num{0.0}, @num{0.0})
* - @ref legend = @nil
* - @ref legendAnchor = #CPTRectAnchorBottom
* - @ref legendDisplacement = (@num{0.0}, @num{0.0})
* - @ref paddingLeft = @num{20.0}
* - @ref paddingTop = @num{20.0}
* - @ref paddingRight = @num{20.0}
* - @ref paddingBottom = @num{20.0}
* - @ref needsDisplayOnBoundsChange = @YES
*
* The new graph will have a @ref plotAreaFrame with a frame equal to the graph’s bounds,
* a @ref defaultPlotSpace created with the @link CPTGraph::newPlotSpace -newPlotSpace @endlink method,
* and an @ref axisSet created with the @link CPTGraph::newAxisSet -newAxisSet @endlink method.
*
* @param newFrame The frame rectangle.
* @return The initialized CPTGraph object.
**/
-(nonnull instancetype)initWithFrame:(CGRect)newFrame
{
if ((self = [super initWithFrame:newFrame])) {
hostingView = nil;
plots = [[NSMutableArray alloc] init];
// Margins
self.paddingLeft = CPTFloat(20.0);
self.paddingTop = CPTFloat(20.0);
self.paddingRight = CPTFloat(20.0);
self.paddingBottom = CPTFloat(20.0);
// Plot area
CPTPlotAreaFrame *newArea = [[CPTPlotAreaFrame alloc] initWithFrame:self.bounds];
self.plotAreaFrame = newArea;
// Plot spaces
plotSpaces = [[NSMutableArray alloc] init];
CPTPlotSpace *newPlotSpace = [self newPlotSpace];
[self addPlotSpace:newPlotSpace];
// Axis set
CPTAxisSet *newAxisSet = [self newAxisSet];
self.axisSet = newAxisSet;
// Title
title = nil;
attributedTitle = nil;
titlePlotAreaFrameAnchor = CPTRectAnchorTop;
titleTextStyle = [CPTTextStyle textStyle];
titleDisplacement = CGPointZero;
titleAnnotation = nil;
// Legend
legend = nil;
legendAnnotation = nil;
legendAnchor = CPTRectAnchorBottom;
legendDisplacement = CGPointZero;
inTitleUpdate = NO;
self.needsDisplayOnBoundsChange = YES;
}
return self;
}
/// @}
/// @cond
-(nonnull instancetype)initWithLayer:(nonnull id)layer
{
if ((self = [super initWithLayer:layer])) {
CPTGraph *theLayer = (CPTGraph *)layer;
hostingView = theLayer->hostingView;
plotAreaFrame = theLayer->plotAreaFrame;
plots = theLayer->plots;
plotSpaces = theLayer->plotSpaces;
title = theLayer->title;
attributedTitle = theLayer->attributedTitle;
titlePlotAreaFrameAnchor = theLayer->titlePlotAreaFrameAnchor;
titleTextStyle = theLayer->titleTextStyle;
titleDisplacement = theLayer->titleDisplacement;
titleAnnotation = theLayer->titleAnnotation;
legend = theLayer->legend;
legendAnnotation = theLayer->legendAnnotation;
legendAnchor = theLayer->legendAnchor;
legendDisplacement = theLayer->legendDisplacement;
inTitleUpdate = theLayer->inTitleUpdate;
}
return self;
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
/// @endcond
#pragma mark -
#pragma mark NSCoding Methods
/// @cond
-(void)encodeWithCoder:(nonnull NSCoder *)coder
{
[super encodeWithCoder:coder];
[coder encodeConditionalObject:self.hostingView forKey:@"CPTGraph.hostingView"];
[coder encodeObject:self.plotAreaFrame forKey:@"CPTGraph.plotAreaFrame"];
[coder encodeObject:self.plots forKey:@"CPTGraph.plots"];
[coder encodeObject:self.plotSpaces forKey:@"CPTGraph.plotSpaces"];
[coder encodeObject:self.title forKey:@"CPTGraph.title"];
[coder encodeObject:self.attributedTitle forKey:@"CPTGraph.attributedTitle"];
[coder encodeObject:self.titleTextStyle forKey:@"CPTGraph.titleTextStyle"];
[coder encodeInteger:self.titlePlotAreaFrameAnchor forKey:@"CPTGraph.titlePlotAreaFrameAnchor"];
[coder encodeCPTPoint:self.titleDisplacement forKey:@"CPTGraph.titleDisplacement"];
[coder encodeObject:self.titleAnnotation forKey:@"CPTGraph.titleAnnotation"];
[coder encodeObject:self.legend forKey:@"CPTGraph.legend"];
[coder encodeObject:self.legendAnnotation forKey:@"CPTGraph.legendAnnotation"];
[coder encodeInteger:self.legendAnchor forKey:@"CPTGraph.legendAnchor"];
[coder encodeCPTPoint:self.legendDisplacement forKey:@"CPTGraph.legendDisplacement"];
// No need to archive these properties:
// inTitleUpdate
}
-(nullable instancetype)initWithCoder:(nonnull NSCoder *)coder
{
if ((self = [super initWithCoder:coder])) {
hostingView = [coder decodeObjectOfClass:[CPTGraphHostingView class]
forKey:@"CPTGraph.hostingView"];
plotAreaFrame = [coder decodeObjectOfClass:[CPTPlotAreaFrame class]
forKey:@"CPTGraph.plotAreaFrame"];
CPTPlotArray *plotArray = [coder decodeObjectOfClasses:[NSSet setWithArray:@[[NSArray class], [CPTPlot class]]]
forKey:@"CPTGraph.plots"];
if ( plotArray ) {
plots = [plotArray mutableCopy];
}
else {
plots = [[NSMutableArray alloc] init];
}
plotSpaces = [[NSMutableArray alloc] init];
CPTPlotSpaceArray *plotSpaceArray = [coder decodeObjectOfClasses:[NSSet setWithArray:@[[NSArray class], [CPTPlotSpace class]]]
forKey:@"CPTGraph.plotSpaces"];
if ( plotSpaceArray ) {
for ( CPTPlotSpace *space in plotSpaceArray ) {
[self addPlotSpace:space];
}
}
title = [[coder decodeObjectOfClass:[NSString class]
forKey:@"CPTGraph.title"] copy];
attributedTitle = [[coder decodeObjectOfClass:[NSAttributedString class]
forKey:@"CPTGraph.attributedTitle"] copy];
titleTextStyle = [[coder decodeObjectOfClass:[CPTTextStyle class]
forKey:@"CPTGraph.titleTextStyle"] copy];
titlePlotAreaFrameAnchor = (CPTRectAnchor)[coder decodeIntegerForKey:@"CPTGraph.titlePlotAreaFrameAnchor"];
titleDisplacement = [coder decodeCPTPointForKey:@"CPTGraph.titleDisplacement"];
titleAnnotation = [coder decodeObjectOfClass:[CPTLayerAnnotation class]
forKey:@"CPTGraph.titleAnnotation"];
legend = [coder decodeObjectOfClass:[CPTLegend class]
forKey:@"CPTGraph.legend"];
legendAnnotation = [coder decodeObjectOfClass:[CPTLayerAnnotation class]
forKey:@"CPTGraph.legendAnnotation"];
legendAnchor = (CPTRectAnchor)[coder decodeIntegerForKey:@"CPTGraph.legendAnchor"];
legendDisplacement = [coder decodeCPTPointForKey:@"CPTGraph.legendDisplacement"];
inTitleUpdate = NO;
}
return self;
}
/// @endcond
#pragma mark -
#pragma mark NSSecureCoding Methods
/// @cond
+(BOOL)supportsSecureCoding
{
return YES;
}
/// @endcond
#pragma mark -
#pragma mark Drawing
/// @cond
-(void)layoutAndRenderInContext:(nonnull CGContextRef)context
{
[self reloadDataIfNeeded];
[self.axisSet.axes makeObjectsPerformSelector:@selector(relabel)];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
#if TARGET_OS_OSX
// Workaround since @available macro is not there
if ( [NSView instancesRespondToSelector:@selector(effectiveAppearance)] ) {
NSAppearance *oldAppearance = NSAppearance.currentAppearance;
NSView *view = (NSView *)self.hostingView;
NSAppearance.currentAppearance = view.effectiveAppearance;
[super layoutAndRenderInContext:context];
NSAppearance.currentAppearance = oldAppearance;
}
else {
[super layoutAndRenderInContext:context];
}
#else
if ( @available(iOS 13, *)) {
if ( [UITraitCollection instancesRespondToSelector:@selector(performAsCurrentTraitCollection:)] ) {
UITraitCollection *traitCollection = ((UIView *)self.hostingView).traitCollection;
if ( traitCollection ) {
[traitCollection performAsCurrentTraitCollection: ^{
[super layoutAndRenderInContext:context];
}];
}
else {
[super layoutAndRenderInContext:context];
}
}
else {
[super layoutAndRenderInContext:context];
}
}
else {
[super layoutAndRenderInContext:context];
}
#endif
#pragma clang diagnostic pop
}
/// @endcond
#pragma mark -
#pragma mark Animation
/// @cond
+(BOOL)needsDisplayForKey:(nonnull NSString *)aKey
{
static NSSet<NSString *> *keys = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
keys = [NSSet setWithArray:@[@"titleDisplacement",
@"legendDisplacement"]];
});
if ( [keys containsObject:aKey] ) {
return YES;
}
else {
return [super needsDisplayForKey:aKey];
}
}
/// @endcond
#pragma mark -
#pragma mark Retrieving Plots
/**
* @brief Makes all plots reload their data.
**/
-(void)reloadData
{
[self.plots makeObjectsPerformSelector:@selector(reloadData)];
[self.plotSpaces makeObjectsPerformSelector:@selector(removeAllCategories)];
}
/**
* @brief Makes all plots reload their data if their data cache is out of date.
**/
-(void)reloadDataIfNeeded
{
[self.plots makeObjectsPerformSelector:@selector(reloadDataIfNeeded)];
}
/** @brief All plots associated with the graph.
* @return An array of all plots associated with the graph.
**/
-(nonnull CPTPlotArray *)allPlots
{
return [NSArray arrayWithArray:self.plots];
}
/** @brief Gets the plot at the given index in the plot array.
* @param idx An index within the bounds of the plot array.
* @return The plot at the given index.
**/
-(nullable CPTPlot *)plotAtIndex:(NSUInteger)idx
{
if ( idx < self.plots.count ) {
return (self.plots)[idx];
}
else {
return nil;
}
}
/** @brief Gets the plot with the given identifier from the plot array.
* @param identifier A plot identifier.
* @return The plot with the given identifier or @nil if it was not found.
**/
-(nullable CPTPlot *)plotWithIdentifier:(nullable id<NSCopying>)identifier
{
for ( CPTPlot *plot in self.plots ) {
if ( [plot.identifier isEqual:identifier] ) {
return plot;
}
}
return nil;
}
#pragma mark -
#pragma mark Organizing Plots
/** @brief Add a plot to the default plot space.
* @param plot The plot.
**/
-(void)addPlot:(nonnull CPTPlot *)plot
{
[self addPlot:plot toPlotSpace:self.defaultPlotSpace];
}
/** @brief Add a plot to the given plot space.
* @param plot The plot.
* @param space The plot space.
**/
-(void)addPlot:(nonnull CPTPlot *)plot toPlotSpace:(nullable CPTPlotSpace *)space
{
if ( plot ) {
[self.plots addObject:plot];
plot.plotSpace = space;
plot.graph = self;
[self.plotAreaFrame.plotGroup addPlot:plot];
}
}
/** @brief Remove a plot from the graph.
* @param plot The plot to remove.
**/
-(void)removePlot:(nullable CPTPlot *)plot
{
if ( plot ) {
CPTPlot *thePlot = plot;
if ( [self.plots containsObject:thePlot] ) {
thePlot.plotSpace = nil;
thePlot.graph = nil;
[self.plotAreaFrame.plotGroup removePlot:thePlot];
[self.plots removeObject:thePlot];
}
else {
[NSException raise:CPTException format:@"Tried to remove CPTPlot which did not exist."];
}
}
}
/** @brief Add a plot to the default plot space at the given index in the plot array.
* @param plot The plot.
* @param idx An index within the bounds of the plot array.
**/
-(void)insertPlot:(nonnull CPTPlot *)plot atIndex:(NSUInteger)idx
{
[self insertPlot:plot atIndex:idx intoPlotSpace:self.defaultPlotSpace];
}
/** @brief Add a plot to the given plot space at the given index in the plot array.
* @param plot The plot.
* @param idx An index within the bounds of the plot array.
* @param space The plot space.
**/
-(void)insertPlot:(nonnull CPTPlot *)plot atIndex:(NSUInteger)idx intoPlotSpace:(nullable CPTPlotSpace *)space
{
if ( plot ) {
[self.plots insertObject:plot atIndex:idx];
plot.plotSpace = space;
plot.graph = self;
[self.plotAreaFrame.plotGroup insertPlot:plot atIndex:idx];
}
}
/** @brief Remove a plot from the graph.
* @param identifier The identifier of the plot to remove.
**/
-(void)removePlotWithIdentifier:(nullable id<NSCopying>)identifier
{
CPTPlot *plotToRemove = [self plotWithIdentifier:identifier];
if ( plotToRemove ) {
plotToRemove.plotSpace = nil;
plotToRemove.graph = nil;
[self.plotAreaFrame.plotGroup removePlot:plotToRemove];
[self.plots removeObjectIdenticalTo:plotToRemove];
}
}
#pragma mark -
#pragma mark Retrieving Plot Spaces
-(nullable CPTPlotSpace *)defaultPlotSpace
{
return self.plotSpaces.count > 0 ? (self.plotSpaces)[0] : nil;
}
/** @brief All plot spaces associated with the graph.
* @return An array of all plot spaces associated with the graph.
**/
-(nonnull CPTPlotSpaceArray *)allPlotSpaces
{
return [NSArray arrayWithArray:self.plotSpaces];
}
/** @brief Gets the plot space at the given index in the plot space array.
* @param idx An index within the bounds of the plot space array.
* @return The plot space at the given index.
**/
-(nullable CPTPlotSpace *)plotSpaceAtIndex:(NSUInteger)idx
{
return self.plotSpaces.count > idx ? (self.plotSpaces)[idx] : nil;
}
/** @brief Gets the plot space with the given identifier from the plot space array.
* @param identifier A plot space identifier.
* @return The plot space with the given identifier or @nil if it was not found.
**/
-(nullable CPTPlotSpace *)plotSpaceWithIdentifier:(nullable id<NSCopying>)identifier
{
for ( CPTPlotSpace *plotSpace in self.plotSpaces ) {
if ( [plotSpace.identifier isEqual:identifier] ) {
return plotSpace;
}
}
return nil;
}
#pragma mark -
#pragma mark Set Plot Area
/// @cond
-(void)setPlotAreaFrame:(nullable CPTPlotAreaFrame *)newArea
{
if ( plotAreaFrame != newArea ) {
plotAreaFrame.graph = nil;
[plotAreaFrame removeFromSuperlayer];
plotAreaFrame = newArea;
if ( newArea ) {
CPTPlotAreaFrame *theFrame = newArea;
[self addSublayer:theFrame];
theFrame.graph = self;
}
for ( CPTPlotSpace *space in self.plotSpaces ) {
space.graph = self;
}
}
}
/// @endcond
#pragma mark -
#pragma mark Organizing Plot Spaces
/** @brief Add a plot space to the graph.
* @param space The plot space.
**/
-(void)addPlotSpace:(nonnull CPTPlotSpace *)space
{
NSParameterAssert(space);
[self.plotSpaces addObject:space];
space.graph = self;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(plotSpaceMappingDidChange:)
name:CPTPlotSpaceCoordinateMappingDidChangeNotification
object:space];
[[NSNotificationCenter defaultCenter] postNotificationName:CPTGraphDidAddPlotSpaceNotification
object:self
userInfo:@{ CPTGraphPlotSpaceNotificationKey: space }
];
}
/** @brief Remove a plot space from the graph.
* @param plotSpace The plot space.
**/
-(void)removePlotSpace:(nullable CPTPlotSpace *)plotSpace
{
if ( plotSpace ) {
CPTPlotSpace *thePlotSpace = plotSpace;
if ( [self.plotSpaces containsObject:thePlotSpace] ) {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:CPTPlotSpaceCoordinateMappingDidChangeNotification
object:thePlotSpace];
// Remove space
thePlotSpace.graph = nil;
[self.plotSpaces removeObject:thePlotSpace];
// Update axes that referenced space
for ( CPTAxis *axis in self.axisSet.axes ) {
if ( axis.plotSpace == thePlotSpace ) {
axis.plotSpace = nil;
}
}
[[NSNotificationCenter defaultCenter] postNotificationName:CPTGraphDidRemovePlotSpaceNotification
object:self
userInfo:@{ CPTGraphPlotSpaceNotificationKey: thePlotSpace }
];
}
else {
[NSException raise:CPTException format:@"Tried to remove CPTPlotSpace which did not exist."];
}
}
}
#pragma mark -
#pragma mark Coordinate Changes in Plot Spaces
/// @cond
-(void)plotSpaceMappingDidChange:(nonnull NSNotification *)notif
{
CPTPlotSpace *plotSpace = notif.object;
BOOL backgroundBandsNeedRedraw = NO;
for ( CPTAxis *axis in self.axisSet.axes ) {
if ( axis.plotSpace == plotSpace ) {
[axis setNeedsRelabel];
[axis updateAxisTitle];
if ( !backgroundBandsNeedRedraw ) {
backgroundBandsNeedRedraw = (axis.alternatingBandFills.count > 0) || (axis.backgroundLimitBands.count > 0);
}
}
}
for ( CPTPlot *plot in self.plots ) {
if ( plot.plotSpace == plotSpace ) {
[plot setNeedsDisplay];
}
}
if ( backgroundBandsNeedRedraw ) {
[self.plotAreaFrame.plotArea setNeedsDisplay];
}
}
/// @endcond
#pragma mark -
#pragma mark Axis Set
/// @cond
-(nullable CPTAxisSet *)axisSet
{
return self.plotAreaFrame.axisSet;
}
-(void)setAxisSet:(nullable CPTAxisSet *)newSet
{
self.plotAreaFrame.axisSet = newSet;
}
/// @endcond
#pragma mark -
#pragma mark Themes
/** @brief Apply a theme to style the graph.
* @param theme The theme object used to style the graph.
**/
-(void)applyTheme:(nullable CPTTheme *)theme
{
[theme applyThemeToGraph:self];
}
#pragma mark -
#pragma mark Legend
/// @cond
-(void)setLegend:(nullable CPTLegend *)newLegend
{
if ( newLegend != legend ) {
legend = newLegend;
CPTLayerAnnotation *theLegendAnnotation = self.legendAnnotation;
if ( legend ) {
if ( theLegendAnnotation ) {
theLegendAnnotation.contentLayer = legend;
}
else {
CPTLayerAnnotation *newLegendAnnotation = [[CPTLayerAnnotation alloc] initWithAnchorLayer:self];
newLegendAnnotation.contentLayer = legend;
newLegendAnnotation.displacement = self.legendDisplacement;
newLegendAnnotation.rectAnchor = self.legendAnchor;
newLegendAnnotation.contentAnchorPoint = [self contentAnchorForRectAnchor:self.legendAnchor];
[self addAnnotation:newLegendAnnotation];
self.legendAnnotation = newLegendAnnotation;
}
}
else {
if ( theLegendAnnotation ) {
[self removeAnnotation:theLegendAnnotation];
self.legendAnnotation = nil;
}
}
}
}
-(void)setLegendAnchor:(CPTRectAnchor)newLegendAnchor
{
if ( newLegendAnchor != legendAnchor ) {
legendAnchor = newLegendAnchor;
CPTLayerAnnotation *theLegendAnnotation = self.legendAnnotation;
if ( theLegendAnnotation ) {
theLegendAnnotation.rectAnchor = newLegendAnchor;
theLegendAnnotation.contentAnchorPoint = [self contentAnchorForRectAnchor:self.legendAnchor];
}
}
}
-(void)setLegendDisplacement:(CGPoint)newLegendDisplacement
{
if ( !CGPointEqualToPoint(newLegendDisplacement, legendDisplacement)) {
legendDisplacement = newLegendDisplacement;
self.legendAnnotation.displacement = newLegendDisplacement;
}
}
-(CGPoint)contentAnchorForRectAnchor:(CPTRectAnchor)anchor
{
CGPoint contentAnchor = CGPointZero;
switch ( anchor ) {
case CPTRectAnchorBottomLeft:
contentAnchor = CGPointZero;
break;
case CPTRectAnchorBottom:
contentAnchor = CPTPointMake(0.5, 0.0);
break;
case CPTRectAnchorBottomRight:
contentAnchor = CPTPointMake(1.0, 0.0);
break;
case CPTRectAnchorLeft:
contentAnchor = CPTPointMake(0.0, 0.5);
break;
case CPTRectAnchorRight:
contentAnchor = CPTPointMake(1.0, 0.5);
break;
case CPTRectAnchorTopLeft:
contentAnchor = CPTPointMake(0.0, 1.0);
break;
case CPTRectAnchorTop:
contentAnchor = CPTPointMake(0.5, 1.0);
break;
case CPTRectAnchorTopRight:
contentAnchor = CPTPointMake(1.0, 1.0);
break;
case CPTRectAnchorCenter:
contentAnchor = CPTPointMake(0.5, 0.5);
break;
}
return contentAnchor;
}
/// @endcond
#pragma mark -
#pragma mark Accessors
/// @cond
-(void)setPaddingLeft:(CGFloat)newPadding
{
if ( newPadding != self.paddingLeft ) {
super.paddingLeft = newPadding;
[self.axisSet.axes makeObjectsPerformSelector:@selector(setNeedsDisplay)];
}
}
-(void)setPaddingRight:(CGFloat)newPadding
{
if ( newPadding != self.paddingRight ) {
super.paddingRight = newPadding;
[self.axisSet.axes makeObjectsPerformSelector:@selector(setNeedsDisplay)];
}
}
-(void)setPaddingTop:(CGFloat)newPadding
{
if ( newPadding != self.paddingTop ) {
super.paddingTop = newPadding;
[self.axisSet.axes makeObjectsPerformSelector:@selector(setNeedsDisplay)];
}
}
-(void)setPaddingBottom:(CGFloat)newPadding
{
if ( newPadding != self.paddingBottom ) {
super.paddingBottom = newPadding;
[self.axisSet.axes makeObjectsPerformSelector:@selector(setNeedsDisplay)];
}
}
-(nullable CPTNumberArray *)topDownLayerOrder
{
return self.plotAreaFrame.plotArea.topDownLayerOrder;
}
-(void)setTopDownLayerOrder:(nullable CPTNumberArray *)newArray
{
self.plotAreaFrame.plotArea.topDownLayerOrder = newArray;
}
-(void)setTitle:(nullable NSString *)newTitle
{
if ( newTitle != title ) {
title = [newTitle copy];
if ( !self.inTitleUpdate ) {
self.inTitleUpdate = YES;
self.attributedTitle = nil;
self.inTitleUpdate = NO;
CPTLayerAnnotation *theTitleAnnotation = self.titleAnnotation;
if ( title ) {
if ( theTitleAnnotation ) {
((CPTTextLayer *)theTitleAnnotation.contentLayer).text = title;
}
else {
CPTPlotAreaFrame *frameLayer = self.plotAreaFrame;
if ( frameLayer ) {
CPTLayerAnnotation *newTitleAnnotation = [[CPTLayerAnnotation alloc] initWithAnchorLayer:frameLayer];
CPTTextLayer *newTextLayer = [[CPTTextLayer alloc] initWithText:title style:self.titleTextStyle];
newTitleAnnotation.contentLayer = newTextLayer;
newTitleAnnotation.displacement = self.titleDisplacement;
newTitleAnnotation.rectAnchor = self.titlePlotAreaFrameAnchor;
newTitleAnnotation.contentAnchorPoint = [self contentAnchorForRectAnchor:self.titlePlotAreaFrameAnchor];
[self addAnnotation:newTitleAnnotation];
self.titleAnnotation = newTitleAnnotation;
}
}
}
else {
if ( theTitleAnnotation ) {
[self removeAnnotation:theTitleAnnotation];
self.titleAnnotation = nil;
}
}
}
}
}
-(void)setAttributedTitle:(nullable NSAttributedString *)newTitle
{
if ( newTitle != attributedTitle ) {
attributedTitle = [newTitle copy];
if ( !self.inTitleUpdate ) {
self.inTitleUpdate = YES;
CPTLayerAnnotation *theTitleAnnotation = self.titleAnnotation;
if ( attributedTitle ) {
self.titleTextStyle = [CPTTextStyle textStyleWithAttributes:[attributedTitle attributesAtIndex:0
effectiveRange:NULL]];
self.title = [attributedTitle.string copy];
if ( theTitleAnnotation ) {
((CPTTextLayer *)theTitleAnnotation.contentLayer).attributedText = attributedTitle;
}
else {
CPTPlotAreaFrame *frameLayer = self.plotAreaFrame;
if ( frameLayer ) {
CPTLayerAnnotation *newTitleAnnotation = [[CPTLayerAnnotation alloc] initWithAnchorLayer:frameLayer];
CPTTextLayer *newTextLayer = [[CPTTextLayer alloc] initWithAttributedText:attributedTitle];
newTitleAnnotation.contentLayer = newTextLayer;
newTitleAnnotation.displacement = self.titleDisplacement;
newTitleAnnotation.rectAnchor = self.titlePlotAreaFrameAnchor;
newTitleAnnotation.contentAnchorPoint = [self contentAnchorForRectAnchor:self.titlePlotAreaFrameAnchor];
[self addAnnotation:newTitleAnnotation];
self.titleAnnotation = newTitleAnnotation;
}
}
}
else {
self.titleTextStyle = nil;
self.title = nil;
if ( theTitleAnnotation ) {
[self removeAnnotation:theTitleAnnotation];
self.titleAnnotation = nil;
}