public
Description: My OS X Thermometer client.
Clone URL: git://github.com/dustin/osx-thermometer.git
Search Repo:
osx-thermometer / SparklineCell.m
100644 241 lines (202 sloc) 6.126 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
// SparklineCell.m
// VisualStatWatch
//
// Created by Dustin Sallings on 2005/5/5.
// Copyright 2005 Dustin Sallings <dustin@spy.net>. All rights reserved.
//
 
#import "SparklineCell.h"
 
#include <time.h>
 
#define MAX_READINGS 100
 
// ------------------------------------------------------------------------
// SparklineDatum
// ------------------------------------------------------------------------
 
@implementation SparklineDatum
 
-(id)initWithTimestamp:(long)ts value:(NSNumber *)v
{
  id rv=[super init];
  timestamp=ts;
  if(v == nil) {
    value=[NSNumber numberWithInt: 0];
  } else {
    value=[v retain];
  }
  return(rv);
}
 
-(long)timestamp
{
  return(timestamp);
}
 
-(NSNumber *)value
{
  return(value);
}
 
-(NSString *)description
{
  return([NSString stringWithFormat: @"{SparklineDatum ts=%ld %@}",
    timestamp, value]);
}
 
@end
 
// ------------------------------------------------------------------------
// SparklineCell
// ------------------------------------------------------------------------
 
@implementation SparklineCell
 
-(id)init
{
  id rv=[super init];
  return(rv);
}
 
// Translate an absolute NSPoint to a tracking point.
-(NSPoint)translatePoint:(NSPoint)p
{
  return(NSMakePoint(p.x - trackingCell.origin.x,
    p.y - trackingCell.origin.y));
}
 
- (BOOL)trackMouse:(NSEvent *)theEvent inRect:(NSRect)cellFrame
  ofView:(NSView *)controlView untilMouseUp:(BOOL)untilMouseUp
{
  trackingCell=cellFrame;
  trackingView=controlView;
  displaying=NO;
 
  NSHelpManager *helpManager = [NSHelpManager sharedHelpManager];
 
  [super trackMouse:theEvent inRect:cellFrame ofView:controlView
    untilMouseUp:untilMouseUp];
  return(YES);
}
 
- (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView
{
  NSPoint translatedPoint=[self translatePoint: startPoint];
  BOOL defaultRv=[super startTrackingAt:startPoint inView:controlView];
  return(YES);
}
 
- (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint
  inView:(NSView *)controlView mouseIsUp:(BOOL)flag
{
  [super stopTracking:lastPoint at:stopPoint
    inView:controlView mouseIsUp:flag];
  // NSLog(@"Stopped tracking");
  NSHelpManager *helpManager = [NSHelpManager sharedHelpManager];
  [helpManager removeContextHelpForObject:trackingView];
}
 
- (BOOL)continueTracking:(NSPoint)lastPoint at:(NSPoint)currentPoint
    inView:(NSView *)controlView
{
  NSPoint translatedPoint=[self translatePoint: currentPoint];
 
  id vals=[self objectValue];
  double pixdiff=trackingCell.size.width/((double)[vals count]-1.0);
  if(pixdiff > 1) {
    pixdiff=1;
  }
  // Calulate the array offset as a function of the X offset and the pixdiff
  int offset=(int)(translatedPoint.x * pixdiff);
 
  // Figure out where we want the help
  NSWindow *mainWindow=[trackingView window];
  NSRect windowPos=[mainWindow frame];
  NSPoint relHelpPoint=NSMakePoint(currentPoint.x-24, currentPoint.y+25);
  NSPoint helpPoint=NSMakePoint(
    windowPos.origin.x + relHelpPoint.x,
    (windowPos.origin.y + windowPos.size.height)
      - (trackingCell.size.height + relHelpPoint.y));
 
  if([vals count] > offset) {
    SparklineDatum *datum=[vals objectAtIndex: offset];
    NSDate *when=[NSDate dateWithTimeIntervalSince1970: [datum timestamp]];
    NSString *msg=[NSString stringWithFormat: @"%@ at %@",
      [datum value], when];
 
    NSHelpManager *helpManager = [NSHelpManager sharedHelpManager];
    [helpManager setContextHelp:[[[NSAttributedString alloc]
      initWithString:msg] autorelease] forObject:trackingView];
    displaying=YES;
  }
 
  if(displaying) {
    NSHelpManager *helpManager = [NSHelpManager sharedHelpManager];
    [helpManager showContextHelpForObject:trackingView
      locationHint:helpPoint];
  }
 
  BOOL defaultRv=[super continueTracking:lastPoint at:currentPoint
    inView:controlView];
  return(YES);
}
 
-(void)plotVals:(NSArray *)vals inRect:(NSRect)rect
{
  double pixdiff=rect.size.width/((double)[vals count]-1.0);
  if(pixdiff > 1) {
    pixdiff=1;
  }
  NSEnumerator *enumerator = [vals objectEnumerator];
  id object=nil;
  double x=0;
  NSBezierPath *path=[NSBezierPath bezierPath];
  while(object = [enumerator nextObject]) {
    double v=[[object value] doubleValue];
    double newx=rect.origin.x + x;
    double rangeDiff=(maxValue - minValue);
    double valPercent=(maxValue - v)/rangeDiff;
    double newy=rect.origin.y+(valPercent*(double)rect.size.height);
 
    if(newy > rect.origin.y + rect.size.height) {
      NSLog(@"newy exceeded maximum value (was %.2f)", newy);
      newy=rect.origin.y + rect.size.height;
    }
    if(newy < rect.origin.y) {
      NSLog(@"newy fell below minimum value (was %.2f)", newy);
      newy=rect.origin.y;
    }
 
    NSPoint p=NSMakePoint(newx, newy);
    if(x < rect.origin.x + rect.size.width) {
      NSString *op=nil;
 
      if(x == 0) {
        op=@"move";
        [path moveToPoint: p];
      } else {
        op=@"line";
        [path lineToPoint: p];
      }
      /*
      NSLog(@"\t%.2f(%.2f:%.2f) %@ to %.0fx%.0f in %.0f,%.0f - %.0f,%.0f",
        v, minValue, maxValue, op, newx, newy,
        rect.origin.x, rect.origin.y,
        rect.origin.x + rect.size.width,
          rect.origin.y + rect.size.height);
      */
    }
    x+=pixdiff;
  }
  [path stroke];
}
 
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
  [super drawInteriorWithFrame:cellFrame inView:controlView];
  NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
 
  [[NSColor blackColor] set];
 
  if([self objectValue] != nil && [[self objectValue] count] > 1) {
    // Plot the vals
    [self plotVals:[self objectValue] inRect:cellFrame];
  }
 
  [pool release];
}
 
-(void)setObjectValue:(id)val
{
  [super setObjectValue:val];
 
  if([val count] > 0) {
    double firstValue=[[[val objectAtIndex:0] value] doubleValue];
    minValue=firstValue;
    maxValue=firstValue;
    NSEnumerator *e=[val reverseObjectEnumerator];
    // Figure out the min and max
    id datum=nil;
    while((datum = [e nextObject]) != nil) {
      // NSLog(@"Dealing with %@", num);
  
      if(datum != nil) {
        double val=[[datum value] doubleValue];
        if(val > maxValue) {
          // NSLog(@"New max is %lld", val);
          maxValue=val;
        }
        if(val < minValue) {
          // NSLog(@"New min is %lld", val);
          minValue=val;
        }
      }
    } // Flipping through data
  } // hasValues
}
 
@end