-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphView.m
66 lines (55 loc) · 1.87 KB
/
GraphView.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
//
// GraphView.m
// GraphCalculator
//
// Created by Luca Finzi Contini on 02/08/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "GraphView.h"
#import "AxesDrawer.h"
@implementation GraphView
@synthesize dataSource = _dataSource; // datasource is set by a class that implments the protocol.
- (void) setup
{
self.contentMode = UIViewContentModeRedraw;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setup];
}
NSLog(@"-- INIT WITH FRAME : new size = (%f, %f)", self.bounds.size.width, self.bounds.size.height);
return self;
}
- (void) awakeFromNib
{
NSLog(@"-- ENTER awakeFromNib : new size = (%f, %f)", self.bounds.size.width, self.bounds.size.height);
[self setup];
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
/*
MVC.
Graph model = scale, origin, vector of points.
*/
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
// call the class that implements the dataSource
NSDictionary *data=[self.dataSource getDataForGraphView:self];
NSNumber *ox = (NSNumber *)[data objectForKey:@"ox"];
NSNumber *oy = (NSNumber *)[data objectForKey:@"oy"];
NSNumber *scale = (NSNumber *)[data objectForKey:@"scale"];
NSArray *Yvalues = (NSArray *)[data objectForKey:@"Yvalues"];
CGContextMoveToPoint(context, 0, [[Yvalues objectAtIndex:0] doubleValue]);
for ( CGFloat X = 1; X < Yvalues.count; X ++ ){
CGContextAddLineToPoint(context, X, [[Yvalues objectAtIndex:X] doubleValue]);
}
CGContextStrokePath(context);
CGPoint o = CGPointMake(ox.doubleValue, oy.doubleValue);
[AxesDrawer drawAxesInRect:rect originAtPoint:o scale:scale.doubleValue];
}
@end