-
Notifications
You must be signed in to change notification settings - Fork 610
/
CPTBarPlot.m
1811 lines (1499 loc) · 65 KB
/
CPTBarPlot.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
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 "CPTBarPlot.h"
#import "CPTColor.h"
#import "CPTExceptions.h"
#import "CPTGradient.h"
#import "CPTLegend.h"
#import "CPTMutableLineStyle.h"
#import "CPTMutableNumericData.h"
#import "CPTMutablePlotRange.h"
#import "CPTPathExtensions.h"
#import "CPTPlotArea.h"
#import "CPTPlotSpaceAnnotation.h"
#import "CPTUtilities.h"
#import "CPTXYPlotSpace.h"
#import "NSCoderExtensions.h"
#import <tgmath.h>
/** @defgroup plotAnimationBarPlot Bar Plot
* @brief Bar plot properties that can be animated using Core Animation.
* @ingroup plotAnimation
**/
/** @if MacOnly
* @defgroup plotBindingsBarPlot Bar Plot Bindings
* @brief Binding identifiers for bar plots.
* @ingroup plotBindings
* @endif
**/
CPTBarPlotBinding const CPTBarPlotBindingBarLocations = @"barLocations"; ///< Bar locations.
CPTBarPlotBinding const CPTBarPlotBindingBarTips = @"barTips"; ///< Bar tips.
CPTBarPlotBinding const CPTBarPlotBindingBarBases = @"barBases"; ///< Bar bases.
CPTBarPlotBinding const CPTBarPlotBindingBarFills = @"barFills"; ///< Bar fills.
CPTBarPlotBinding const CPTBarPlotBindingBarLineStyles = @"barLineStyles"; ///< Bar line styles.
CPTBarPlotBinding const CPTBarPlotBindingBarWidths = @"barWidths"; ///< Bar widths.
/// @cond
@interface CPTBarPlot()
@property (nonatomic, readwrite, copy, nullable) CPTNumberArray *barLocations;
@property (nonatomic, readwrite, copy, nullable) CPTNumberArray *barTips;
@property (nonatomic, readwrite, copy, nullable) CPTNumberArray *barBases;
@property (nonatomic, readwrite, copy, nullable) CPTFillArray *barFills;
@property (nonatomic, readwrite, copy, nullable) CPTLineStyleArray *barLineStyles;
@property (nonatomic, readwrite, copy, nullable) CPTLineStyleArray *barWidths;
@property (nonatomic, readwrite, assign) NSUInteger pointingDeviceDownIndex;
-(BOOL)barAtRecordIndex:(NSUInteger)idx basePoint:(nonnull CGPoint *)basePoint tipPoint:(nonnull CGPoint *)tipPoint;
-(nullable CGMutablePathRef)newBarPathWithContext:(nullable CGContextRef)context recordIndex:(NSUInteger)recordIndex;
-(nonnull CGMutablePathRef)newBarPathWithContext:(nullable CGContextRef)context basePoint:(CGPoint)basePoint tipPoint:(CGPoint)tipPoint width:(NSNumber *)width;
-(nullable CPTFill *)barFillForIndex:(NSUInteger)idx;
-(nullable CPTLineStyle *)barLineStyleForIndex:(NSUInteger)idx;
-(nonnull NSNumber *)barWidthForIndex:(NSUInteger)idx;
-(void)drawBarInContext:(nonnull CGContextRef)context recordIndex:(NSUInteger)idx;
-(CGFloat)lengthInView:(NSDecimal)plotLength;
-(double)doubleLengthInPlotCoordinates:(NSDecimal)decimalLength;
-(BOOL)barIsVisibleWithBasePoint:(CGPoint)basePoint width:(NSNumber *)width;
@end
/// @endcond
#pragma mark -
/**
* @brief A two-dimensional bar plot.
* @see See @ref plotAnimationBarPlot "Bar Plot" for a list of animatable properties.
* @if MacOnly
* @see See @ref plotBindingsBarPlot "Bar Plot Bindings" for a list of supported binding identifiers.
* @endif
**/
@implementation CPTBarPlot
@dynamic barLocations;
@dynamic barTips;
@dynamic barBases;
@dynamic barFills;
@dynamic barLineStyles;
@dynamic barWidths;
/** @property CGFloat barCornerRadius
* @brief The corner radius for the end of the bars. Default is @num{0.0} for square corners.
* @ingroup plotAnimationBarPlot
**/
@synthesize barCornerRadius;
/** @property CGFloat barBaseCornerRadius
* @brief The corner radius for the end of the bars drawn at the base value. Default is @num{0.0} for square corners.
* @ingroup plotAnimationBarPlot
**/
@synthesize barBaseCornerRadius;
/** @property nonnull NSNumber *barOffset
* @brief The starting offset of the first bar in location data units.
* @ingroup plotAnimationBarPlot
**/
@synthesize barOffset;
/** @property BOOL barWidthsAreInViewCoordinates
* @brief Whether the bar width and bar offset is in view coordinates, or in plot coordinates.
* Default is @NO, meaning plot coordinates are used.
**/
@synthesize barWidthsAreInViewCoordinates;
/** @property nonnull NSNumber *barWidth
* @brief The width of each bar. Either view or plot coordinates can be used.
*
* With plot coordinates, the bar locations are one data unit apart (e.g., 1, 2, 3, etc.),
* a value of @num{1.0} will result in bars that touch each other; a value of @num{0.5} will result in bars that are as wide
* as the gap between them.
*
* @see barWidthsAreInViewCoordinates
* @ingroup plotAnimationBarPlot
**/
@synthesize barWidth;
/** @property nullable CPTLineStyle *lineStyle
* @brief The line style for the bar outline.
* If @nil, the outline is not drawn.
**/
@synthesize lineStyle;
/** @property nullable CPTFill *fill
* @brief The fill style for the bars.
* If @nil, the bars are not filled.
**/
@synthesize fill;
/** @property BOOL barsAreHorizontal
* @brief If @YES, the bars will have a horizontal orientation, otherwise they will be vertical.
**/
@synthesize barsAreHorizontal;
/** @property nonnull NSNumber *baseValue
* @brief The coordinate value of the fixed end of the bars.
* This is only used if @ref barBasesVary is @NO. Otherwise, the data source
* will be queried for an appropriate value of #CPTBarPlotFieldBarBase.
* @ingroup plotAnimationBarPlot
**/
@synthesize baseValue;
/** @property BOOL barBasesVary
* @brief If @NO, a constant base value is used for all bars.
* If @YES, the data source is queried to supply a base value for each bar.
* @see baseValue
**/
@synthesize barBasesVary;
/** @property nullable CPTPlotRange *plotRange
* @brief Sets the plot range for the independent axis.
*
* If a plot range is provided, the bars are spaced evenly throughout the plot range. If @ref plotRange is @nil,
* bar locations are provided by Cocoa bindings or the bar plot datasource. If locations are not provided by
* either bindings or the datasource, the first bar will be placed at zero (@num{0}) and subsequent bars will be at
* successive positive integer coordinates.
**/
@synthesize plotRange;
/** @internal
* @property NSUInteger pointingDeviceDownIndex
* @brief The index that was selected on the pointing device down event.
**/
@synthesize pointingDeviceDownIndex;
#pragma mark -
#pragma mark Convenience Factory Methods
/** @brief Creates and returns a new CPTBarPlot instance initialized with a bar fill consisting of a linear gradient between black and the given color.
* @param color The beginning color.
* @param horizontal If @YES, the bars will have a horizontal orientation, otherwise they will be vertical.
* @return A new CPTBarPlot instance initialized with a linear gradient bar fill.
**/
+(nonnull instancetype)tubularBarPlotWithColor:(nonnull CPTColor *)color horizontalBars:(BOOL)horizontal
{
CPTBarPlot *barPlot = [[self alloc] init];
CPTMutableLineStyle *barLineStyle = [[CPTMutableLineStyle alloc] init];
barLineStyle.lineWidth = CPTFloat(1.0);
barLineStyle.lineColor = [CPTColor blackColor];
barPlot.lineStyle = barLineStyle;
barPlot.barsAreHorizontal = horizontal;
barPlot.barWidth = @0.8;
barPlot.barCornerRadius = CPTFloat(2.0);
CPTGradient *fillGradient = [CPTGradient gradientWithBeginningColor:color endingColor:[CPTColor blackColor]];
fillGradient.angle = CPTFloat(horizontal ? -90.0 : 0.0);
barPlot.fill = [CPTFill fillWithGradient:fillGradient];
barPlot.barWidthsAreInViewCoordinates = NO;
return barPlot;
}
#pragma mark -
#pragma mark Init/Dealloc
/// @cond
#if TARGET_OS_SIMULATOR || TARGET_OS_IPHONE
#else
+(void)initialize
{
if ( self == [CPTBarPlot class] ) {
[self exposeBinding:CPTBarPlotBindingBarLocations];
[self exposeBinding:CPTBarPlotBindingBarTips];
[self exposeBinding:CPTBarPlotBindingBarBases];
[self exposeBinding:CPTBarPlotBindingBarFills];
[self exposeBinding:CPTBarPlotBindingBarLineStyles];
[self exposeBinding:CPTBarPlotBindingBarWidths];
}
}
#endif
/// @endcond
/// @name Initialization
/// @{
/** @brief Initializes a newly allocated CPTBarPlot object with the provided frame rectangle.
*
* This is the designated initializer. The initialized layer will have the following properties:
* - @ref lineStyle = default line style
* - @ref fill = solid black fill
* - @ref barWidth = @num{0.5}
* - @ref barWidthsAreInViewCoordinates = @NO
* - @ref barOffset = @num{0.0}
* - @ref barCornerRadius = @num{0.0}
* - @ref barBaseCornerRadius = @num{0.0}
* - @ref baseValue = @num{0}
* - @ref barsAreHorizontal = @NO
* - @ref barBasesVary = @NO
* - @ref plotRange = @nil
* - @ref labelOffset = @num{10.0}
* - @ref labelField = #CPTBarPlotFieldBarTip
*
* @param newFrame The frame rectangle.
* @return The initialized CPTBarPlot object.
**/
-(nonnull instancetype)initWithFrame:(CGRect)newFrame
{
if ((self = [super initWithFrame:newFrame])) {
lineStyle = [[CPTLineStyle alloc] init];
fill = [CPTFill fillWithColor:[CPTColor blackColor]];
barWidth = @0.5;
barWidthsAreInViewCoordinates = NO;
barOffset = @0.0;
barCornerRadius = CPTFloat(0.0);
barBaseCornerRadius = CPTFloat(0.0);
baseValue = @0.0;
barsAreHorizontal = NO;
barBasesVary = NO;
plotRange = nil;
pointingDeviceDownIndex = NSNotFound;
self.labelOffset = CPTFloat(10.0);
self.labelField = CPTBarPlotFieldBarTip;
}
return self;
}
/// @}
/// @cond
-(nonnull instancetype)initWithLayer:(nonnull id)layer
{
if ((self = [super initWithLayer:layer])) {
CPTBarPlot *theLayer = (CPTBarPlot *)layer;
lineStyle = theLayer->lineStyle;
fill = theLayer->fill;
barWidth = theLayer->barWidth;
barWidthsAreInViewCoordinates = theLayer->barWidthsAreInViewCoordinates;
barOffset = theLayer->barOffset;
barCornerRadius = theLayer->barCornerRadius;
barBaseCornerRadius = theLayer->barBaseCornerRadius;
baseValue = theLayer->baseValue;
barBasesVary = theLayer->barBasesVary;
barsAreHorizontal = theLayer->barsAreHorizontal;
plotRange = theLayer->plotRange;
pointingDeviceDownIndex = NSNotFound;
}
return self;
}
/// @endcond
#pragma mark -
#pragma mark NSCoding Methods
/// @cond
-(void)encodeWithCoder:(nonnull NSCoder *)coder
{
[super encodeWithCoder:coder];
[coder encodeObject:self.lineStyle forKey:@"CPTBarPlot.lineStyle"];
[coder encodeObject:self.fill forKey:@"CPTBarPlot.fill"];
[coder encodeObject:self.barWidth forKey:@"CPTBarPlot.barWidth"];
[coder encodeObject:self.barOffset forKey:@"CPTBarPlot.barOffset"];
[coder encodeCGFloat:self.barCornerRadius forKey:@"CPTBarPlot.barCornerRadius"];
[coder encodeCGFloat:self.barBaseCornerRadius forKey:@"CPTBarPlot.barBaseCornerRadius"];
[coder encodeObject:self.baseValue forKey:@"CPTBarPlot.baseValue"];
[coder encodeBool:self.barsAreHorizontal forKey:@"CPTBarPlot.barsAreHorizontal"];
[coder encodeBool:self.barBasesVary forKey:@"CPTBarPlot.barBasesVary"];
[coder encodeBool:self.barWidthsAreInViewCoordinates forKey:@"CPTBarPlot.barWidthsAreInViewCoordinates"];
[coder encodeObject:self.plotRange forKey:@"CPTBarPlot.plotRange"];
// No need to archive these properties:
// pointingDeviceDownIndex
}
-(nullable instancetype)initWithCoder:(nonnull NSCoder *)coder
{
if ((self = [super initWithCoder:coder])) {
NSNumber *num;
lineStyle = [[coder decodeObjectOfClass:[CPTLineStyle class]
forKey:@"CPTBarPlot.lineStyle"] copy];
fill = [[coder decodeObjectOfClass:[CPTFill class]
forKey:@"CPTBarPlot.fill"] copy];
num = [coder decodeObjectOfClass:[NSNumber class]
forKey:@"CPTBarPlot.barWidth"];
barWidth = num ? num : @0.0;
num = [coder decodeObjectOfClass:[NSNumber class]
forKey:@"CPTBarPlot.barOffset"];
barOffset = num ? num : @0.0;
barCornerRadius = [coder decodeCGFloatForKey:@"CPTBarPlot.barCornerRadius"];
barBaseCornerRadius = [coder decodeCGFloatForKey:@"CPTBarPlot.barBaseCornerRadius"];
num = [coder decodeObjectOfClass:[NSNumber class]
forKey:@"CPTBarPlot.baseValue"];
baseValue = num ? num : @0.0;
barsAreHorizontal = [coder decodeBoolForKey:@"CPTBarPlot.barsAreHorizontal"];
barBasesVary = [coder decodeBoolForKey:@"CPTBarPlot.barBasesVary"];
barWidthsAreInViewCoordinates = [coder decodeBoolForKey:@"CPTBarPlot.barWidthsAreInViewCoordinates"];
plotRange = [[coder decodeObjectOfClass:[CPTPlotRange class]
forKey:@"CPTBarPlot.plotRange"] copy];
pointingDeviceDownIndex = NSNotFound;
}
return self;
}
/// @endcond
#pragma mark -
#pragma mark NSSecureCoding Methods
/// @cond
+(BOOL)supportsSecureCoding
{
return YES;
}
/// @endcond
#pragma mark -
#pragma mark Data Loading
/// @cond
-(void)reloadDataInIndexRange:(NSRange)indexRange
{
[super reloadDataInIndexRange:indexRange];
// Bar fills
[self reloadBarFillsInIndexRange:indexRange];
// Bar line styles
[self reloadBarLineStylesInIndexRange:indexRange];
// Bar widths
[self reloadBarWidthsInIndexRange:indexRange];
// Legend
id<CPTBarPlotDataSource> theDataSource = (id<CPTBarPlotDataSource>)self.dataSource;
if ( [theDataSource respondsToSelector:@selector(legendTitleForBarPlot:recordIndex:)] ) {
[[NSNotificationCenter defaultCenter] postNotificationName:CPTLegendNeedsRedrawForPlotNotification object:self];
}
}
-(void)reloadPlotDataInIndexRange:(NSRange)indexRange
{
[super reloadPlotDataInIndexRange:indexRange];
if ( ![self loadNumbersForAllFieldsFromDataSourceInRecordIndexRange:indexRange] ) {
id<CPTBarPlotDataSource> theDataSource = (id<CPTBarPlotDataSource>)self.dataSource;
// Bar lengths
if ( theDataSource ) {
id newBarLengths = [self numbersFromDataSourceForField:CPTBarPlotFieldBarTip recordIndexRange:indexRange];
[self cacheNumbers:newBarLengths forField:CPTBarPlotFieldBarTip atRecordIndex:indexRange.location];
if ( self.barBasesVary ) {
id newBarBases = [self numbersFromDataSourceForField:CPTBarPlotFieldBarBase recordIndexRange:indexRange];
[self cacheNumbers:newBarBases forField:CPTBarPlotFieldBarBase atRecordIndex:indexRange.location];
}
else {
self.barBases = nil;
}
}
else {
self.barTips = nil;
self.barBases = nil;
}
// Locations of bars
if ( self.plotRange ) {
// Spread bars evenly over the plot range
CPTMutableNumericData *locationData = nil;
if ( self.doublePrecisionCache ) {
locationData = [[CPTMutableNumericData alloc] initWithData:[NSData data]
dataType:CPTDataType(CPTFloatingPointDataType, sizeof(double), CFByteOrderGetCurrent())
shape:nil];
locationData.shape = @[@(indexRange.length)];
double doublePrecisionDelta = 1.0;
if ( indexRange.length > 1 ) {
doublePrecisionDelta = self.plotRange.lengthDouble / (double)(indexRange.length - 1);
}
double locationDouble = self.plotRange.locationDouble;
double *dataBytes = (double *)locationData.mutableBytes;
double *dataEnd = dataBytes + indexRange.length;
while ( dataBytes < dataEnd ) {
*dataBytes++ = locationDouble;
locationDouble += doublePrecisionDelta;
}
}
else {
locationData = [[CPTMutableNumericData alloc] initWithData:[NSData data]
dataType:CPTDataType(CPTDecimalDataType, sizeof(NSDecimal), CFByteOrderGetCurrent())
shape:nil];
locationData.shape = @[@(indexRange.length)];
NSDecimal delta = CPTDecimalFromInteger(1);
if ( indexRange.length > 1 ) {
delta = CPTDecimalDivide(self.plotRange.lengthDecimal, CPTDecimalFromUnsignedInteger(indexRange.length - 1));
}
NSDecimal locationDecimal = self.plotRange.locationDecimal;
NSDecimal *dataBytes = (NSDecimal *)locationData.mutableBytes;
NSDecimal *dataEnd = dataBytes + indexRange.length;
while ( dataBytes < dataEnd ) {
*dataBytes++ = locationDecimal;
locationDecimal = CPTDecimalAdd(locationDecimal, delta);
}
}
[self cacheNumbers:locationData forField:CPTBarPlotFieldBarLocation atRecordIndex:indexRange.location];
}
else if ( theDataSource ) {
// Get locations from the datasource
id newBarLocations = [self numbersFromDataSourceForField:CPTBarPlotFieldBarLocation recordIndexRange:indexRange];
[self cacheNumbers:newBarLocations forField:CPTBarPlotFieldBarLocation atRecordIndex:indexRange.location];
}
else {
// Make evenly spaced locations starting at zero
CPTMutableNumericData *locationData = nil;
if ( self.doublePrecisionCache ) {
locationData = [[CPTMutableNumericData alloc] initWithData:[NSData data]
dataType:CPTDataType(CPTFloatingPointDataType, sizeof(double), CFByteOrderGetCurrent())
shape:nil];
locationData.shape = @[@(indexRange.length)];
double locationDouble = 0.0;
double *dataBytes = (double *)locationData.mutableBytes;
double *dataEnd = dataBytes + indexRange.length;
while ( dataBytes < dataEnd ) {
*dataBytes++ = locationDouble;
locationDouble += 1.0;
}
}
else {
locationData = [[CPTMutableNumericData alloc] initWithData:[NSData data]
dataType:CPTDataType(CPTDecimalDataType, sizeof(NSDecimal), CFByteOrderGetCurrent())
shape:nil];
locationData.shape = @[@(indexRange.length)];
NSDecimal locationDecimal = CPTDecimalFromInteger(0);
NSDecimal *dataBytes = (NSDecimal *)locationData.mutableBytes;
NSDecimal *dataEnd = dataBytes + indexRange.length;
NSDecimal one = CPTDecimalFromInteger(1);
while ( dataBytes < dataEnd ) {
*dataBytes++ = locationDecimal;
locationDecimal = CPTDecimalAdd(locationDecimal, one);
}
}
[self cacheNumbers:locationData forField:CPTBarPlotFieldBarLocation atRecordIndex:indexRange.location];
}
}
}
/// @endcond
/**
* @brief Reload all bar fills from the data source immediately.
**/
-(void)reloadBarFills
{
[self reloadBarFillsInIndexRange:NSMakeRange(0, self.cachedDataCount)];
}
/** @brief Reload bar fills in the given index range from the data source immediately.
* @param indexRange The index range to load.
**/
-(void)reloadBarFillsInIndexRange:(NSRange)indexRange
{
id<CPTBarPlotDataSource> theDataSource = (id<CPTBarPlotDataSource>)self.dataSource;
BOOL needsLegendUpdate = NO;
if ( [theDataSource respondsToSelector:@selector(barFillsForBarPlot:recordIndexRange:)] ) {
needsLegendUpdate = YES;
[self cacheArray:[theDataSource barFillsForBarPlot:self recordIndexRange:indexRange]
forKey:CPTBarPlotBindingBarFills
atRecordIndex:indexRange.location];
}
else if ( [theDataSource respondsToSelector:@selector(barFillForBarPlot:recordIndex:)] ) {
needsLegendUpdate = YES;
id nilObject = [CPTPlot nilData];
CPTMutableFillArray *array = [[NSMutableArray alloc] initWithCapacity:indexRange.length];
NSUInteger maxIndex = NSMaxRange(indexRange);
for ( NSUInteger idx = indexRange.location; idx < maxIndex; idx++ ) {
CPTFill *dataSourceFill = [theDataSource barFillForBarPlot:self recordIndex:idx];
if ( dataSourceFill ) {
[array addObject:dataSourceFill];
}
else {
[array addObject:nilObject];
}
}
[self cacheArray:array
forKey:CPTBarPlotBindingBarFills
atRecordIndex:indexRange.location];
}
// Legend
if ( needsLegendUpdate ) {
[[NSNotificationCenter defaultCenter] postNotificationName:CPTLegendNeedsRedrawForPlotNotification object:self];
}
[self setNeedsDisplay];
}
/**
* @brief Reload all bar line styles from the data source immediately.
**/
-(void)reloadBarLineStyles
{
[self reloadBarLineStylesInIndexRange:NSMakeRange(0, self.cachedDataCount)];
}
/** @brief Reload bar line styles in the given index range from the data source immediately.
* @param indexRange The index range to load.
**/
-(void)reloadBarLineStylesInIndexRange:(NSRange)indexRange
{
id<CPTBarPlotDataSource> theDataSource = (id<CPTBarPlotDataSource>)self.dataSource;
BOOL needsLegendUpdate = NO;
if ( [theDataSource respondsToSelector:@selector(barLineStylesForBarPlot:recordIndexRange:)] ) {
needsLegendUpdate = YES;
[self cacheArray:[theDataSource barLineStylesForBarPlot:self recordIndexRange:indexRange]
forKey:CPTBarPlotBindingBarLineStyles
atRecordIndex:indexRange.location];
}
else if ( [theDataSource respondsToSelector:@selector(barLineStyleForBarPlot:recordIndex:)] ) {
needsLegendUpdate = YES;
id nilObject = [CPTPlot nilData];
CPTMutableLineStyleArray *array = [[NSMutableArray alloc] initWithCapacity:indexRange.length];
NSUInteger maxIndex = NSMaxRange(indexRange);
for ( NSUInteger idx = indexRange.location; idx < maxIndex; idx++ ) {
CPTLineStyle *dataSourceLineStyle = [theDataSource barLineStyleForBarPlot:self recordIndex:idx];
if ( dataSourceLineStyle ) {
[array addObject:dataSourceLineStyle];
}
else {
[array addObject:nilObject];
}
}
[self cacheArray:array
forKey:CPTBarPlotBindingBarLineStyles
atRecordIndex:indexRange.location];
}
// Legend
if ( needsLegendUpdate ) {
[[NSNotificationCenter defaultCenter] postNotificationName:CPTLegendNeedsRedrawForPlotNotification object:self];
}
[self setNeedsDisplay];
}
/**
* @brief Reload all bar widths from the data source immediately.
**/
-(void)reloadBarWidths
{
[self reloadBarWidthsInIndexRange:NSMakeRange(0, self.cachedDataCount)];
}
/** @brief Reload bar widths in the given index range from the data source immediately.
* @param indexRange The index range to load.
**/
-(void)reloadBarWidthsInIndexRange:(NSRange)indexRange
{
id<CPTBarPlotDataSource> theDataSource = (id<CPTBarPlotDataSource>)self.dataSource;
if ( [theDataSource respondsToSelector:@selector(barWidthsForBarPlot:recordIndexRange:)] ) {
[self cacheArray:[theDataSource barWidthsForBarPlot:self recordIndexRange:indexRange]
forKey:CPTBarPlotBindingBarWidths
atRecordIndex:indexRange.location];
}
else if ( [theDataSource respondsToSelector:@selector(barWidthForBarPlot:recordIndex:)] ) {
id nilObject = [CPTPlot nilData];
CPTMutableNumberArray *array = [[NSMutableArray alloc] initWithCapacity:indexRange.length];
NSUInteger maxIndex = NSMaxRange(indexRange);
for ( NSUInteger idx = indexRange.location; idx < maxIndex; idx++ ) {
NSNumber *width = [theDataSource barWidthForBarPlot:self recordIndex:idx];
if ( width ) {
[array addObject:width];
}
else {
[array addObject:nilObject];
}
}
[self cacheArray:array
forKey:CPTBarPlotBindingBarWidths
atRecordIndex:indexRange.location];
}
[self setNeedsDisplay];
}
#pragma mark -
#pragma mark Length Conversions for Independent Coordinate (e.g., widths, offsets)
/// @cond
-(CGFloat)lengthInView:(NSDecimal)decimalLength
{
CGFloat length;
if ( self.barWidthsAreInViewCoordinates ) {
length = CPTDecimalCGFloatValue(decimalLength);
}
else {
CPTCoordinate coordinate = (self.barsAreHorizontal ? CPTCoordinateY : CPTCoordinateX);
CPTXYPlotSpace *thePlotSpace = (CPTXYPlotSpace *)self.plotSpace;
NSDecimal xLocation = thePlotSpace.xRange.locationDecimal;
NSDecimal yLocation = thePlotSpace.yRange.locationDecimal;
NSDecimal originPlotPoint[2];
NSDecimal displacedPlotPoint[2];
switch ( coordinate ) {
case CPTCoordinateX:
originPlotPoint[CPTCoordinateX] = xLocation;
originPlotPoint[CPTCoordinateY] = yLocation;
displacedPlotPoint[CPTCoordinateX] = CPTDecimalAdd(xLocation, decimalLength);
displacedPlotPoint[CPTCoordinateY] = yLocation;
break;
case CPTCoordinateY:
originPlotPoint[CPTCoordinateX] = xLocation;
originPlotPoint[CPTCoordinateY] = yLocation;
displacedPlotPoint[CPTCoordinateX] = xLocation;
displacedPlotPoint[CPTCoordinateY] = CPTDecimalAdd(yLocation, decimalLength);
break;
default:
break;
}
CGPoint originPoint = [thePlotSpace plotAreaViewPointForPlotPoint:originPlotPoint numberOfCoordinates:2];
CGPoint displacedPoint = [thePlotSpace plotAreaViewPointForPlotPoint:displacedPlotPoint numberOfCoordinates:2];
switch ( coordinate ) {
case CPTCoordinateX:
length = displacedPoint.x - originPoint.x;
break;
case CPTCoordinateY:
length = displacedPoint.y - originPoint.y;
break;
default:
length = CPTFloat(0.0);
break;
}
}
return length;
}
-(double)doubleLengthInPlotCoordinates:(NSDecimal)decimalLength
{
double length;
if ( self.barWidthsAreInViewCoordinates ) {
CGFloat floatLength = CPTDecimalCGFloatValue(decimalLength);
CGPoint originViewPoint = CGPointZero;
CGPoint displacedViewPoint = CPTPointMake(floatLength, floatLength);
double originPlotPoint[2], displacedPlotPoint[2];
CPTPlotSpace *thePlotSpace = self.plotSpace;
[thePlotSpace doublePrecisionPlotPoint:originPlotPoint numberOfCoordinates:2 forPlotAreaViewPoint:originViewPoint];
[thePlotSpace doublePrecisionPlotPoint:displacedPlotPoint numberOfCoordinates:2 forPlotAreaViewPoint:displacedViewPoint];
if ( self.barsAreHorizontal ) {
length = displacedPlotPoint[CPTCoordinateY] - originPlotPoint[CPTCoordinateY];
}
else {
length = displacedPlotPoint[CPTCoordinateX] - originPlotPoint[CPTCoordinateX];
}
}
else {
length = CPTDecimalDoubleValue(decimalLength);
}
return length;
}
-(NSDecimal)lengthInPlotCoordinates:(NSDecimal)decimalLength
{
NSDecimal length;
if ( self.barWidthsAreInViewCoordinates ) {
CGFloat floatLength = CPTDecimalCGFloatValue(decimalLength);
CGPoint originViewPoint = CGPointZero;
CGPoint displacedViewPoint = CPTPointMake(floatLength, floatLength);
NSDecimal originPlotPoint[2], displacedPlotPoint[2];
CPTPlotSpace *thePlotSpace = self.plotSpace;
[thePlotSpace plotPoint:originPlotPoint numberOfCoordinates:2 forPlotAreaViewPoint:originViewPoint];
[thePlotSpace plotPoint:displacedPlotPoint numberOfCoordinates:2 forPlotAreaViewPoint:displacedViewPoint];
if ( self.barsAreHorizontal ) {
length = CPTDecimalSubtract(displacedPlotPoint[CPTCoordinateY], originPlotPoint[CPTCoordinateY]);
}
else {
length = CPTDecimalSubtract(displacedPlotPoint[CPTCoordinateX], originPlotPoint[CPTCoordinateX]);
}
}
else {
length = decimalLength;
}
return length;
}
/// @endcond
#pragma mark -
#pragma mark Data Ranges
/// @cond
-(nullable CPTPlotRange *)plotRangeForCoordinate:(CPTCoordinate)coord
{
CPTPlotRange *range = [super plotRangeForCoordinate:coord];
if ( !self.barBasesVary ) {
switch ( coord ) {
case CPTCoordinateX:
if ( self.barsAreHorizontal ) {
NSDecimal base = self.baseValue.decimalValue;
if ( ![range contains:base] ) {
CPTMutablePlotRange *newRange = [range mutableCopy];
[newRange unionPlotRange:[CPTPlotRange plotRangeWithLocationDecimal:base lengthDecimal:CPTDecimalFromInteger(0)]];
range = newRange;
}
}
break;
case CPTCoordinateY:
if ( !self.barsAreHorizontal ) {
NSDecimal base = self.baseValue.decimalValue;
if ( ![range contains:base] ) {
CPTMutablePlotRange *newRange = [range mutableCopy];
[newRange unionPlotRange:[CPTPlotRange plotRangeWithLocationDecimal:base lengthDecimal:CPTDecimalFromInteger(0)]];
range = newRange;
}
}
break;
default:
break;
}
}
return range;
}
-(nullable CPTPlotRange *)plotRangeEnclosingField:(NSUInteger)fieldEnum
{
CPTPlotRange *range = nil;
switch ( fieldEnum ) {
case CPTBarPlotFieldBarLocation:
range = [self plotRangeEnclosingBars];
break;
case CPTBarPlotFieldBarTip:
case CPTBarPlotFieldBarBase:
range = [self plotRangeForField:fieldEnum];
break;
default:
break;
}
return range;
}
/// @endcond
/** @brief Computes a plot range that completely encloses all of the bars.
*
* For a horizontal bar plot, this range starts at the left edge of the first bar and continues to the right edge
* of the last bar. Similarly, this range starts at the bottom edge of the first bar and continues to the top edge
* of the last bar for vertical bar plots. The length will have the same sign as the corresponding plot range from the plot space.
*
* @return A plot range that completely encloses all of the bars.
**/
-(nullable CPTPlotRange *)plotRangeEnclosingBars
{
BOOL horizontalBars = self.barsAreHorizontal;
CPTMutablePlotRange *range;
if ( horizontalBars ) {
range = [[self plotRangeForCoordinate:CPTCoordinateY] mutableCopy];
}
else {
range = [[self plotRangeForCoordinate:CPTCoordinateX] mutableCopy];
}
NSDecimal barOffsetLength = [self lengthInPlotCoordinates:self.barOffset.decimalValue];
NSDecimal barWidthLength = [self lengthInPlotCoordinates:self.barWidth.decimalValue];
NSDecimal halfBarWidth = CPTDecimalDivide(barWidthLength, CPTDecimalFromInteger(2));
NSDecimal rangeLocation = range.locationDecimal;
NSDecimal rangeLength = range.lengthDecimal;
if ( CPTDecimalGreaterThanOrEqualTo(rangeLength, CPTDecimalFromInteger(0))) {
rangeLocation = CPTDecimalSubtract(rangeLocation, halfBarWidth);
range.locationDecimal = CPTDecimalAdd(rangeLocation, barOffsetLength);
range.lengthDecimal = CPTDecimalAdd(rangeLength, barWidthLength);
}
else {
rangeLocation = CPTDecimalAdd(rangeLocation, halfBarWidth);
range.locationDecimal = CPTDecimalSubtract(rangeLocation, barOffsetLength);
range.lengthDecimal = CPTDecimalSubtract(rangeLength, barWidthLength);
}
return range;
}
#pragma mark -
#pragma mark Drawing
/// @cond
-(void)renderAsVectorInContext:(nonnull CGContextRef)context
{
if ( self.hidden ) {
return;
}
CPTMutableNumericData *cachedLocations = [self cachedNumbersForField:CPTBarPlotFieldBarLocation];
CPTMutableNumericData *cachedLengths = [self cachedNumbersForField:CPTBarPlotFieldBarTip];
if ((cachedLocations == nil) || (cachedLengths == nil)) {
return;
}
BOOL basesVary = self.barBasesVary;
CPTMutableNumericData *cachedBases = [self cachedNumbersForField:CPTBarPlotFieldBarBase];
if ( basesVary && (cachedBases == nil)) {
return;
}
NSUInteger barCount = self.cachedDataCount;
if ( barCount == 0 ) {
return;
}
if ( cachedLocations.numberOfSamples != cachedLengths.numberOfSamples ) {
[NSException raise:CPTException format:@"Number of bar locations and lengths do not match"];
}
if ( basesVary && (cachedLengths.numberOfSamples != cachedBases.numberOfSamples)) {
[NSException raise:CPTException format:@"Number of bar lengths and bases do not match"];
}
[super renderAsVectorInContext:context];
CGContextBeginTransparencyLayer(context, NULL);
for ( NSUInteger ii = 0; ii < barCount; ii++ ) {
// Draw
[self drawBarInContext:context recordIndex:ii];
}
CGContextEndTransparencyLayer(context);
}
-(BOOL)barAtRecordIndex:(NSUInteger)idx basePoint:(nonnull CGPoint *)basePoint tipPoint:(nonnull CGPoint *)tipPoint
{
BOOL horizontalBars = self.barsAreHorizontal;
CPTCoordinate independentCoord = (horizontalBars ? CPTCoordinateY : CPTCoordinateX);
CPTCoordinate dependentCoord = (horizontalBars ? CPTCoordinateX : CPTCoordinateY);
CPTPlotSpace *thePlotSpace = self.plotSpace;
if ( self.doublePrecisionCache ) {
double plotPoint[2];
plotPoint[independentCoord] = [self cachedDoubleForField:CPTBarPlotFieldBarLocation recordIndex:idx];
if ( isnan(plotPoint[independentCoord])) {
return NO;
}
// Tip point
plotPoint[dependentCoord] = [self cachedDoubleForField:CPTBarPlotFieldBarTip recordIndex:idx];
if ( isnan(plotPoint[dependentCoord])) {
return NO;
}
*tipPoint = [thePlotSpace plotAreaViewPointForDoublePrecisionPlotPoint:plotPoint numberOfCoordinates:2];
// Base point
if ( !self.barBasesVary ) {
plotPoint[dependentCoord] = self.baseValue.doubleValue;
}
else {
plotPoint[dependentCoord] = [self cachedDoubleForField:CPTBarPlotFieldBarBase recordIndex:idx];
}
if ( isnan(plotPoint[dependentCoord])) {
return NO;
}
*basePoint = [thePlotSpace plotAreaViewPointForDoublePrecisionPlotPoint:plotPoint numberOfCoordinates:2];
}
else {
NSDecimal plotPoint[2];
plotPoint[independentCoord] = [self cachedDecimalForField:CPTBarPlotFieldBarLocation recordIndex:idx];
if ( NSDecimalIsNotANumber(&plotPoint[independentCoord])) {
return NO;
}
// Tip point
plotPoint[dependentCoord] = [self cachedDecimalForField:CPTBarPlotFieldBarTip recordIndex:idx];
if ( NSDecimalIsNotANumber(&plotPoint[dependentCoord])) {
return NO;
}
*tipPoint = [thePlotSpace plotAreaViewPointForPlotPoint:plotPoint numberOfCoordinates:2];
// Base point
if ( !self.barBasesVary ) {
plotPoint[dependentCoord] = self.baseValue.decimalValue;
}
else {
plotPoint[dependentCoord] = [self cachedDecimalForField:CPTBarPlotFieldBarBase recordIndex:idx];
}
if ( NSDecimalIsNotANumber(&plotPoint[dependentCoord])) {
return NO;
}
*basePoint = [thePlotSpace plotAreaViewPointForPlotPoint:plotPoint numberOfCoordinates:2];
}
// Determine bar width and offset.
CGFloat barOffsetLength = [self lengthInView:self.barOffset.decimalValue];
// Offset
if ( horizontalBars ) {
basePoint->y += barOffsetLength;
tipPoint->y += barOffsetLength;
}
else {
basePoint->x += barOffsetLength;
tipPoint->x += barOffsetLength;
}
return YES;
}
-(nullable CGMutablePathRef)newBarPathWithContext:(nullable CGContextRef)context recordIndex:(NSUInteger)recordIndex
{
// Get base and tip points
CGPoint basePoint, tipPoint;
BOOL barExists = [self barAtRecordIndex:recordIndex basePoint:&basePoint tipPoint:&tipPoint];
if ( !barExists ) {
return NULL;