Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Custom view for markers in graph
  • Loading branch information
sergeylenkov committed Jan 18, 2012
1 parent 44cd5a6 commit 9947cd8
Show file tree
Hide file tree
Showing 12 changed files with 342 additions and 61 deletions.
3 changes: 2 additions & 1 deletion Classes/YBGraphView.h
Expand Up @@ -20,6 +20,7 @@
- (NSColor *)graphView:(YBGraphView *)graph colorForGraph:(NSInteger)index;
- (NSString *)graphView:(YBGraphView *)graph legendTitleForGraph:(NSInteger)index;
- (NSString *)graphView:(YBGraphView *)graph markerTitleForGraph:(NSInteger)graphIndex forElement:(NSInteger)elementIndex;
- (NSView *)graphView:(YBGraphView *)graph markerViewForGraph:(NSInteger)graphIndex forElement:(NSInteger)elementIndex;

@required

Expand Down Expand Up @@ -76,6 +77,7 @@
BOOL isRoundGridY;
id delegate;
id dataSource;
NSMutableArray *_customMarkers;
}

@property (nonatomic, retain) NSNumberFormatter *formatter;
Expand Down Expand Up @@ -109,7 +111,6 @@
@property (nonatomic, assign) IBOutlet id <YBGraphViewDataSource> dataSource;

- (void)draw;
- (void)drawLegendInRect:(NSRect)rect;

+ (NSColor *)colorByIndex:(NSInteger)index;

Expand Down
157 changes: 105 additions & 52 deletions Classes/YBGraphView.m
Expand Up @@ -13,6 +13,13 @@
#define OFFSET_WITH_INFO_Y 60
#define OFFSET_LEGENT 160

@interface YBGraphView (Private)

- (void)drawLegendInRect:(NSRect)rect;
- (void)drawCustomView:(NSView *)view atPoint:(NSPoint)point inRect:(NSRect)rect;

@end

@implementation YBGraphView

@synthesize formatter;
Expand Down Expand Up @@ -45,6 +52,26 @@ @implementation YBGraphView
@synthesize isRoundGridY;
@synthesize showMarkerNearPoint;

- (void)dealloc {
[series release];
[graphs release];
[legends release];
[formatter release];
[info release];
[marker release];
[bullet release];
[backgroundColor release];
[textColor release];
[font release];
[infoFont release];
[legends release];
[_customMarkers release];

[self removeTrackingArea:trackingArea];

[super dealloc];
}

- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];

