Skip to content

Commit

Permalink
Two additioanl graphs for incorrect responses
Browse files Browse the repository at this point in the history
  • Loading branch information
doubt72 committed Apr 1, 2013
1 parent fc1da20 commit 45e191c
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 30 deletions.
15 changes: 13 additions & 2 deletions Sokudoku/Interface/GraphView.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,28 @@
#import <Cocoa/Cocoa.h>
@class Package;

typedef enum
{
AVG_SPEED = 0,
STUDY_TIME = 1,
CHARACTER_COUNT = 2,
CORRECT_RATE = 3,
STUDY_TIME_WITH_SPEED = 4,
CORRECT_RATE_WITH_COUNT = 5
} graphTypes;

@interface GraphView : NSView {
@private
Package *package;

NSString *tag;
int timeFrame;
int graphType;
graphTypes graphType;
}

@property Package *package;
@property NSString *tag;
@property int timeFrame, graphType;
@property int timeFrame;
@property graphTypes graphType;

@end
116 changes: 88 additions & 28 deletions Sokudoku/Interface/GraphView.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ - (id)initWithFrame:(NSRect)frame
self = [super initWithFrame:frame];
if (self) {
timeFrame = 7;
graphType = 0;
graphType = AVG_SPEED;
}

return self;
Expand Down Expand Up @@ -87,17 +87,20 @@ - (void)drawRect:(NSRect)dirtyRect
// Calculate data to graph -- initialize array for graph elements
NSMutableArray *data = [NSMutableArray arrayWithCapacity:timeFrame];
for (int i = 0; i < timeFrame; i++) {
if (graphType == 0) {
if (graphType == AVG_SPEED) {
[data addObject:[NSArray arrayWithObjects:[NSNumber numberWithFloat:0],
[NSNumber numberWithFloat:0], nil]];
} else if (graphType == 1) {
} else if (graphType == STUDY_TIME) {
[data addObject:[NSNumber numberWithFloat:0]];
} else if (graphType == 2) {
} else if (graphType == CHARACTER_COUNT) {
[data addObject:[NSNumber numberWithInt:0]];
} else if (graphType == 3) {
} else if (graphType == STUDY_TIME_WITH_SPEED) {
[data addObject:[NSArray arrayWithObjects:[NSNumber numberWithFloat:0],
[NSNumber numberWithFloat:0],
[NSNumber numberWithFloat:0], nil]];
} else if (graphType == CORRECT_RATE_WITH_COUNT || graphType == CORRECT_RATE) {
[data addObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:0],
[NSNumber numberWithInt:0], nil]];
}
}
NSArray *events = [package eventsForTag:tag];
Expand All @@ -120,45 +123,55 @@ - (void)drawRect:(NSRect)dirtyRect
float weight = [event weight];
float weightedTime = [event weightedTime];
float partialTime = [event partialTime];
if (graphType == 0) {
if (graphType == AVG_SPEED) {
NSArray *oldObject = [data objectAtIndex:bucket];
NSArray *newObject = [NSArray arrayWithObjects:[NSNumber numberWithFloat:[[oldObject objectAtIndex:0] floatValue] + weightedTime],
[NSNumber numberWithFloat:[[oldObject objectAtIndex:1] floatValue] + weight], nil];
[data replaceObjectAtIndex:bucket withObject:newObject];
} else if (graphType == 1) {
} else if (graphType == STUDY_TIME) {
[data replaceObjectAtIndex:bucket withObject:[NSNumber numberWithFloat:[[data objectAtIndex:bucket] floatValue] + partialTime]];
} else if (graphType == 2) {
} else if (graphType == CHARACTER_COUNT) {
[data replaceObjectAtIndex:bucket withObject:[NSNumber numberWithInt:[[data objectAtIndex:bucket] intValue] + 1]];
} else if (graphType == 3) {
} else if (graphType == STUDY_TIME_WITH_SPEED) {
NSArray *oldObject = [data objectAtIndex:bucket];
NSArray *newObject = [NSArray arrayWithObjects:[NSNumber numberWithFloat:[[oldObject objectAtIndex:0] floatValue] + weightedTime],
[NSNumber numberWithFloat:[[oldObject objectAtIndex:1] floatValue] + weight],
[NSNumber numberWithFloat:[[oldObject objectAtIndex:2] floatValue] + partialTime], nil];
[data replaceObjectAtIndex:bucket withObject:newObject];
} else if (graphType == CORRECT_RATE_WITH_COUNT || graphType == CORRECT_RATE) {
int currCorrect = [[[data objectAtIndex:bucket] objectAtIndex:0] intValue];
int currInCorrect = [[[data objectAtIndex:bucket] objectAtIndex:1] intValue];
if ([event correct] == YES) {
currCorrect++;
} else {
currInCorrect++;
}
[data replaceObjectAtIndex:bucket
withObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:currCorrect], [NSNumber numberWithInt:currInCorrect], nil]];
}
}

