Skip to content
This repository has been archived by the owner on Aug 17, 2022. It is now read-only.

Commit

Permalink
Refactoring, bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeylenkov committed Nov 16, 2011
1 parent 8323af4 commit f85464a
Show file tree
Hide file tree
Showing 13 changed files with 532 additions and 256 deletions.
7 changes: 3 additions & 4 deletions Classes/YBBarGraphView.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ - (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];

if (self) {
self.formatter = [[NSNumberFormatter alloc] init];
formatter = [[NSNumberFormatter alloc] init];

[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
Expand Down Expand Up @@ -76,10 +76,9 @@ - (id)initWithFrame:(NSRect)frame {
self.infoFont = [NSFont fontWithName:@"Helvetica Neue" size:12];
self.legendFont = [NSFont boldSystemFontOfSize:11];

self.marker = [[YBMarker alloc] init];

self.showMarker = NO;
marker = [[YBMarker alloc] init];

showMarker = NO;
enableMarker = YES;

NSTrackingAreaOptions trackingOptions = NSTrackingMouseMoved | NSTrackingMouseEnteredAndExited | NSTrackingActiveInActiveApp;
Expand Down
4 changes: 2 additions & 2 deletions Classes/YBBullet.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#import <Foundation/Foundation.h>

enum {
PTBulletTypeCircle = 0,
PTMarkerTypeSquare = 1
YBBulletTypeCircle = 0,
YBMarkerTypeSquare = 1
};

@interface YBBullet : NSObject {
Expand Down
8 changes: 5 additions & 3 deletions Classes/YBBullet.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ @implementation YBBullet
@synthesize type;

- (id)init {
if (self = [super init]) {
self = [super init];

if (self) {
self.color = [NSColor blackColor];
self.borderColor = [NSColor whiteColor];

Expand All @@ -30,7 +32,7 @@ - (id)init {
}

- (void)drawAtPoint:(NSPoint)point {
if (type == PTBulletTypeCircle) {
if (type == YBBulletTypeCircle) {
NSBezierPath *path = [NSBezierPath bezierPathWithOvalInRect:NSMakeRect(point.x - (size / 2), point.y - (size / 2), size, size)];

[path setLineWidth:borderWidht];
Expand All @@ -43,7 +45,7 @@ - (void)drawAtPoint:(NSPoint)point {
[path fill];
}

if (type == PTMarkerTypeSquare) {
if (type == YBMarkerTypeSquare) {
NSBezierPath *path = [NSBezierPath bezierPathWithRect:NSMakeRect(point.x - (size / 2), point.y - (size / 2), size, size)];

[path setLineWidth:borderWidht];
Expand Down
4 changes: 3 additions & 1 deletion Classes/YBChartView.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
NSFont *infoFont;
NSFont *legendFont;
NSColor *backgroundColor;
NSColor *textColor;
NSColor *textColor;
BOOL isGradient;
}

@property (nonatomic, retain) NSMutableArray *series;
Expand All @@ -42,6 +43,7 @@
@property (nonatomic, retain) NSFont *legendFont;
@property (nonatomic, retain) NSColor *backgroundColor;
@property (nonatomic, retain) NSColor *textColor;
@property (nonatomic, assign) BOOL isGradient;

- (void)draw;

Expand Down
108 changes: 65 additions & 43 deletions Classes/YBChartView.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#define OFFSET 20
#define OFFSET_WITH_INFO 30
#define OFFSET_LEGENT 140
#define OFFSET_LEGENT 60

@implementation YBChartView

Expand All @@ -22,10 +22,13 @@ @implementation YBChartView
@synthesize legendFont;
@synthesize backgroundColor;
@synthesize textColor;
@synthesize isGradient;

- (id)initWithFrame:(NSRect)frame {
if (self = [super initWithFrame:frame]) {
self.formatter = [[NSNumberFormatter alloc] init];
self = [super initWithFrame:frame];

if (self) {
formatter = [[NSNumberFormatter alloc] init];

[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
Expand All @@ -45,10 +48,11 @@ - (id)initWithFrame:(NSRect)frame {
self.font = [NSFont boldSystemFontOfSize:11];
self.legendFont = [NSFont boldSystemFontOfSize:11];
self.infoFont = [NSFont systemFontOfSize:11];
self.isGradient = NO;

marker = [[YBMarker alloc] init];

self.marker = [[YBMarker alloc] init];

self.showMarker = NO;
showMarker = NO;
hideMarker = NO;
enableMarker = YES;

Expand Down Expand Up @@ -95,14 +99,6 @@ - (void)drawRect:(NSRect)rect {

return;
}

int offset = 0.0;

if (drawInfo) {
offset = OFFSET_WITH_INFO;
} else {
offset = OFFSET;
}

float max = 0.0;

Expand All @@ -118,7 +114,7 @@ - (void)drawRect:(NSRect)rect {
float startAngle = 0.0;

if (percent == 0.0) {
percent == 1;
percent = 1.0;
}

NSMutableArray *chartSeries = [[NSMutableArray alloc] init];
Expand Down Expand Up @@ -152,8 +148,8 @@ - (void)drawRect:(NSRect)rect {
[chartValues addObject:[NSNumber numberWithFloat:other]];
}

float chartSpaceHeight = rect.size.height;
float chartSpaceWidth = rect.size.width;
float chartSpaceHeight;// = rect.size.height;
float chartSpaceWidth;// = rect.size.width;

if (drawLegend) {
chartSpaceWidth = rect.size.width - OFFSET_LEGENT;
Expand Down Expand Up @@ -193,20 +189,20 @@ - (void)drawRect:(NSRect)rect {

YBPointInfo *pointInfo = nil;

float radius = chartSpaceHeight / 2;

if (chartSpaceHeight > chartSpaceWidth) {
radius = chartSpaceWidth / 2;
}

radius = radius - OFFSET * 2;

for (int i = 0; i < [chartValues count]; i++) {
float percents = [[chartValues objectAtIndex:i] floatValue] / percent;
float endAngle = startAngle + percents * 3.6;

NSBezierPath *path = [NSBezierPath bezierPath];
[path setLineWidth:0.1];

float radius = chartSpaceHeight / 2;

if (chartSpaceHeight > chartSpaceWidth) {
radius = chartSpaceWidth / 2;
}

radius = radius - OFFSET * 2;

[path moveToPoint:center];
[path appendBezierPathWithArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle];
Expand All @@ -219,10 +215,10 @@ - (void)drawRect:(NSRect)rect {
}
} else {
[[YBChartView colorByIndex:-1] set];
}
}
[path fill];
if ([path containsPoint:mousePoint]) {
pointInfo = [[[YBPointInfo alloc] init] autorelease];
pointInfo.x = mousePoint.x;
Expand All @@ -235,9 +231,9 @@ - (void)drawRect:(NSRect)rect {
}

if (i < maxChartsCount) {
marker.backgroundColor = [YBChartView markerColorByIndex:i];
//marker.backgroundColor = [YBChartView markerColorByIndex:i];
} else {
marker.backgroundColor = [YBChartView markerColorByIndex:-1];
//marker.backgroundColor = [YBChartView markerColorByIndex:-1];
}
}

Expand Down Expand Up @@ -268,11 +264,11 @@ - (void)drawRect:(NSRect)rect {

//NSString *title = [chartSeries objectAtIndex:i];

radius = chartSpaceHeight / 2;
/*radius = chartSpaceHeight / 2;
if (chartSpaceHeight > chartSpaceWidth) {
radius = chartSpaceWidth / 2;
}
}*/

fromPoint.x = fromPoint.x + center.x;
fromPoint.y = fromPoint.y + center.y;
Expand Down Expand Up @@ -327,6 +323,19 @@ - (void)drawRect:(NSRect)rect {
startAngle = startAngle + percents * 3.6;
}

if (isGradient) {
NSColor *color = [NSColor colorWithDeviceRed:0.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:0.15];

NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:color endingColor:[color highlightWithLevel:0.6]];

NSBezierPath *path = [NSBezierPath bezierPath];
[path moveToPoint:center];
[path appendBezierPathWithArcWithCenter:center radius:radius startAngle:0.0 endAngle:360.0];
[gradient drawInBezierPath:path angle:90.0];

[gradient release];
}

// draw marker

if (showMarker && !hideMarker && enableMarker && pointInfo != nil) {
Expand All @@ -342,12 +351,25 @@ - (void)drawRect:(NSRect)rect {
if (drawLegend) {
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setAlignment:NSLeftTextAlignment];

[paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];

int n = 0;

int width = 0;

for (int i = 0; i < [chartSeries count]; i++) {
NSString *legend = [chartSeries objectAtIndex:i];

NSDictionary *attsDict = [NSDictionary dictionaryWithObjectsAndKeys:legendFont, NSFontAttributeName, [NSNumber numberWithInt:NSNoUnderlineStyle], NSUnderlineStyleAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];
NSSize size = [legend sizeWithAttributes:attsDict];

if (size.width > width) {
width = size.width;
}
}

for (int i = 0; i < [chartValues count]; i++) {
if ([[chartValues objectAtIndex:i] floatValue] > 0) {
int top = rect.size.height - 80;
int top = rect.size.height - OFFSET_LEGENT;

NSDictionary *attsDict = [NSDictionary dictionaryWithObjectsAndKeys:textColor, NSForegroundColorAttributeName, font, NSFontAttributeName, [NSNumber numberWithInt:NSNoUnderlineStyle], NSUnderlineStyleAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];

Expand All @@ -357,8 +379,8 @@ - (void)drawRect:(NSRect)rect {
[[YBChartView colorByIndex:-1] set];
}

NSRectFill(NSMakeRect(rect.size.width - (OFFSET_LEGENT + 20), top - n * 30, 10, 10));
[[chartSeries objectAtIndex:i] drawInRect:NSMakeRect(rect.size.width - OFFSET_LEGENT, top - n * 30, OFFSET_LEGENT - 10, 16) withAttributes:attsDict];
NSRectFill(NSMakeRect(rect.size.width - width - OFFSET_LEGENT - 20, top - n * 30, 10, 10));
[[chartSeries objectAtIndex:i] drawInRect:NSMakeRect(rect.size.width - width - OFFSET_LEGENT, (top - n * 30) - 4, width, 16) withAttributes:attsDict];

n = n + 1;
}
Expand Down Expand Up @@ -389,19 +411,19 @@ + (NSColor *)colorByIndex:(NSInteger)index {

switch (index) {
case 0:
color = [NSColor colorWithDeviceRed:5/255.0 green:141/255.0 blue:199/255.0 alpha:1.0];
color = [NSColor colorWithDeviceRed:1/255.0 green:165/255.0 blue:218/255.0 alpha:1.0];
break;
case 1:
color = [NSColor colorWithDeviceRed:80/255.0 green:180/255.0 blue:50/255.0 alpha:1.0];
case 1:
color = [NSColor colorWithDeviceRed:122/255.0 green:184/255.0 blue:37/255.0 alpha:1.0];
break;
case 2:
color = [NSColor colorWithDeviceRed:255/255.0 green:102/255.0 blue:0/255.0 alpha:1.0];
color = [NSColor colorWithDeviceRed:202/255.0 green:85/255.0 blue:43/255.0 alpha:1.0];
break;
case 3:
color = [NSColor colorWithDeviceRed:255/255.0 green:158/255.0 blue:1/255.0 alpha:1.0];
color = [NSColor colorWithDeviceRed:241/255.0 green:182/255.0 blue:49/255.0 alpha:1.0];
break;
case 4:
color = [NSColor colorWithDeviceRed:252/255.0 green:210/255.0 blue:2/255.0 alpha:1.0];
color = [NSColor colorWithDeviceRed:129/255.0 green:52/255.0 blue:79/255.0 alpha:1.0];
break;
case 5:
color = [NSColor colorWithDeviceRed:248/255.0 green:255/255.0 blue:1/255.0 alpha:1.0];
Expand Down
27 changes: 19 additions & 8 deletions Classes/YBGraphView.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@
BOOL drawInfo;
NSString *info;
BOOL drawLegend;
BOOL zeroAsMinValue;

BOOL useMinValue;
float minValue;
BOOL isRevert;

NSTrackingArea *trackingArea;
NSPoint mousePoint;

BOOL showMarker;
BOOL hideMarker;
BOOL enableMarker;

BOOL showMarkerNearPoint;
YBMarker *marker;
NSFont *font;
NSFont *infoFont;
Expand All @@ -40,10 +42,12 @@
NSColor *textColor;
CGFloat lineWidth;
BOOL drawBullet;
YBBullet *bullet;

YBBullet *bullet;
BOOL fillGraph;

BOOL drawBottomMarker;
NSInteger gridYCount;
NSInteger roundGridYTo;
BOOL isRoundGridY;
id delegate;
id dataSource;
}
Expand All @@ -68,13 +72,20 @@
@property (nonatomic, retain) NSColor *textColor;
@property (nonatomic, assign) CGFloat lineWidth;
@property (nonatomic, assign) BOOL drawBullet;
@property (nonatomic, assign) BOOL zeroAsMinValue;
@property (nonatomic, assign) BOOL useMinValue;
@property (nonatomic, assign) float minValue;
@property (nonatomic, assign) BOOL isRevert;
@property (nonatomic, assign) BOOL fillGraph;
@property (nonatomic, assign) BOOL drawBottomMarker;
@property (nonatomic, assign) NSInteger gridYCount;
@property (nonatomic, assign) NSInteger roundGridYTo;
@property (nonatomic, assign) BOOL isRoundGridY;
@property (nonatomic, assign) BOOL showMarkerNearPoint;

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

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

@end

Expand Down
Loading

0 comments on commit f85464a

Please sign in to comment.