I would like to draw a bar chart in an existing view on the user interface. The single bar highs are calculated after user input and stored in an array. The array should be passed to my custom GraphView class after a button is bushed to draw the bar chart finally. But it doesn't work. I don't know if I pass the array right (how can I check it?) and how to access it in my drawrect method to set the single bar highs? As soon I put NSNumber *balkenHoehe = [balkenArray objectAtIndex:i];
in the drawrect method and balkenHoehe
as rect-hight I get errors. WHAT DO I HAVE TO DO? Thanks!
here is my code:
my AppDelegate.h
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSTextField *eingabeNennmass;
@property (weak) IBOutlet NSTextField *eingabeToleranzOben;
@property (weak) IBOutlet NSTextField *eingabeToleranzUnten;
@property (weak) IBOutlet NSTextField *eingabeAnzahlWerte;
@property (weak) IBOutlet NSTextField *ausgabeMittelwert;
@property (weak) IBOutlet NSTextField *ausgabeToleranz
@property (weak, nonatomic) IBOutlet GraphView *ausgabeGraph;
- (IBAction)pushSimulation:(NSButton *)sender;
@end
my AppDelegate.m shortened
#import "GraphView.h"
#import "AppDelegate.h"
implementation AppDelegate
....//couple calculating code and building the array klassenArray//...
[klassenArray addObject:[NSNumber numberWithDouble: n]];
....//more code//...
[_ausgabeGraph setBalkenArray:klassenArray]; (connected with the GraphView view on the user interface)
....//more code//...
my GraphView.h
#import <Cocoa/Cocoa.h>
@interface GraphView : NSView
{
NSMutableArray *balkenArray;
}
- (NSMutableArray *) balkenArray;
- (void) setBalkenArray:(NSMutableArray *)abalkenArray;
@end
my GraphView.m
#import "GraphView.h"
@implementation GraphView
//*******************************
- (void) setBalkenArray:(NSMutableArray *)abalkenArray
{
balkenArray = abalkenArray;
[self setNeedsDisplay:YES];
}
//*******************************
- (NSMutableArray *)balkenArray
{
return balkenArray;
}
//*******************************
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
//*******************************
- (void)drawRect:(NSRect)dirtyRect
{
NSRect bounds = [self bounds];
float fieldWith = bounds.size.width / 12.0;
float fieldHeight = bounds.size.height / 12.0;
//background
NSRect hintergrundRect =
hintergrundRect = NSMakeRect(bounds.origin.x, bounds.origin.y,
bounds.size.width, bounds.size.height);
[[NSColor grayColor] set];
[NSBezierPath fillRect:hintergrundRect];
//Diagram
for (int i = 1; i<=10; i++) {
NSNumber *balkenHoehe = [balkenArray objectAtIndex:i];
// NSLog(@"rectnumber at index %d is %@", i, balkenHoehe);
NSRect aRect =
aRect = NSMakeRect (fieldWith*i, fieldHeight, fieldWith, balkenHoehe); //x, y, width, hight
// draw rect
[[NSColor whiteColor] set];
[NSBezierPath fillRect:aRect];
// draw border
[[NSColor blackColor] set];
NSBezierPath *aPath = [NSBezierPath bezierPathWithRect:aRect];
[aPath setLineWidth:1];
[aPath stroke];
}
}
@end