Skip to content

Commit d416969

Browse files
committed
Handle one datapoint in regular routines, not as special case
Simplify handling of zero points Add NumberOfPoints field to TestBed Fix bug when increasing numberofPoints by more than two Add Activity Indicator to testBed to test didFinishDrawing
1 parent 773f5a7 commit d416969

7 files changed

Lines changed: 1039 additions & 969 deletions

File tree

Classes/BEMLine.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ - (void)drawRect:(CGRect)rect {
3030
//---- Draw Reference Lines ---//
3131
//----------------------------//
3232
self.layer.sublayers = nil;
33-
33+
if (self.points.count == 0) {
34+
self.backgroundColor = self.bottomColor ?: [UIColor clearColor];
35+
return;
36+
};
37+
3438
UIBezierPath *verticalReferenceLinesPath = [UIBezierPath bezierPath];
3539
UIBezierPath *horizontalReferenceLinesPath = [UIBezierPath bezierPath];
3640
UIBezierPath *referenceFramePath = [UIBezierPath bezierPath];

Classes/BEMSimpleLineGraphView.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ IB_DESIGNABLE @interface BEMSimpleLineGraphView : UIView <UIGestureRecognizerDel
157157

158158

159159
/** Animation style used when the graph appears. Default value is BEMLineAnimationDraw.
160-
@see Refer to \p BEMLineAnimation for a complete list of animation styles. */
160+
@see Refer to \p BEMLineAnimation for a complete list of animation styles.
161+
@discussion Setting this to BEMLineANimationNone also sets animationGraphEntranceTime to zero.
162+
*/
161163
@property (nonatomic) BEMLineAnimation animationGraphStyle;
162164

163165

@@ -484,7 +486,7 @@ IB_DESIGNABLE @interface BEMSimpleLineGraphView : UIView <UIGestureRecognizerDel
484486
/** Sent to the delegate each time the line graph finishes animating and drawing.
485487
@discussion The respective graph object has been completely drawn and animated at this point. It is safe to use \p graphSnapshotImage after recieving this method call on the delegate.
486488
487-
This method may be called in addition to the \p lineGraphDidFinishLoading: method, after drawing has completed. \p animationGraphEntranceTime is taken into account when calling this method.
489+
This method will be called in addition to the \p lineGraphDidFinishLoading: method, after drawing has completed. \p animationGraphEntranceTime is taken into account when calling this method.
488490
489491
@seealso lineGraphDidFinishLoading:
490492
@param graph The graph object that finished drawing. */

Classes/BEMSimpleLineGraphView.m

Lines changed: 84 additions & 99 deletions
Large diffs are not rendered by default.

Sample Project/TestBed/Base.lproj/Main.storyboard

Lines changed: 887 additions & 845 deletions
Large diffs are not rendered by default.

Sample Project/TestBed/DetailViewController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
@property (readonly) NSDate * oldestDate, * newestDate;
2222
@property (readonly) CGFloat smallestValue, biggestValue;
23-
@property (readonly) NSInteger numberOfPoints;
23+
@property (assign, nonatomic) NSInteger numberOfPoints;
2424
@property (assign, nonatomic) float percentNulls;
2525

2626
//data needed to implement delegate methods

Sample Project/TestBed/DetailViewController.m

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ @interface DetailViewController ()
1414

1515
@property (strong, nonatomic) NSDate * oldestDate, * newestDate;
1616
@property (assign, nonatomic) CGFloat smallestValue, biggestValue;
17-
@property (nonatomic, assign) NSInteger numberOfPoints;
18-
1917

2018
@property (weak, nonatomic) IBOutlet UIStepper *graphObjectIncrement;
19+
@property (strong, nonatomic) IBOutlet UIActivityIndicatorView * activity;
2120

2221
@property (strong, nonatomic) NSMutableArray <NSNumber *> *arrayOfValues;
2322
@property (strong, nonatomic) NSMutableArray <NSDate *> *arrayOfDates;
@@ -41,9 +40,11 @@ @implementation DetailViewController
4140

4241
- (void)viewDidLoad {
4342
[super viewDidLoad];
44-
self.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
43+
self.activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
4544
self.navigationItem.leftItemsSupplementBackButton = YES;
46-
45+
UIBarButtonItem * activityButton = [[UIBarButtonItem alloc] initWithCustomView:self.activity];
46+
self.navigationItem.rightBarButtonItems = [self.navigationItem.rightBarButtonItems arrayByAddingObject:activityButton];
47+
4748
self.maxValue = -1.0;
4849
self.minValue = -1.0;
4950
self.maxXValue = -1.0;
@@ -66,7 +67,7 @@ - (void)viewDidLoad {
6667

6768
// Do any additional setup after loading the view.
6869

69-
self.graphObjectIncrement.value = 1000;
70+
_numberOfPoints = 10;
7071

7172
[self hydrateDatasets];
7273

@@ -75,21 +76,35 @@ - (void)viewDidLoad {
7576

7677
#pragma mark Data management
7778

79+
-(void) setNumberOfPoints:(NSInteger)numberOfPoints {
80+
if (numberOfPoints != _numberOfPoints) {
81+
NSInteger oldNumberOfPoints = _numberOfPoints;
82+
83+
_numberOfPoints = numberOfPoints;
84+
85+
if (numberOfPoints == oldNumberOfPoints + 1) {
86+
[self addPointToGraph];
87+
} else if (numberOfPoints == oldNumberOfPoints - 1) {
88+
[self removePointFromGraph];
89+
} else {
90+
[self hydrateDatasets];
91+
}
92+
[self.myGraph reloadGraph];
93+
}
94+
}
95+
7896
float randomProbability () {
7997
return (float) ((double)(arc4random())) / UINT32_MAX;
8098
}
8199

82-
83100
- (void)hydrateDatasets {
84101
// Reset the arrays of values (Y-Axis points) and dates (X-Axis points / labels)
85102
if (!self.arrayOfValues) self.arrayOfValues = [[NSMutableArray alloc] init];
86103
if (!self.arrayOfDates) self.arrayOfDates = [[NSMutableArray alloc] init];
87104
[self.arrayOfValues removeAllObjects];
88105
[self.arrayOfDates removeAllObjects];
89106

90-
self.totalNumber = 0;
91107
NSDate *date = [NSDate date];
92-
self.numberOfPoints = self.graphObjectIncrement.value;
93108
// Add objects to the array based on the stepper value
94109
CGFloat lastValue = 5000;
95110
for (int i = 0; i < self.numberOfPoints; i++) {
@@ -104,13 +119,15 @@ - (void)hydrateDatasets {
104119
date = [self dateForGraphAfterDate:date];
105120
}
106121
[self checkMaximums];
122+
self.graphObjectIncrement.value = self.numberOfPoints;
107123
}
108124

109125
-(void) checkMaximums {
110126
self.oldestDate = [NSDate distantFuture];
111127
self.newestDate = [NSDate distantPast];
112128
self.biggestValue = -INFINITY;
113129
self.smallestValue = INFINITY;
130+
self.totalNumber = 0;
114131
for (NSInteger i = 0; i < self.numberOfPoints; i++) {
115132
CGFloat value = self.arrayOfValues[(NSUInteger)i].floatValue;
116133
if (value < BEMNullGraphValue) {
@@ -119,7 +136,7 @@ -(void) checkMaximums {
119136
self.smallestValue = MIN(self.smallestValue,value );
120137
}
121138
}
122-
self.oldestDate = self.arrayOfDates[0];
139+
if (self.arrayOfDates.count > 0) self.oldestDate = self.arrayOfDates[0];
123140
self.newestDate = [self.arrayOfDates lastObject]; //needs to be last for notification
124141

125142
}
@@ -157,14 +174,7 @@ - (float)getRandomFloat {
157174
}
158175

159176
- (IBAction)addOrRemovePointFromGraph:(id)sender {
160-
if (self.graphObjectIncrement.value > self.numberOfPoints) {
161-
[self addPointToGraph];
162-
} else if (self.graphObjectIncrement.value < self.numberOfPoints) {
163-
[self removePointFromGraph];
164-
}
165177
self.numberOfPoints = self.graphObjectIncrement.value;
166-
[self checkMaximums];
167-
[self.myGraph reloadGraph];
168178
}
169179

170180
- (void) addPointToGraph {
@@ -180,6 +190,7 @@ - (void) addPointToGraph {
180190
[self.arrayOfValues addObject:newValue];
181191
NSDate *lastDate = self.arrayOfDates.count > 0 ? [self.arrayOfDates lastObject]: [NSDate date];
182192
NSDate *newDate = [self dateForGraphAfterDate:lastDate];
193+
self.oldestDate = newDate;
183194
[self.arrayOfDates addObject:newDate];
184195
}
185196

@@ -188,8 +199,10 @@ - (void) removePointFromGraph {
188199
// Remove point
189200
[self.arrayOfValues removeObjectAtIndex:0];
190201
[self.arrayOfDates removeObjectAtIndex:0];
202+
[self checkMaximums];
191203
}
192204
}
205+
193206
-(NSString *) formatNumber: (NSNumber *) number {
194207
return [NSNumberFormatter localizedStringFromNumber:number
195208
numberStyle:NSNumberFormatterDecimalStyle];
@@ -287,6 +300,16 @@ -(BOOL) respondsToSelector:(SEL)aSelector {
287300
}
288301
}
289302

303+
-(void) lineGraphDidBeginLoading:(BEMSimpleLineGraphView *)graph {
304+
[self.activity startAnimating];
305+
306+
}
307+
308+
-(void) lineGraphDidFinishDrawing:(BEMSimpleLineGraphView *)graph {
309+
[self.activity stopAnimating];
310+
311+
}
312+
290313

291314
- (NSString *)lineGraph:(BEMSimpleLineGraphView *)graph labelOnXAxisForIndex:(NSInteger)index {
292315
// return [NSString stringWithFormat:@"%lu", (long)index];

Sample Project/TestBed/MasterViewController.m

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ @interface MasterViewController () <ARFontPickerViewControllerDelegate, UITextFi
9494
@property (strong, nonatomic) IBOutlet BEMSimpleLineGraphView *myGraph;
9595

9696
@property (strong, nonatomic) NSDictionary <NSString *, id> *methodList;
97+
@property (strong, nonatomic) IBOutlet UITextField *numberOfPointsField;
9798

9899
@property (strong, nonatomic) IBOutlet UITextField *widthLine;
99100
@property (strong, nonatomic) IBOutlet UITextField *staticPaddingField;
@@ -219,13 +220,15 @@ - (void)viewDidLoad {
219220
[self.detailViewController addObserver:self forKeyPath:@"newestDate" options:NSKeyValueObservingOptionNew context:nil];
220221
[self restoreProperties];
221222
[self restoreUI];
222-
223+
[self.detailViewController addObserver:self forKeyPath:@"numberOfPoints" options:NSKeyValueObservingOptionNew context:nil];
223224
}
224225

225226
-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
226-
// if ([keyPath isEqualToString:@"newestDate"]) {
227+
if ([keyPath isEqualToString:@"newestDate"]) {
227228
[self rangePlaceHolders:self];
228-
// }
229+
} else if ([keyPath isEqualToString:@"numberOfPoints"]) {
230+
self.numberOfPointsField.intValue = self.detailViewController.numberOfPoints ;
231+
}
229232
}
230233

231234
-(void) applicationWillResign:(id) sender {
@@ -309,6 +312,7 @@ -(void) restoreProperties {
309312
self.detailViewController.property = [defaults type ##ForKey:@#property]; \
310313
}
311314

315+
RestoreDetail (numberOfPoints, integer );
312316
RestoreDetail (percentNulls, float );
313317
RestoreDetail (popUpText, object);
314318
RestoreDetail (popUpPrefix, object);
@@ -396,6 +400,7 @@ -(void) saveProperties{
396400
[defaults setFloat:self.myGraph.labelFont.pointSize forKey:@"labelFontSize"] ;
397401

398402
#define EncodeDetail(property, type) [defaults set ## type: self.detailViewController.property forKey:@#property]
403+
EncodeDetail (numberOfPoints, Integer );
399404
EncodeDetail (percentNulls, Float );
400405

401406
EncodeDetail (popUpText, Object);
@@ -425,6 +430,7 @@ -(void) saveProperties{
425430

426431

427432
-(void) restoreUI {
433+
self.numberOfPointsField.intValue = self.detailViewController.numberOfPoints;
428434
self.widthLine.floatValue = self.myGraph.widthLine;
429435
self.staticPaddingField.floatValue = self.detailViewController.staticPaddingValue;
430436
self.bezierSwitch.on = self.myGraph.enableBezierCurve;
@@ -535,6 +541,12 @@ -(void) rangePlaceHolders: (id) sender {
535541
Gradient choices
536542
*/
537543

544+
- (IBAction)numberOfPointsDidChange:(UITextField *)sender {
545+
NSInteger value = sender.intValue;
546+
if (value >= 0 ) {
547+
self.detailViewController.numberOfPoints = value;
548+
}
549+
}
538550

539551
#pragma mark Main Line
540552
- (IBAction)widthLineDidChange:(UITextField *)sender {
@@ -879,6 +891,7 @@ -(void) updateAnimationGraphStyle {
879891
- (IBAction)animationGraphStyle:(UIButton *)sender {
880892
if (self.myGraph.animationGraphStyle == BEMLineAnimationNone) {
881893
self.myGraph.animationGraphStyle = BEMLineAnimationDraw;
894+
self.myGraph.animationGraphEntranceTime = self.animationEntranceTime.floatValue;
882895
} else {
883896
self.myGraph.animationGraphStyle ++;
884897
}
@@ -887,7 +900,7 @@ - (IBAction)animationGraphStyle:(UIButton *)sender {
887900
}
888901

889902
- (IBAction)animationGraphEntranceTimeDidChange:(UITextField *)sender {
890-
self.myGraph.animationGraphEntranceTime = (CGFloat) sender.text.floatValue;
903+
self.myGraph.animationGraphEntranceTime = (CGFloat) sender.floatValue;
891904
[self.myGraph reloadGraph];
892905
}
893906

@@ -1202,7 +1215,8 @@ -(BOOL) textFieldShouldReturn:(UITextField *)textField {
12021215

12031216
-(void) dealloc {
12041217
[[NSNotificationCenter defaultCenter] removeObserver:self];
1205-
[self.detailViewController removeObserver:self forKeyPath:@"totalNumber" ];
1218+
[self.detailViewController removeObserver:self forKeyPath:@"newestDate" ];
1219+
[self.detailViewController removeObserver:self forKeyPath:@"numberOfPoints" ];
12061220

12071221
}
12081222
@end

0 commit comments

Comments
 (0)