@@ -46,18 +46,7 @@ - (void)viewDidLoad {
4646
4747 [self hydrateDatasets ];
4848
49- CGFloat sum= 0 ;
50- for (NSNumber * number in self.arrayOfValues ) {
51- CGFloat n = number.doubleValue ;
52-
53- if (n <= BEMNullGraphValue) {
54- sum += n;
55- }
56- }
57- // The labels to report the values of the graph when the user touches it
58- self.labelValues .text = [NSString stringWithFormat: @" %i " , (int ) sum];
59- self.labelDates .text = @" between now and later" ;
60-
49+ [self updateLabelsBelowGraph: self .myGraph];
6150}
6251
6352#pragma mark Data management
@@ -82,7 +71,7 @@ - (void)hydrateDatasets {
8271 } else {
8372 [self .arrayOfDates addObject: [self dateForGraphAfterDate: self .arrayOfDates[i-1 ]]]; // Dates for the X-Axis of the graph
8473 }
85- if (showNullValue && i == 4 ) {
74+ if (showNullValue && (i % 4 == 0 ) ) {
8675 self.arrayOfValues [i] = @(BEMNullGraphValue);
8776 } else {
8877 self.totalNumber = self.totalNumber + [[self .arrayOfValues objectAtIndex: i] intValue ]; // All of the values added together
@@ -129,7 +118,13 @@ - (IBAction)addOrRemovePointFromGraph:(id)sender {
129118
130119- (void ) addPointToGraph {
131120 // Add point
132- [self .arrayOfValues addObject: @([self getRandomFloat ])];
121+ NSNumber * newValue ;
122+ if (self.arrayOfValues .count % 4 == 0 ) {
123+ newValue = @(BEMNullGraphValue);
124+ } else {
125+ newValue = @([self getRandomFloat ]);
126+ }
127+ [self .arrayOfValues addObject: newValue];
133128 NSDate *lastDate = self.arrayOfDates .count > 0 ? [self .arrayOfDates lastObject ]: [NSDate date ];
134129 NSDate *newDate = [self dateForGraphAfterDate: lastDate];
135130 [self .arrayOfDates addObject: newDate];
@@ -144,19 +139,24 @@ - (void) removePointFromGraph {
144139 [self .myGraph reloadGraph ];
145140 }
146141}
142+ -(NSString *) formatNumber : (NSNumber *) number {
143+ return [NSNumberFormatter localizedStringFromNumber: number
144+ numberStyle: NSNumberFormatterDecimalStyle];
145+
146+ }
147147
148148- (void )prepareForSegue : (UIStoryboardSegue *)segue sender : (id )sender {
149149 [super prepareForSegue: segue sender: sender];
150150
151151 if ([segue.identifier isEqualToString: @" showStats" ]) {
152152 BEMGraphCalculator * calc = [BEMGraphCalculator sharedCalculator ];
153153 StatsViewController *controller = segue.destinationViewController ;
154- controller.standardDeviation = [NSString stringWithFormat: @" %.2f " , [[ calc calculateStandardDeviationOnGraph: self .myGraph] doubleValue ]];
155- controller.average = [NSString stringWithFormat: @" %.2f " , [[ calc calculatePointValueAverageOnGraph: self .myGraph] doubleValue ]];
156- controller.median = [NSString stringWithFormat: @" %.2f " , [[ calc calculatePointValueMedianOnGraph: self .myGraph] doubleValue ]];
157- controller.mode = [NSString stringWithFormat: @" %.2f " , [[ calc calculatePointValueModeOnGraph: self .myGraph] doubleValue ]];
158- controller.minimum = [NSString stringWithFormat: @" %.2f " , [[ calc calculateMinimumPointValueOnGraph: self .myGraph] doubleValue ]];
159- controller.maximum = [NSString stringWithFormat: @" %.2f " , [[ calc calculateMaximumPointValueOnGraph: self .myGraph] doubleValue ]];
154+ controller.standardDeviation = [self formatNumber: [ calc calculateStandardDeviationOnGraph: self .myGraph]];
155+ controller.average = [self formatNumber: [ calc calculatePointValueAverageOnGraph: self .myGraph]];
156+ controller.median = [self formatNumber: [ calc calculatePointValueMedianOnGraph: self .myGraph]];
157+ controller.mode = [self formatNumber: [ calc calculatePointValueModeOnGraph: self .myGraph]];
158+ controller.minimum = [self formatNumber: [ calc calculateMinimumPointValueOnGraph: self .myGraph]];
159+ controller.maximum = [self formatNumber: [ calc calculateMaximumPointValueOnGraph: self .myGraph]];
160160 controller.snapshotImage = [self .myGraph graphSnapshotImage ];
161161 }
162162}
@@ -165,7 +165,7 @@ - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
165165#pragma mark - SimpleLineGraph Data Source
166166
167167- (NSUInteger )numberOfPointsInLineGraph : (BEMSimpleLineGraphView *)graph {
168- return ( int ) [self .arrayOfValues count ];
168+ return [self .arrayOfValues count ];
169169}
170170
171171- (CGFloat)lineGraph : (BEMSimpleLineGraphView *)graph valueForPointAtIndex : (NSUInteger )index {
@@ -233,12 +233,16 @@ - (NSString *)popUpPrefixForlineGraph:(BEMSimpleLineGraphView *)graph {
233233}
234234
235235-(NSString *) popUpTextForlineGraph : (BEMSimpleLineGraphView *)graph atIndex : (NSUInteger )index {
236- if (!self.popUpText ) return @" Invalid format string" ;
236+ if (!self.popUpText ) return @" Empty format string" ;
237+ @try {
237238#pragma clang diagnostic push
238239#pragma clang diagnostic ignored "-Wformat-nonliteral"
239- return [NSString stringWithFormat: self .popUpText, index];
240+ return [NSString stringWithFormat: self .popUpText, index];
240241#pragma clang diagnostic pop
241-
242+ } @catch (NSException *exception) {
243+ return [NSString stringWithFormat: @" Invalid format string: %@ " , exception ];
244+ }
245+
242246}
243247
244248- (BOOL )lineGraph : (BEMSimpleLineGraphView *)graph alwaysDisplayPopUpAtIndex : (NSUInteger )index {
@@ -272,7 +276,7 @@ - (void)lineGraph:(BEMSimpleLineGraphView *)graph modifyPopupView:(UIView *)popu
272276 NSAssert (popupView == self.customView, @" View problem" );
273277#pragma clang diagnostic push
274278#pragma clang diagnostic ignored "-Wformat-nonliteral"
275- if (!self.myGraph .formatStringForValues ) return ;
279+ if (!self.myGraph .formatStringForValues . length ) return ;
276280 self.customViewLabel .text = [NSString stringWithFormat: self .myGraph.formatStringForValues, [self lineGraph: graph valueForPointAtIndex: index] ];
277281#pragma pop
278282}
@@ -326,33 +330,36 @@ - (CGFloat)incrementValueForYAxisOnLineGraph:(BEMSimpleLineGraphView *)graph {
326330#pragma mark Touch handling
327331
328332- (void )lineGraph : (BEMSimpleLineGraphView *)graph didTouchGraphWithClosestIndex : (NSUInteger )index {
329- self.labelValues .text = [NSString stringWithFormat: @" %@ " , [self .arrayOfValues objectAtIndex: index]];
330- self.labelDates .text = [NSString stringWithFormat: @" in %@ " , [self labelForDateAtIndex: index]];
333+ self.labelValues .text = [self formatNumber: [self .arrayOfValues objectAtIndex: index]];
334+ self.labelDates .text = [NSString stringWithFormat: @" on %@ " , [self labelForDateAtIndex: index]];
331335}
332336
333337- (void )lineGraph : (BEMSimpleLineGraphView *)graph didReleaseTouchFromGraphWithClosestIndex : (CGFloat)index {
334338 [UIView animateWithDuration: 0.2 delay: 0 options: UIViewAnimationOptionCurveEaseOut animations: ^{
335339 self.labelValues .alpha = 0.0 ;
336340 self.labelDates .alpha = 0.0 ;
337341 } completion: ^(BOOL finished) {
338- self.labelValues .text = [NSString stringWithFormat: @" %i " , [[BEMGraphCalculator sharedCalculator ] calculatePointValueSumOnGraph: graph].intValue];
339- self.labelDates .text = [NSString stringWithFormat: @" between %@ and %@ " , [self labelForDateAtIndex: 0 ], [self labelForDateAtIndex: self .arrayOfDates.count - 1 ]];
340-
342+ [self updateLabelsBelowGraph: graph];
341343 [UIView animateWithDuration: 0.5 delay: 0 options: UIViewAnimationOptionCurveEaseOut animations: ^{
342344 self.labelValues .alpha = 1.0 ;
343345 self.labelDates .alpha = 1.0 ;
344346 } completion: nil ];
345347 }];
346348}
347349
348- - (void )lineGraphDidFinishLoading : (BEMSimpleLineGraphView *)graph {
350+ -(void ) updateLabelsBelowGraph : (BEMSimpleLineGraphView *)graph {
349351 if (self.arrayOfValues .count > 0 ) {
350- self.labelValues .text = [NSString stringWithFormat: @" %i " , [[BEMGraphCalculator sharedCalculator ] calculatePointValueSumOnGraph: graph].intValue];
352+ NSNumber * sum = [[BEMGraphCalculator sharedCalculator ] calculatePointValueSumOnGraph: graph];
353+ self.labelValues .text =[self formatNumber: sum];
351354 self.labelDates .text = [NSString stringWithFormat: @" between %@ and %@ " , [self labelForDateAtIndex: 0 ], [self labelForDateAtIndex: self .arrayOfDates.count - 1 ]];
352355 } else {
353356 self.labelValues .text = @" No data" ;
354357 self.labelDates .text = @" " ;
355358 }
356359}
357360
361+ - (void )lineGraphDidFinishLoading : (BEMSimpleLineGraphView *)graph {
362+ [self updateLabelsBelowGraph: graph];
363+ }
364+
358365@end
0 commit comments