-
Notifications
You must be signed in to change notification settings - Fork 0
/
CWGraphPlotView.m
175 lines (130 loc) · 5.31 KB
/
CWGraphPlotView.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
// CWGraphicPlotView.m
// MMCourseWork
//
// Created by Alexey Streltsow on 12/1/09.
// Copyright 2009 Karma World LLC. All rights reserved.
//
#import "CWGraphPlotView.h"
#import "CWGraphPlotDataSource.h"
const int AXIS_SPACING = 20;
const int GUIDE_SPACING = 40;
const int GUIDE_LENGTH = 5;
@interface CWGraphPlotView ( )
- (void)drawAxisToContext:(NSGraphicsContext*)context;
@property (nonatomic, retain) CWGraphPlotViewAxis* horAxis;
@property (nonatomic, retain) CWGraphPlotViewAxis* vertAxis;
@end
@implementation CWGraphPlotView
@synthesize dataSources = _dataSources;
@synthesize delegate = _delegate;
@synthesize precision = _precision;
@synthesize horAxis = _horAxis, vertAxis = _vertAxis;
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
_dataSources = [[NSMutableArray array] retain];
}
return self;
}
- (void) reloadData {
// reset axises
self.horAxis = nil;
self.vertAxis = nil;
[self setNeedsDisplay:YES];
}
- (void)drawAxisToContext:(NSGraphicsContext*)context {
self.horAxis = [_delegate graphicPlotView:self axisWithType:CWGraphPlotAxisHorizontal];
self.vertAxis = [_delegate graphicPlotView:self axisWithType:CWGraphPlotAxisVertical];
[self.horAxis drawWithType:CWGraphPlotAxisHorizontal context:context];
[self.vertAxis drawWithType:CWGraphPlotAxisVertical context:context];
}
- (void)drawRect:(NSRect)dirtyRect {
NSGraphicsContext* context = [NSGraphicsContext currentContext];
// make background color
NSBezierPath *bp = [NSBezierPath bezierPathWithRect:NSMakeRect(0, 0, self.frame.size.width, self.frame.size.height)];
[[NSColor whiteColor] set];
[bp fill];
[self drawAxisToContext:context];
if ( !_horAxis )
{
NSAssert(0, @"no horizontal axis!");
return;
}
double minYValue = INFINITY;
double maxYValue = -INFINITY;
NSMutableArray* paths = [NSMutableArray array];
for (id<CWGraphPlotViewDataSource> dataSource in _dataSources) {
if ( ![dataSource enabled] ) {
continue;
}
int capacity = (_horAxis.axisMaxValue - _horAxis.axisMinValue) / _horAxis.step;
NSMutableArray* points = [NSMutableArray arrayWithCapacity:capacity];
// get values from datasource and find global extremum for all datasources
for (double curArg = _horAxis.axisMinValue; curArg <= _horAxis.axisMaxValue; curArg += [dataSource rangeStep]) {
if ( ! [dataSource canProvideDataForArgument:curArg] ) {
continue;
}
double value = [dataSource graphicPlotView:self valueForArgument:curArg];
minYValue = MIN( minYValue, value );
maxYValue = MAX( maxYValue, value );
[points addObject: [NSValue valueWithPoint:NSPointFromCGPoint(CGPointMake(curArg, value))]];
}
[paths addObject:points];
}
if ( minYValue != INFINITY && maxYValue != INFINITY ) {
double size = (maxYValue - minYValue);
double hsize = (_horAxis.axisMaxValue - _horAxis.axisMinValue);
if ( size == 0 ) size = 1.0;
if ( hsize == 0 ) hsize = 1.0;
double yratio = self.frame.size.height / size;
if ( yratio < 0 ) yratio *= (-1.0);
double xratio = self.frame.size.width / hsize;
// draw guides
[[NSColor grayColor] setStroke];
// horizontal guide
NSDictionary* fontAttributes = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSFont fontWithName:@"Helvetica" size:9.0], [NSColor grayColor], nil] forKeys:[NSArray arrayWithObjects:NSFontAttributeName, NSForegroundColorAttributeName, nil]];
NSBezierPath* hguide = [NSBezierPath bezierPath];
for( int i = 1; i < self.frame.size.width / GUIDE_SPACING+1; ++i) {
[hguide moveToPoint:NSPointFromCGPoint(CGPointMake(i * GUIDE_SPACING, 0))];
[hguide lineToPoint:NSPointFromCGPoint(CGPointMake(i * GUIDE_SPACING, GUIDE_LENGTH))];
NSString* title = [NSString stringWithFormat:@"%.2f", i * GUIDE_SPACING * 1.0 / xratio];
NSSize sizeTitle = [title sizeWithAttributes:fontAttributes];
[title drawAtPoint:NSPointFromCGPoint(CGPointMake(i*GUIDE_SPACING-sizeTitle.width/2, GUIDE_LENGTH)) withAttributes:fontAttributes];
}
[hguide stroke];
// vertical guide
NSBezierPath* vguide = [NSBezierPath bezierPath];
for( int i = 1; i < self.frame.size.height / GUIDE_SPACING + 1; ++i) {
[vguide moveToPoint:NSPointFromCGPoint(CGPointMake(0, i * GUIDE_SPACING))];
[vguide lineToPoint:NSPointFromCGPoint(CGPointMake(GUIDE_LENGTH, i * GUIDE_SPACING))];
NSString* title = [NSString stringWithFormat:@"%.2f", i * GUIDE_SPACING * 1.0 / yratio];
NSSize sizeTitle = [title sizeWithAttributes:fontAttributes];
[title drawAtPoint:NSPointFromCGPoint(CGPointMake(GUIDE_LENGTH, i*GUIDE_SPACING-sizeTitle.height/2)) withAttributes:fontAttributes];
}
[vguide stroke];
NSAffineTransform* xform = [NSAffineTransform transform];
[xform scaleXBy:xratio yBy:yratio];
//[xform concat];
int i = 0;
for (NSMutableArray* points in paths) {
NSBezierPath* aPath = [NSBezierPath bezierPath];
[aPath moveToPoint:NSMakePoint(0, 0)];
NSColor* lineColor = [[_dataSources objectAtIndex:i++] plotColor];
[lineColor setStroke];
for (NSValue* point in points) {
NSPoint aPoint = [point pointValue];
aPoint = [xform transformPoint:aPoint];
[aPath lineToPoint:aPoint ];
}
[aPath stroke];
}
}
[super drawRect:dirtyRect];
}
- (void) dealloc {
[_dataSources release];
[super dealloc];
}
@end