-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPHGraphView.m
More file actions
157 lines (137 loc) · 3.27 KB
/
PHGraphView.m
File metadata and controls
157 lines (137 loc) · 3.27 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// PHGraphView.m
// Graph
//
// Created by Pierre-Henri Jondot on 30/03/08.
// Ported to iPhone by brian@fluidmedia.com 08-2008
//
#import "PHGraphView.h"
@implementation PHGraphView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
xAxis = [[NSMutableArray alloc] init];
yAxis = [[NSMutableArray alloc] init];
graphObjects = [[NSMutableArray alloc] init];
hasBorder = YES;
leftBorder = 20;
rightBorder = 20;
bottomBorder = 0;
topBorder = 125;
}
return self;
}
-(void)dealloc
{
[xAxis release];
[yAxis release];
[graphObjects release];
[super dealloc];
}
-(void)addPHxAxis:(PHxAxis*)axis
{
[xAxis addObject:axis];
}
-(void)addPHyAxis:(PHyAxis*)axis
{
[yAxis addObject:axis];
}
-(void)addPHGraphObject:(PHGraphObject*)object
{
[graphObjects addObject:object];
}
-(void)removePHxAxis:(PHxAxis*)axis
{
[xAxis removeObject:axis];
}
-(void)removePHyAxis:(PHyAxis*)axis
{
[yAxis removeObject:axis];
}
-(void)removePHGraphObject:(PHGraphObject*)object
{
[graphObjects removeObject:object];
}
//direct accessors to the arrays of axis and objects
-(NSMutableArray*)xAxisMutableArray
{
return xAxis;
}
-(NSMutableArray*)yAxisMutableArray
{
return yAxis;
}
-(NSMutableArray*)graphObjectsMutableArray
{
return graphObjects;
}
-(void)setXAxisMutableArray:(NSMutableArray*)anArray
{
[anArray retain];
[xAxis release];
xAxis = anArray;
}
-(void)setYAxisMutableArray:(NSMutableArray*)anArray
{
[anArray retain];
[yAxis release];
yAxis = anArray;
}
-(void)setGraphObjectsMutableArray:(NSMutableArray*)anArray
{
[anArray retain];
[graphObjects release];
graphObjects = anArray;
}
-(void)setHasBorder:(BOOL)value
{
hasBorder = value;
}
-(void)setLeftBorder:(float)newLeftBorder rightBorder:(float)newRightBorder
bottomBorder:(float)newBottomBorder topBorder:(float)newTopBorder
{
leftBorder = newLeftBorder;
rightBorder = newRightBorder;
bottomBorder = newBottomBorder;
topBorder = newTopBorder;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect arect = [self frame];
CGRect clippingArea = CGRectMake(arect.origin.x, arect.origin.y, arect.size.width, arect.size.height);
if (hasBorder) {
clippingArea.origin.x += leftBorder;
clippingArea.origin.y += bottomBorder;
clippingArea.size.width -= leftBorder+rightBorder;
clippingArea.size.height -= bottomBorder+topBorder;
}
CGRect drawingArea = CGRectMake(clippingArea.origin.x, clippingArea.origin.y, clippingArea.size.width, clippingArea.size.height);
int n = [graphObjects count], i;
for (i=0; i<n; i++)
{
PHGraphObject *object=(PHGraphObject*)[graphObjects objectAtIndex:i];
if (([object shouldDraw]) && (!([object isLongToDraw])))
{
CGContextSaveGState(context);
if (hasBorder) CGContextClipToRect(context,clippingArea);
[[graphObjects objectAtIndex:i] drawWithContext:context rect:drawingArea];
CGContextRestoreGState(context);
}
}
n=[xAxis count];
for (i=0; i<n; i++)
{
PHxAxis* axis = [xAxis objectAtIndex:i];
[axis setDrawOutside:hasBorder];
[axis drawWithContext:context rect:drawingArea];
}
n=[yAxis count];
for (i=0; i<n; i++)
{
PHyAxis* axis = [yAxis objectAtIndex:i];
[axis setDrawOutside:hasBorder];
[axis drawWithContext:context rect:drawingArea];
}
CGContextFlush(context);
}
@end