Expand All @@ -62,7 +89,8 @@ - (id)initWithFrame:(NSRect)frame {
series = [[NSMutableArray alloc] init];
graphs = [[NSMutableArray alloc] init];
legends = [[NSMutableArray alloc] init];

_customMarkers = [[NSMutableArray alloc] init];

drawInfo = NO;
info = @"";

Expand Down Expand Up @@ -532,6 +560,8 @@ - (void)drawRect:(NSRect)rect {

pointInfo.x = startPoint.x;
pointInfo.y = startPoint.y;
pointInfo.graph = i;
pointInfo.element = j;

if ([dataSource respondsToSelector:@selector(graphView: markerTitleForGraph: forElement:)]) {
pointInfo.title = [dataSource graphView:self markerTitleForGraph:i forElement:j];
Expand All @@ -548,7 +578,9 @@ - (void)drawRect:(NSRect)rect {

pointInfo.x = startPoint.x;
pointInfo.y = startPoint.y;

pointInfo.graph = i;
pointInfo.element = j;

if ([dataSource respondsToSelector:@selector(graphView: markerTitleForGraph: forElement:)]) {
pointInfo.title = [dataSource graphView:self markerTitleForGraph:i forElement:j];
} else {
Expand Down Expand Up @@ -615,6 +647,8 @@ - (void)drawRect:(NSRect)rect {

pointInfo.x = lastPoint.x;
pointInfo.y = lastPoint.y;
pointInfo.graph = i;
pointInfo.element = [values count] - 1;

if (![[[graphs objectAtIndex:i] objectAtIndex:[values count] - 1] isKindOfClass:[NSNull class]]) {
if ([dataSource respondsToSelector:@selector(graphView: markerTitleForGraph: forElement:)]) {
Expand All @@ -634,7 +668,9 @@ - (void)drawRect:(NSRect)rect {

pointInfo.x = lastPoint.x;
pointInfo.y = lastPoint.y;

pointInfo.graph = i;
pointInfo.element = [values count] - 1;

if (![[[graphs objectAtIndex:i] objectAtIndex:[values count] - 1] isKindOfClass:[NSNull class]]) {
if ([dataSource respondsToSelector:@selector(graphView: markerTitleForGraph: forElement:)]) {
pointInfo.title = [dataSource graphView:self markerTitleForGraph:i forElement:[values count] - 1];
Expand Down Expand Up @@ -675,15 +711,25 @@ - (void)drawRect:(NSRect)rect {
// draw markers

if (showMarker && !hideMarker && enableMarker) {
int i = 0;

for (NSView *view in _customMarkers) {
[view removeFromSuperview];
}

[_customMarkers removeAllObjects];

for (YBPointInfo *pointInfo in markers) {
NSPoint point;
point.x = pointInfo.x;
point.y = pointInfo.y;

[marker drawAtPoint:point inRect:rect withTitle:pointInfo.title];
i++;
if ([dataSource respondsToSelector:@selector(graphView: markerViewForGraph: forElement:)]) {
NSView *customMarker = [dataSource graphView:self markerViewForGraph:pointInfo.graph forElement:pointInfo.element];

[self drawCustomView:customMarker atPoint:point inRect:rect];
[_customMarkers addObject:customMarker];
} else {
[marker drawAtPoint:point inRect:rect withTitle:pointInfo.title];
}
}
}

Expand Down Expand Up @@ -736,6 +782,58 @@ - (void)drawLegendInRect:(NSRect)rect {
[paragraphStyle release];
}

- (void)drawCustomView:(NSView *)customView atPoint:(NSPoint)point inRect:(NSRect)rect {
NSSize size = customView.bounds.size;
int offsetY = 4;

if (point.y + size.height > rect.size.height) {
offsetY = (size.height + 4) * -1;
}

[customView setFrameOrigin:NSMakePoint(point.x - (size.width / 2), point.y + offsetY)];
[self addSubview:customView];
}


#pragma mark -
#pragma mark Mouse Events
#pragma mark -

- (void)mouseDown:(NSEvent *)event {
enableMarker = !enableMarker;
[self setNeedsDisplay:YES];
}

- (void)mouseEntered:(NSEvent *)event {
hideMarker = NO;
[self setNeedsDisplay:YES];
}

- (void)mouseExited:(NSEvent *)event {
hideMarker = YES;
[self setNeedsDisplay:YES];
}

- (void)mouseMoved:(NSEvent *)event {
NSPoint location = [event locationInWindow];
mousePoint = [self convertPoint:location fromView:nil];

[self setNeedsDisplay:YES];
}

- (void)updateTrackingAreas {
[self removeTrackingArea:trackingArea];

NSTrackingAreaOptions trackingOptions = NSTrackingMouseMoved | NSTrackingMouseEnteredAndExited | NSTrackingActiveInActiveApp;

trackingArea = [[[NSTrackingArea alloc] initWithRect:[self bounds] options:trackingOptions owner:self userInfo:nil] autorelease];
[self addTrackingArea:trackingArea];
}

#pragma mark -
#pragma mark Utilites
#pragma mark -

+ (NSColor *)colorByIndex:(NSInteger)index {
NSColor *color;

Expand Down Expand Up @@ -778,49 +876,4 @@ + (NSColor *)colorByIndex:(NSInteger)index {
return color;
}

- (void)mouseDown:(NSEvent *)event {
enableMarker = !enableMarker;
[self setNeedsDisplay:YES];
}

- (void)mouseEntered:(NSEvent *)event {
hideMarker = NO;
[self setNeedsDisplay:YES];
}

- (void)mouseExited:(NSEvent *)event {
hideMarker = YES;
[self setNeedsDisplay:YES];
}

- (void)mouseMoved:(NSEvent *)event {
NSPoint location = [event locationInWindow];
mousePoint = [self convertPoint:location fromView:nil];

[self setNeedsDisplay:YES];
}

- (void)updateTrackingAreas {
[self removeTrackingArea:trackingArea];

NSTrackingAreaOptions trackingOptions = NSTrackingMouseMoved | NSTrackingMouseEnteredAndExited | NSTrackingActiveInActiveApp;

trackingArea = [[[NSTrackingArea alloc] initWithRect:[self bounds] options:trackingOptions owner:self userInfo:nil] autorelease];
[self addTrackingArea:trackingArea];
}

- (void)dealloc {
[series release];
[graphs release];
[legends release];
[formatter release];
[info release];
[font release];
[marker release];
[backgroundColor release];
[textColor release];
[self removeTrackingArea:trackingArea];
[super dealloc];
}

@end
2 changes: 0 additions & 2 deletions Classes/YBMarker.h
Expand Up @@ -21,7 +21,6 @@ enum {
NSColor *borderColor;
NSInteger borderWidht;
NSInteger type;
NSInteger position;
BOOL shadow;
}

Expand All @@ -31,7 +30,6 @@ enum {
@property (nonatomic, retain) NSColor *borderColor;
@property (nonatomic, assign) NSInteger borderWidht;
@property (nonatomic, assign) NSInteger type;
@property (nonatomic, assign) NSInteger position;
@property (nonatomic, assign) BOOL shadow;

- (void)drawAtPoint:(NSPoint)point inRect:(NSRect)rect withTitle:(NSString *)title;
Expand Down
3 changes: 0 additions & 3 deletions Classes/YBMarker.m
Expand Up @@ -16,7 +16,6 @@ @implementation YBMarker
@synthesize borderColor;
@synthesize borderWidht;
@synthesize type;
@synthesize position;
@synthesize shadow;

- (id)init {
Expand All @@ -32,7 +31,6 @@ - (id)init {

self.borderWidht = 2;
self.type = 0;
self.position = 0;
self.shadow = NO;
}

Expand Down Expand Up @@ -207,7 +205,6 @@ - (void)drawAtPoint:(NSPoint)point inRect:(NSRect)rect withTitle:(NSString *)tit
[backgroundColor set];
[path fill];


int offsetX = rectWidth - width;
int offsetY = rectHeight - height;

Expand Down
4 changes: 4 additions & 0 deletions Classes/YBPointInfo.h
Expand Up @@ -12,10 +12,14 @@
NSInteger x;
NSInteger y;
NSString *title;
NSInteger graph;
NSInteger element;
}

@property (nonatomic, assign) NSInteger x;
@property (nonatomic, assign) NSInteger y;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, assign) NSInteger graph;
@property (nonatomic, assign) NSInteger element;

@end
2 changes: 2 additions & 0 deletions Classes/YBPointInfo.m
Expand Up @@ -13,6 +13,8 @@ @implementation YBPointInfo
@synthesize x;
@synthesize y;
@synthesize title;
@synthesize graph;
@synthesize element;

- (void)dealloc {
[title release];
Expand Down
11 changes: 11 additions & 0 deletions Examples/Graph/Graph.xcodeproj/project.pbxproj
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
A5208B6814C6EE8C0054668E /* MarkerView.m in Sources */ = {isa = PBXBuildFile; fileRef = A5208B6714C6EE8C0054668E /* MarkerView.m */; };
A5208B7B14C7284B0054668E /* Tooltip.png in Resources */ = {isa = PBXBuildFile; fileRef = A5208B7A14C7284A0054668E /* Tooltip.png */; };
A544BCCF147FFA43003D21EF /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A544BCCE147FFA43003D21EF /* Cocoa.framework */; };
A544BCD9147FFA44003D21EF /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = A544BCD7147FFA44003D21EF /* InfoPlist.strings */; };
A544BCDB147FFA44003D21EF /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = A544BCDA147FFA44003D21EF /* main.m */; };
Expand All @@ -20,6 +22,9 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
A5208B6614C6EE8C0054668E /* MarkerView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MarkerView.h; sourceTree = "<group>"; };
A5208B6714C6EE8C0054668E /* MarkerView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MarkerView.m; sourceTree = "<group>"; };
A5208B7A14C7284A0054668E /* Tooltip.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Tooltip.png; sourceTree = "<group>"; };
A544BCCA147FFA43003D21EF /* Graph.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Graph.app; sourceTree = BUILT_PRODUCTS_DIR; };
A544BCCE147FFA43003D21EF /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
A544BCD1147FFA43003D21EF /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -94,6 +99,7 @@
A544BCD4147FFA43003D21EF /* Graph */ = {
isa = PBXGroup;
children = (
A5208B7A14C7284A0054668E /* Tooltip.png */,
A544BCF4147FFA85003D21EF /* YBBullet.h */,
A544BCF5147FFA85003D21EF /* YBBullet.m */,
A544BCEB147FFA62003D21EF /* YBGraphView.h */,
Expand All @@ -104,6 +110,8 @@
A544BCF0147FFA62003D21EF /* YBPointInfo.m */,
A544BCE0147FFA44003D21EF /* AppDelegate.h */,
A544BCE1147FFA44003D21EF /* AppDelegate.m */,
A5208B6614C6EE8C0054668E /* MarkerView.h */,
A5208B6714C6EE8C0054668E /* MarkerView.m */,
A544BCE3147FFA44003D21EF /* MainMenu.xib */,
A544BCD5147FFA44003D21EF /* Supporting Files */,
);
Expand Down Expand Up @@ -176,6 +184,7 @@
A544BCD9147FFA44003D21EF /* InfoPlist.strings in Resources */,
A544BCDF147FFA44003D21EF /* Credits.rtf in Resources */,
A544BCE5147FFA44003D21EF /* MainMenu.xib in Resources */,
A5208B7B14C7284B0054668E /* Tooltip.png in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -192,6 +201,7 @@
A544BCF2147FFA62003D21EF /* YBMarker.m in Sources */,
A544BCF3147FFA62003D21EF /* YBPointInfo.m in Sources */,
A544BCF6147FFA85003D21EF /* YBBullet.m in Sources */,
A5208B6814C6EE8C0054668E /* MarkerView.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -311,6 +321,7 @@
A544BCEA147FFA44003D21EF /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
2 changes: 2 additions & 0 deletions Examples/Graph/Graph/AppDelegate.h
Expand Up @@ -8,10 +8,12 @@

#import <Cocoa/Cocoa.h>
#import "YBGraphView.h"
#import "MarkerView.h"

@interface AppDelegate : NSObject <NSApplicationDelegate> {
NSMutableArray *series;
NSMutableArray *values;
IBOutlet MarkerView *markerView;
}

@property (assign) IBOutlet NSWindow *window;
Expand Down

0 comments on commit 9947cd8

Please sign in to comment.