Skip to content

Commit

Permalink
Modified the sample project to show some various new options that the…
Browse files Browse the repository at this point in the history
… framework supports. Some of these implementions are in comments as to not chance the behavior of the sample project too much.
Rob Day authored and Rob Day committed Feb 27, 2015
1 parent c5110b3 commit c04ac0a
Showing 3 changed files with 137 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Classes/BEMSimpleLineGraphView.m
Original file line number Diff line number Diff line change
@@ -641,7 +641,7 @@ - (void)drawXAxis {

NSString *xAxisLabelText = [self xAxisTextForIndex:startingIndex];

UILabel *labelXAxis = [self xAxisLabelWithText:xAxisLabelText atIndex:index];
UILabel *labelXAxis = [self xAxisLabelWithText:xAxisLabelText atIndex:startingIndex];
[xAxisLabels addObject:labelXAxis];

if(self.positionYAxisRight) {
Original file line number Diff line number Diff line change
@@ -12,13 +12,17 @@
<dict>
<key>1CA8AD815677B6FB0DD135F65EC4576AE68426CF</key>
<string>https://github.com/Boris-Em/BEMSimpleLineGraph.git</string>
<key>3A216D26EB93D9EB79DE1E013DBEF75CF4509620</key>
<string>https://github.dowjones.net/DJMobile/ios-thesituation.git</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>Sample Project/SimpleLineChart.xcodeproj</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>1CA8AD815677B6FB0DD135F65EC4576AE68426CF</key>
<string>../../..</string>
<key>3A216D26EB93D9EB79DE1E013DBEF75CF4509620</key>
<string>../../../..</string>
</dict>
<key>IDESourceControlProjectURL</key>
<string>https://github.com/Boris-Em/BEMSimpleLineGraph.git</string>
@@ -28,6 +32,14 @@
<string>1CA8AD815677B6FB0DD135F65EC4576AE68426CF</string>
<key>IDESourceControlProjectWCConfigurations</key>
<array>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
<key>IDESourceControlWCCIdentifierKey</key>
<string>3A216D26EB93D9EB79DE1E013DBEF75CF4509620</string>
<key>IDESourceControlWCCName</key>
<string></string>
</dict>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
152 changes: 124 additions & 28 deletions Sample Project/SimpleLineChart/ViewController.m
Original file line number Diff line number Diff line change
@@ -25,18 +25,8 @@ - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.arrayOfValues = [[NSMutableArray alloc] init];
self.arrayOfDates = [[NSMutableArray alloc] init];

previousStepperValue = self.graphObjectIncrement.value;
totalNumber = 0;

for (int i = 0; i < 9; i++) {
[self.arrayOfValues addObject:@([self getRandomInteger])]; // Random values for the graph
[self.arrayOfDates addObject:[NSString stringWithFormat:@"%@", @(2000 + i)]]; // Dates for the X-Axis of the graph

totalNumber = totalNumber + [[self.arrayOfValues objectAtIndex:i] intValue]; // All of the values added together
}

[self hydrateDatasets];

/* This is commented out because the graph is created in the interface with this sample app. However, the code remains as an example for creating the graph using code.
BEMSimpleLineGraphView *myGraph = [[BEMSimpleLineGraphView alloc] initWithFrame:CGRectMake(0, 60, 320, 250)];
@@ -70,6 +60,12 @@ - (void)viewDidLoad {
self.myGraph.enableReferenceAxisFrame = YES;
self.myGraph.animationGraphStyle = BEMLineAnimationDraw;

//Dash the y reference lines
self.myGraph.lineDashPatternForReferenceYAxisLines = @[@(2),@(2)];

//Show the y axis values with this format string
self.myGraph.formatStringForValues = @"%.1f";

//setup initial selectet segment
self.curveChoice.selectedSegmentIndex = self.myGraph.enableBezierCurve;

@@ -83,18 +79,53 @@ - (void)didReceiveMemoryWarning {
// Dispose of any resources that can be recreated.
}

#pragma mark - Graph Actions

- (IBAction)refresh:(id)sender {
- (void)hydrateDatasets {
if(!self.arrayOfValues) self.arrayOfValues = [[NSMutableArray alloc] init];
if(!self.arrayOfDates) self.arrayOfDates = [[NSMutableArray alloc] init];
[self.arrayOfValues removeAllObjects];
[self.arrayOfDates removeAllObjects];

for (int i = 0; i < self.graphObjectIncrement.value; i++) {
[self.arrayOfValues addObject:@([self getRandomInteger])]; // Random values for the graph
[self.arrayOfDates addObject:[NSString stringWithFormat:@"%@", @(2000 + i)]]; // Dates for the X-Axis of the graph
previousStepperValue = self.graphObjectIncrement.value;
totalNumber = 0;
NSDate *baseDate = [NSDate date];
BOOL showNullValue = true;


for (int i = 0; i < 9; i++) {
[self.arrayOfValues addObject:@([self getRandomFloat])]; // Random values for the graph
if (i == 0) {
[self.arrayOfDates addObject:baseDate]; // Dates for the X-Axis of the graph
} else if (showNullValue && i == 4) {
[self.arrayOfDates addObject:[self dateForGraphAfterDate:self.arrayOfDates[i-1]]]; // Dates for the X-Axis of the graph
self.arrayOfValues[i] = @(BEMNullGraphValue);
} else {
[self.arrayOfDates addObject:[self dateForGraphAfterDate:self.arrayOfDates[i-1]]]; // Dates for the X-Axis of the graph
}


totalNumber = totalNumber + [(self.arrayOfValues)[i] intValue]; // All of the values added together
totalNumber = totalNumber + [[self.arrayOfValues objectAtIndex:i] intValue]; // All of the values added together
}

}

- (NSDate *)dateForGraphAfterDate:(NSDate *)date {
NSTimeInterval secondsInTwelveHours = 12 * 60 * 60;
NSDate *newDate = [date dateByAddingTimeInterval:secondsInTwelveHours];
return newDate;
}

- (NSString *)labelForDateAtIndex:(NSInteger)index {
NSDate *date = self.arrayOfDates[index];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateFormat = @"MM/dd";
NSString *label = [df stringFromDate:date];
return label;
}

#pragma mark - Graph Actions

- (IBAction)refresh:(id)sender {
[self hydrateDatasets];
UIColor *color;
if (self.graphColorChoice.selectedSegmentIndex == 0) color = [UIColor colorWithRed:31.0/255.0 green:187.0/255.0 blue:166.0/255.0 alpha:1.0];
else if (self.graphColorChoice.selectedSegmentIndex == 1) color = [UIColor colorWithRed:255.0/255.0 green:187.0/255.0 blue:31.0/255.0 alpha:1.0];
@@ -112,17 +143,18 @@ - (IBAction)refresh:(id)sender {
[self.myGraph reloadGraph];
}

- (NSInteger)getRandomInteger
- (float)getRandomFloat
{
NSInteger i1 = (int)(arc4random() % 10000);
float i1 = (float)(arc4random() % 1000000) / 100 ;
return i1;
}

- (IBAction)addOrRemoveLineFromGraph:(id)sender {
if (self.graphObjectIncrement.value > previousStepperValue) {
// Add line
[self.arrayOfValues addObject:@([self getRandomInteger])];
[self.arrayOfDates addObject:[NSString stringWithFormat:@"%i", (int) [[self.arrayOfDates lastObject] integerValue] + 1]];
[self.arrayOfValues addObject:@([self getRandomFloat])];
NSDate *newDate = [self dateForGraphAfterDate:(NSDate *)[self.arrayOfDates lastObject]];
[self.arrayOfDates addObject:newDate];
[self.myGraph reloadGraph];
} else if (self.graphObjectIncrement.value < previousStepperValue) {
// Remove line
@@ -153,14 +185,15 @@ - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
}
}


#pragma mark - SimpleLineGraph Data Source

- (NSInteger)numberOfPointsInLineGraph:(BEMSimpleLineGraphView *)graph {
return (int)[self.arrayOfValues count];
}

- (CGFloat)lineGraph:(BEMSimpleLineGraphView *)graph valueForPointAtIndex:(NSInteger)index {
return [[self.arrayOfValues objectAtIndex:index] floatValue];
return [[self.arrayOfValues objectAtIndex:index] doubleValue];
}

#pragma mark - SimpleLineGraph Delegate
@@ -170,13 +203,14 @@ - (NSInteger)numberOfGapsBetweenLabelsOnLineGraph:(BEMSimpleLineGraphView *)grap
}

- (NSString *)lineGraph:(BEMSimpleLineGraphView *)graph labelOnXAxisForIndex:(NSInteger)index {
NSString *label = [self.arrayOfDates objectAtIndex:index];

NSString *label = [self labelForDateAtIndex:index];
return [label stringByReplacingOccurrencesOfString:@" " withString:@"\n"];
}

- (void)lineGraph:(BEMSimpleLineGraphView *)graph didTouchGraphWithClosestIndex:(NSInteger)index {
self.labelValues.text = [NSString stringWithFormat:@"%@", [self.arrayOfValues objectAtIndex:index]];
self.labelDates.text = [NSString stringWithFormat:@"in %@", [self.arrayOfDates objectAtIndex:index]];
self.labelDates.text = [NSString stringWithFormat:@"in %@", [self labelForDateAtIndex:index]];
}

- (void)lineGraph:(BEMSimpleLineGraphView *)graph didReleaseTouchFromGraphWithClosestIndex:(CGFloat)index {
@@ -185,7 +219,7 @@ - (void)lineGraph:(BEMSimpleLineGraphView *)graph didReleaseTouchFromGraphWithCl
self.labelDates.alpha = 0.0;
} completion:^(BOOL finished) {
self.labelValues.text = [NSString stringWithFormat:@"%i", [[self.myGraph calculatePointValueSum] intValue]];
self.labelDates.text = [NSString stringWithFormat:@"between %@ and %@", [self.arrayOfDates firstObject], [self.arrayOfDates lastObject]];
self.labelDates.text = [NSString stringWithFormat:@"between %@ and %@", [self labelForDateAtIndex:0], [self labelForDateAtIndex:self.arrayOfDates.count - 1]];

[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.labelValues.alpha = 1.0;
@@ -196,7 +230,69 @@ - (void)lineGraph:(BEMSimpleLineGraphView *)graph didReleaseTouchFromGraphWithCl

- (void)lineGraphDidFinishLoading:(BEMSimpleLineGraphView *)graph {
self.labelValues.text = [NSString stringWithFormat:@"%i", [[self.myGraph calculatePointValueSum] intValue]];
self.labelDates.text = [NSString stringWithFormat:@"between %@ and %@", [self.arrayOfDates firstObject], [self.arrayOfDates lastObject]];
self.labelDates.text = [NSString stringWithFormat:@"between %@ and %@", [self labelForDateAtIndex:0], [self labelForDateAtIndex:self.arrayOfDates.count - 1]];
}

#pragma mark - Optional Datasource Customizations
/*
This section holds a bunch of graph customizations that can be made. They are commented out because they aren't required. If you choose to uncomment some, they will override some of the other delegate and datasource methods above.
*/

//- (NSInteger)baseIndexForXAxisOnLineGraph:(BEMSimpleLineGraphView *)graph {
// return 0;
//}
//
//- (NSInteger)incrementIndexForXAxisOnLineGraph:(BEMSimpleLineGraphView *)graph {
// return 2;
//}

//- (NSArray *)incrementPositionsForXAxisOnLineGraph:(BEMSimpleLineGraphView *)graph {
// NSMutableArray *positions = [NSMutableArray array];
// NSCalendar *calendar = [NSCalendar currentCalendar];
// NSInteger previousDay = -1;
// for(int i = 0; i < self.arrayOfDates.count; i++) {
// NSDate *date = self.arrayOfDates[i];
// NSDateComponents * components = [calendar components:NSCalendarUnitDay fromDate:date];
// NSInteger day = components.day;
// if(day != previousDay) {
// [positions addObject:@(i)];
// previousDay = day;
// }
// }
// return positions;
//
//}
//
//- (CGFloat)baseValueForYAxisOnLineGraph:(BEMSimpleLineGraphView *)graph {
// NSNumber *minValue = [graph calculateMinimumPointValue];
// //Let's round our value down to the nearest 100
// double min = minValue.doubleValue;
// double roundPrecision = 100;
// double offset = roundPrecision / 2;
// double roundedVal = round((min - offset) / roundPrecision) * roundPrecision;
// return roundedVal;
//}
//
//- (CGFloat)incrementValueForYAxisOnLineGraph:(BEMSimpleLineGraphView *)graph {
// NSNumber *minValue = [graph calculateMinimumPointValue];
// NSNumber *maxValue = [graph calculateMaximumPointValue];
// double range = maxValue.doubleValue - minValue.doubleValue;
// float increment = 1.0;
// if (range < 10) {
// increment = 2;
// } else if (range < 100) {
// increment = 10;
// } else if (range < 500) {
// increment = 50;
// } else if (range < 1000) {
// increment = 100;
// } else if (range < 5000) {
// increment = 500;
// } else {
// increment = 1000;
// }
// return increment;
//}

@end

0 comments on commit c04ac0a

Please sign in to comment.