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
|
//
// MATimeGraph.m
// MySQL Administrator
//
// Created by Alfredo Kojima on Thu Jul 22 2004.
// Copyright (c) 2004 MySQL AB. All rights reserved.
//
#import "MATimeGraph.h"
#include <glib.h>
@implementation MATimeGraph
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
_gridColor= [[NSColor colorWithDeviceRed:0.162 green:0.288 blue:0.358 alpha:0.8] retain];
_graphColor= [[NSColor colorWithDeviceRed:0.2 green:0.6 blue:1.0 alpha:1.0] retain];
_maxSamples= 1024;
_samples= g_new(MATimeGraphSample, _maxSamples);
_attr= [[NSDictionary alloc] init];
_caption= nil;
_rangeMin= 0;
_rangeMax= 100;
_gridWidth= 20;
_pixelsPerSecond= 1;
}
return self;
}
- (void)dealloc
{
g_free(_samples);
[_attr release];
[_caption release];
[_grid release];
[_graph release];
[_gridColor release];
[_graphColor release];
[super dealloc];
}
- (void)setRangeMin:(float)min max:(float)max
{
_rangeMin= min;
_rangeMax= max;
}
- (void)drawRect:(NSRect)rect
{
[[NSColor blackColor] set];
NSRectFill(rect);
[_gridColor set];
[_grid stroke];
[_graphColor set];
[_graph stroke];
if (_caption)
{
[_caption drawAtPoint:NSMakePoint(5,3)
withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSColor whiteColor],NSForegroundColorAttributeName,
[NSFont systemFontOfSize:9.0], NSFontAttributeName,nil]];
}
}
- (void)addValue:(double)value
timestamp:(time_t)ts
{
MATimeGraphSample sample;
sample.value= value;
sample.ts= ts;
if (_sampleCount < _maxSamples)
_sampleCount++;
memmove(_samples+1, _samples, sizeof(*_samples)*(_sampleCount-1));
_samples[0]= sample;
[self updateGraph];
[self setNeedsDisplay:YES];
}
- (void)setCaption:(NSString*)caption
{
if (_caption != caption)
{
[_caption release];
_caption= [caption retain];
}
}
- (void)updateGraph
{
int parts;
int i;
time_t time0;
NSSize size= [self frame].size;
float x,y;
float gridHeight;
time_t prevTime;
if (_grid) [_grid release];
if (_graph) [_graph release];
_grid= [[NSBezierPath alloc] init];
[_grid setLineWidth:0.0];
_graph= [[NSBezierPath alloc] init];
[_graph setLineWidth:0.0];
gridHeight= (_rangeMax-_rangeMin)/5;
// horiz grid
parts= (_rangeMax - _rangeMin) / gridHeight;
for (i= 0; i < parts; i++)
{
[_grid moveToPoint:NSMakePoint(1,(size.height/parts)*i+0.5)];
[_grid lineToPoint:NSMakePoint(size.width-1,(size.height/parts)*i+0.5)];
}
if (_sampleCount==0)
time0= 0;
else
time0= _samples[0].ts;
// vert grid
if (_lastTime != time0)
{
if (_lastTime > 0)
{
_gridOffset+= (int)((time0-_lastTime)*_pixelsPerSecond);
}
_gridOffset%= (int)_gridWidth;
_lastTime= time0;
}
for (i= size.width-1-_gridOffset; i > 0; i-= _gridWidth)
{
[_grid moveToPoint:NSMakePoint(i+0.5, 1)];
[_grid lineToPoint:NSMakePoint(i+0.5, size.height-2)];
}
// graph
x= size.width-2;
prevTime= 0;
[_graph moveToPoint:NSMakePoint(x, 2)];
for (i= 0; i < _sampleCount; i++)
{
double xdelta;
xdelta= (prevTime ? (_samples[i].ts - prevTime) : 0.0);
x = x + xdelta * _pixelsPerSecond;
y= 1 + ((_samples[i].value - _rangeMin) * (size.height-2)) / (_rangeMax - _rangeMin);
if (y < 1)
y= 1;
if (x < 0)
break;
[_graph lineToPoint:NSMakePoint(x,y)];
prevTime= _samples[i].ts;
}
if (x > 0)
{
// start at 0
x -= 1;
y = 0.0;
[_graph lineToPoint:NSMakePoint(x,y)];
}
}
- (void)getStatsMin:(double*)min max:(double*)max average:(double*)avg
{
int i;
double mn, mx, av;
mn= 0;
mx= 0;
av= 0;
for (i= 0; i < _sampleCount; i++)
{
if (mn > _samples[i].value)
mn= _samples[i].value;
if (mx < _samples[i].value)
mx= _samples[i].value;
av+= _samples[i].value;
}
if (_sampleCount > 0)
av/= _sampleCount;
else
av= 0;
*min= mn;
*max= mx;
*avg= av;
}
@end
|