-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSCPlotCanvas.m
100 lines (73 loc) · 1.9 KB
/
SCPlotCanvas.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
//
// SCPlotCanvas.m
// Alarm-Clock
//
// Created by Robert Palmer on 30.01.10.
// Copyright 2010 Robert All rights reserved.
//
#import "SCPlotCanvas.h"
#import "SCGraph.h"
#import "SC2DScale.h"
@interface SCPlotCanvas (Private)
- (void)graphChanged;
- (NSRect)valueRangeRect;
@end
@implementation SCPlotCanvas
@synthesize width;
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
graphs = [[NSMutableArray alloc] init];
width = 1000;
}
return self;
}
- (void) dealloc
{
[graphs release];
[super dealloc];
}
- (void)drawRect:(NSRect)dirtyRect {
SC2DScale *scaler = [[[SC2DScale alloc] init] autorelease];
[scaler setFromRect: [self valueRangeRect]];
[scaler setToRect: [self bounds]];
CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSetRGBFillColor(myContext, 1, 1, 1, 1);
CGContextFillRect(myContext, dirtyRect);
for (SCGraph *graph in graphs) {
graph.scaler = scaler;
[graph draw: myContext];
}
}
- (void)addGraph: (SCGraph *)graph {
[graph addObserver:self forKeyPath:@"dirty" options:(NSKeyValueObservingOptionNew)
context:nil];
graph.dirty = NO;
[graphs addObject:graph];
[self graphChanged];
}
#pragma mark -
#pragma mark Private Methods
- (void)graphChanged {
[self setNeedsDisplay:YES];
}
- (NSRect)valueRangeRect {
if ([graphs count] == 0) {
return NSMakeRect(0, 0, width, 30);
}
float currentMaxWidth = [(SCGraph *)[graphs objectAtIndex:0] width];
float startingValue = currentMaxWidth - width;
if (startingValue < 0) {
startingValue = 0;
}
return NSMakeRect(startingValue, 0, width, 30);
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if ([[change valueForKey:@"new"] boolValue] == YES) {
[self graphChanged];
}
}
@end