Skip to content
This repository was archived by the owner on Dec 2, 2020. It is now read-only.

Commit 7dc9f3a

Browse files
committed
Add colors, gradients, and alphas to TestBed
Fix many bugs and buglets
1 parent 7823735 commit 7dc9f3a

26 files changed

Lines changed: 4242 additions & 1274 deletions

Sample Project/SimpleLineChart.xcodeproj/project.pbxproj

Lines changed: 821 additions & 748 deletions
Large diffs are not rendered by default.

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

Lines changed: 882 additions & 113 deletions
Large diffs are not rendered by default.

Sample Project/TestBed/DetailViewController.m

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// MSColorComponentView.h
3+
//
4+
// Created by Maksym Shcheglov on 2014-02-12.
5+
//
6+
// The MIT License (MIT)
7+
// Copyright (c) 2015 Maksym Shcheglov
8+
//
9+
// Permission is hereby granted, free of charge, to any person obtaining a copy
10+
// of this software and associated documentation files (the "Software"), to deal
11+
// in the Software without restriction, including without limitation the rights
12+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
// copies of the Software, and to permit persons to whom the Software is
14+
// furnished to do so, subject to the following conditions:
15+
//
16+
// The above copyright notice and this permission notice shall be included in
17+
// all copies or substantial portions of the Software.
18+
//
19+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
// THE SOFTWARE.
26+
27+
#import <UIKit/UIKit.h>
28+
29+
@class MSSliderView;
30+
31+
/**
32+
* The view to edit a color component.
33+
*/
34+
@interface MSColorComponentView : UIControl
35+
36+
/**
37+
* The title.
38+
*/
39+
@property (nonatomic, copy) NSString *title;
40+
41+
/**
42+
* The current value. The default value is 0.0.
43+
*/
44+
@property (nonatomic, assign) CGFloat value;
45+
46+
/**
47+
* The minimum value. The default value is 0.0.
48+
*/
49+
@property (nonatomic, assign) CGFloat minimumValue;
50+
51+
/**
52+
* The maximum value. The default value is 255.0.
53+
*/
54+
@property (nonatomic, assign) CGFloat maximumValue;
55+
56+
/**
57+
* The format string to use apply for textfield value. \c %.f by default.
58+
*/
59+
@property (nonatomic, copy) NSString *format;
60+
61+
/**
62+
* Sets the array of CGColorRef objects defining the color of each gradient stop on a slider's track.
63+
* The location of each gradient stop is evaluated with formula: i * width_of_the_track / number_of_colors.
64+
*
65+
* @param colors An array of CGColorRef objects.
66+
*/
67+
- (void)setColors:(NSArray *)colors __attribute__((nonnull(1)));
68+
69+
@end

0 commit comments

Comments
 (0)