-
Notifications
You must be signed in to change notification settings - Fork 0
/
HistogramView.m
90 lines (75 loc) · 2.57 KB
/
HistogramView.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
//
// HistogramView.m
// FeatureViewer
//
// Created by Grogee on 1/18/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "HistogramView.h"
@implementation HistogramView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
axisFrame = NULL;
bars = NULL;
}
return self;
}
- (void) drawHistogram:(NSData*)counts andBins: (NSData*)bins
{
//draw a histogram based on the counts and bins; basically, draw rectangles of width equal to the bin widths
//and height equal to the count
//NSGraphicsContext* theContext = [NSGraphicsContext currentContext];
unsigned int _ncounts = [counts length]/sizeof(unsigned int);
unsigned int *_counts = (unsigned int*)[counts bytes];
double *_bins = (double*)[bins bytes];
unsigned int i;
//first draw the axis
CGFloat maxCount = 0.0;
//[frame stroke];
bars = [[NSBezierPath bezierPath] retain];
for(i=0;i<_ncounts-1;i++)
{
if( _counts[i]<maxCount )
{
maxCount = _counts[i];
}
[bars appendBezierPathWithRect:NSMakeRect(_bins[i]/_bins[_ncounts-1], 0.0, (_bins[i+1]-_bins[i])/_bins[_ncounts-1], _counts[i])];
}
NSAffineTransform *transform = [NSAffineTransform transform];
//scale such that maximum y is one
[transform scaleXBy:1.0 yBy:0.9/maxCount];
[bars transformUsingAffineTransform: transform];
axisFrame = [[NSBezierPath bezierPath] retain];
[axisFrame moveToPoint:NSMakePoint(0.0, 0.0)];
[axisFrame lineToPoint:NSMakePoint(0.0, 1.0)];
[axisFrame moveToPoint:NSMakePoint(0.0, 0.0)];
[axisFrame lineToPoint:NSMakePoint(1.0, 0.0)];
//[bars fill];
}
- (void)drawRect:(NSRect)dirtyRect {
// Drawing code here.
NSAffineTransform *transform = [NSAffineTransform transform];
//scale to window size
[transform scaleXBy:[self bounds].size.width yBy:[self bounds].size.height];
if( axisFrame != NULL )
{
[axisFrame transformUsingAffineTransform:transform];
[axisFrame stroke];
[axisFrame transformUsingAffineTransform:transform];
}
if( bars != NULL )
{
[bars transformUsingAffineTransform:transform];
[bars fill];
[bars transformUsingAffineTransform: transform];
}
[transform invert];
if( axisFrame != NULL)
[axisFrame transformUsingAffineTransform:transform];
if( bars != NULL)
[bars transformUsingAffineTransform:transform];
//reset for next drawing; could be slow
}
@end