// Find maximum values to caligraph data bars
float maxValue = 0;
float altMaxValue = 0;
for (int i = 0; i < [data count]; i++) {
if (graphType == 0) {
if (graphType == AVG_SPEED) {
float average = ([[[data objectAtIndex:i] objectAtIndex:0] floatValue] /
[[[data objectAtIndex:i] objectAtIndex:1] floatValue]);
if (average > maxValue) {
maxValue = average;
}
} else if (graphType == 1) {
} else if (graphType == STUDY_TIME) {
float value = [[data objectAtIndex:i] floatValue];
if (value > maxValue) {
maxValue = value;
}
} else if (graphType == 2) {
} else if (graphType == CHARACTER_COUNT) {
int value = [[data objectAtIndex:i] intValue];
if (value > maxValue) {
maxValue = value;
}
} else if (graphType == 3) {
} else if (graphType == STUDY_TIME_WITH_SPEED) {
float average = ([[[data objectAtIndex:i] objectAtIndex:0] floatValue] /
[[[data objectAtIndex:i] objectAtIndex:1] floatValue]);
if (average > altMaxValue) {
Expand All @@ -168,13 +181,30 @@ - (void)drawRect:(NSRect)dirtyRect
if (value > maxValue) {
maxValue = value;
}
} else if (graphType == CORRECT_RATE_WITH_COUNT) {
int currCorrect = [[[data objectAtIndex:i] objectAtIndex:0] intValue];
int currInCorrect = [[[data objectAtIndex:i] objectAtIndex:1] intValue];
if (currCorrect + currInCorrect > maxValue) {
maxValue = currInCorrect + currCorrect;
}
if ((float)currInCorrect / (float)(currCorrect + currInCorrect) > altMaxValue) {
altMaxValue = (float)currInCorrect / (float)(currCorrect + currInCorrect);
}
} else if (graphType == CORRECT_RATE) {
int currCorrect = [[[data objectAtIndex:i] objectAtIndex:0] intValue];
int currInCorrect = [[[data objectAtIndex:i] objectAtIndex:1] intValue];
if ((float)currInCorrect / (float)(currCorrect + currInCorrect) > maxValue) {
maxValue = (float)currInCorrect / (float)(currCorrect + currInCorrect);
}
}
}
// Display maximum value on graph
if (graphType == 1 || graphType == 3) {
if (graphType == STUDY_TIME || graphType == STUDY_TIME_WITH_SPEED) {
text = [NSString stringWithFormat:@"%d", (int)maxValue / 60];
} else if (graphType == 2) {
} else if (graphType == CHARACTER_COUNT || graphType == CORRECT_RATE_WITH_COUNT) {
text = [NSString stringWithFormat:@"%d", (int)maxValue];
} else if (graphType == CORRECT_RATE) {
text = [NSString stringWithFormat:@"%.1f", maxValue * 100];
} else {
text = [NSString stringWithFormat:@"%.1f", maxValue];
}
Expand All @@ -185,39 +215,69 @@ - (void)drawRect:(NSRect)dirtyRect
// Draw data bars
for (int i = 0; i < timeFrame; i++) {
float value = 0;
if (graphType == 0) {
if (graphType == AVG_SPEED) {
if ([[[data objectAtIndex:i] objectAtIndex:1] intValue] != 0) {
value = ([[[data objectAtIndex:i] objectAtIndex:0] floatValue] /
[[[data objectAtIndex:i] objectAtIndex:1] floatValue]);
} else {
value = 0;
}
} else if (graphType == 1) {
} else if (graphType == STUDY_TIME) {
value = [[data objectAtIndex:i] floatValue];
} else if (graphType == 2) {
} else if (graphType == CHARACTER_COUNT) {
value = [[data objectAtIndex:i] intValue];
} else if (graphType == 3) {
} else if (graphType == STUDY_TIME_WITH_SPEED) {
value = [[[data objectAtIndex:i] objectAtIndex:2] floatValue];
} else if (graphType == CORRECT_RATE_WITH_COUNT) {
value = ([[[data objectAtIndex:i] objectAtIndex:0] intValue] +
[[[data objectAtIndex:i] objectAtIndex:1] intValue]);
} else if (graphType == CORRECT_RATE) {
int currCorrect = [[[data objectAtIndex:i] objectAtIndex:0] intValue];
int currInCorrect = [[[data objectAtIndex:i] objectAtIndex:1] intValue];
if (currCorrect + currInCorrect > 0) {
value = (float)currInCorrect / (float)(currCorrect + currInCorrect);
}
}
float width = (boundsX - scale*4) / (float)timeFrame;
float start = scale*3 + (boundsX - scale*4) * (1 - (float)i / (float)timeFrame) - width;
[[NSColor colorWithCalibratedRed:0.5 green:0.5 blue:0.5 alpha:1.0] set];
if (graphType == CORRECT_RATE_WITH_COUNT) {
[[NSColor colorWithCalibratedRed:0.25 green:0.5 blue:0.25 alpha:1.0] set];
} else {
[[NSColor colorWithCalibratedRed:0.5 green:0.5 blue:0.5 alpha:1.0] set];
}
rect = NSMakeRect(start + 1, scale + (boundsY - scale*3) * (1 - value / maxValue), width, (boundsY - scale*3) * (value / maxValue) - 1);
[NSBezierPath fillRect:rect];

if (graphType == CORRECT_RATE_WITH_COUNT) {
value = [[[data objectAtIndex:i] objectAtIndex:1] intValue];
start = scale*3 + (boundsX - scale*4) * (1 - (float)i / (float)timeFrame) - width;
[[NSColor colorWithCalibratedRed:0.65 green:0.25 blue:0.25 alpha:1.0] set];
rect = NSMakeRect(start + 1, scale + (boundsY - scale*3) * (1 - value / maxValue), width, (boundsY - scale*3) * (value / maxValue) - 1);
[NSBezierPath fillRect:rect];
}
}

// Draw line for average speed for combination graph
[[NSColor colorWithCalibratedRed:0.75 green:0.75 blue:0.25 alpha:1.0] set];

float lastValue = 0;
line = nil;
if (graphType == 3) {
if (graphType == STUDY_TIME_WITH_SPEED || graphType == CORRECT_RATE_WITH_COUNT) {
for (int i = timeFrame - 1; i >= 0; i--) {
float value = lastValue;
if ([[[data objectAtIndex:i] objectAtIndex:1] intValue] != 0) {
value = ([[[data objectAtIndex:i] objectAtIndex:0] floatValue] /
[[[data objectAtIndex:i] objectAtIndex:1] floatValue]);
lastValue = value;
if (graphType == STUDY_TIME_WITH_SPEED) {
if ([[[data objectAtIndex:i] objectAtIndex:1] intValue] != 0) {
value = ([[[data objectAtIndex:i] objectAtIndex:0] floatValue] /
[[[data objectAtIndex:i] objectAtIndex:1] floatValue]);
lastValue = value;
}
} else if (graphType == CORRECT_RATE_WITH_COUNT) {
int currCorrect = [[[data objectAtIndex:i] objectAtIndex:0] intValue];
int currInCorrect = [[[data objectAtIndex:i] objectAtIndex:1] intValue];
if (currCorrect + currInCorrect > 0) {
value = (float)currInCorrect / (float)(currCorrect + currInCorrect);
lastValue = value;
}
}
float width = (boundsX - scale*4) / (float)timeFrame;
float start = scale*3 + (boundsX - scale*4) * (1 - ((float)i - 0.5) / (float)timeFrame) - width;
Expand All @@ -229,10 +289,10 @@ - (void)drawRect:(NSRect)dirtyRect
[line lineToPoint:NSMakePoint(start + 1, scale + (boundsY - scale*3) * (1 - value / altMaxValue) - 1)];
}
}
[line lineToPoint:NSMakePoint(boundsX - scale, scale + (boundsY - scale*3) * (1 - lastValue / altMaxValue) - 1)];
[line setLineWidth:1];
[line stroke];
}
[line lineToPoint:NSMakePoint(boundsX - scale, scale + (boundsY - scale*3) * (1 - lastValue / altMaxValue) - 1)];
[line setLineWidth:1];
[line stroke];
}

@end
2 changes: 2 additions & 0 deletions Sokudoku/Interface/GraphWindow.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ - (void)windowDidLoad
[graphType addItemWithTitle:@"Average Answer Speed"];
[graphType addItemWithTitle:@"Study Time Per Day"];
[graphType addItemWithTitle:@"Characters Per Day"];
[graphType addItemWithTitle:@"Percent Incorrect"];
[graphType addItemWithTitle:@"Average Speed + Study Time"];
[graphType addItemWithTitle:@"Answers + Percent Incorrect"];

[timeFrame removeAllItems];
[timeFrame addItemWithTitle:@"One Week"];
Expand Down

0 comments on commit 45e191c

Please sign in to comment.